using HandyControl.Tools.Extension;
using LogoForceTestApp.Core.Mvvm;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Validar;

namespace LogoForceTestApp.Modules.MainModule.ViewModels
{
    [InjectValidation]
    internal class OeeDialogPageViewModel : BindableBase, IDialogAware
    {
        public DelegateCommand CloseCmd { get; set; }
        public string Message { get; set; }
        public DateTime ExceptionTime{ get; set; }
        public string Result { get; set; }
        public Action CloseAction { get ; set ; }

        public string Title { get; }
        public string OP_Id { get; set; }
        [Required]
        public string ExceptionContent { get; set; }
        [Required]
        public string ExceptionCode { get; set; }
        [Required]
        public string ExceptionReason { get; set; }
        [Range(minimum:1,maximum:int.MaxValue)]
        public int Duration { get; set; } = 2;
        public List<string> ExeceptionLists { get; set; }
        public OeeDialogPageViewModel()
        {
            Title = "设备异常处理";
            ExceptionTime = DateTime.Now;
            CloseCmd = new DelegateCommand(Close);
            ExeceptionLists = new List<string>
            {
                "1500002-其他",
                "6014009-急停",
                "30540000-维修保养"
            };
        }

        public bool Check()
        {
            return !string.IsNullOrWhiteSpace(OP_Id)&&
                !string.IsNullOrWhiteSpace(ExceptionCode)&&
                !string.IsNullOrWhiteSpace(ExceptionContent)&&
                !string.IsNullOrWhiteSpace(ExceptionReason)&&
                Duration>0;  
        }
        public event Action<IDialogResult> RequestClose;

        private void Close()
        {
            var dialog = new DialogResult();
            dialog.Parameters.Add("OP_Id", OP_Id);
            dialog.Parameters.Add("ExceptionCode", ExceptionCode);
            dialog.Parameters.Add("ExceptionContent", ExceptionContent);
            dialog.Parameters.Add("Duration", Duration);
            dialog.Parameters.Add("ExceptionTime", ExceptionTime.ToString("yyyy-mm-dd HH:mm:ss"));
            dialog.Parameters.Add("Mode", "手动调试模式");
            dialog.Parameters.Add("Reason", ExceptionReason);
            RequestClose?.Invoke(dialog);
        }

        public bool CanCloseDialog()
        {
            return Check();
        }

        public void OnDialogClosed()
        {
            
        }
        
        public void OnDialogOpened(IDialogParameters parameters)
        {
            OP_Id = parameters.GetValue<string>("OP_Id");
            ExceptionCode = parameters.GetValue<string>("ExceptionCode");
        }
    }
    class DialogResult : IDialogResult
    {
        public DialogResult()
        {
            Parameters=new DialogParameters();
            Result = ButtonResult.OK;
        }
        public IDialogParameters Parameters { get; }

        public ButtonResult Result { get; }
    }
}