using Caliburn.Micro;
using Caliburn.Micro.Core;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using FurnaceUI.Models;
using FurnaceUI.Views.Jobs;
using System.Windows.Controls;
using MECF.Framework.UI.Client.CenterViews.Parameter;
namespace FurnaceUI.Views.Editors
{
    public class AlarmErrorCommandViewModel : FurnaceUIViewModelBase
    {
        private string selectCommand;
        public string SelectCommand
        {
            get { return selectCommand; }
            set { selectCommand = value; this.NotifyOfPropertyChange(nameof(SelectCommand)); }
        }
        private AlarmErrorCommandView _view;
        public string ResultParameterStr { get; set; }
        private string _alarmStr;
        public string AlarmStr
        {
            get { return _alarmStr; }
            set
            {
                _alarmStr = value; this.NotifyOfPropertyChange(nameof(AlarmStr));
            }
        }
        private Visibility _isJumpVisibility = Visibility.Collapsed;
        public Visibility IsJumpVisibility
        {
            get { return _isJumpVisibility; }
            set { _isJumpVisibility = value; this.NotifyOfPropertyChange(nameof(IsJumpVisibility)); }
        }
        private Visibility _isAlarmVisibility = Visibility.Collapsed;
        public Visibility IsAlarmVisibility
        {
            get { return _isAlarmVisibility; }
            set { _isAlarmVisibility = value; this.NotifyOfPropertyChange(nameof(IsAlarmVisibility)); }
        }
        private Visibility _isAbortVisibility = Visibility.Collapsed;
        public Visibility IsAbortVisibility
        {
            get { return _isAbortVisibility; }
            set { _isAbortVisibility = value; this.NotifyOfPropertyChange(nameof(IsAbortVisibility)); }
        }
        private string _abortStr;
        public string AbortStr
        {
            get { return _abortStr; }
            set { _abortStr = value; this.NotifyOfPropertyChange(nameof(AbortStr)); }
        }
        private string _jumpStr;
        public string JumpStr
        {
            get { return _jumpStr; }
            set { _jumpStr = value; this.NotifyOfPropertyChange(nameof(JumpStr)); }
        }
        protected override void OnInitialize()
        {
            base.OnInitialize();
            InitControl();
        }
        /// 
        /// 初始化按钮 是否被选中 
        /// 
        private void InitControl()
        {
            if (string.IsNullOrEmpty(selectCommand))
                return;
            _view = ((GetView() as Window).Content) as AlarmErrorCommandView;
            var key = selectCommand.Split(' ')[0];
            Dictionary dicNames = new Dictionary() {
                { "Call Alarm Recipe","CallAlarmRecipe"},
                { "Call Abort Recipe","CallAbortRecipe"},
                { "ABORT","CallAbortRecipe"},
                { "CALL","CallAlarmRecipe"},
                { "JUMP","JumpStep"},
            };
            string nameKey = dicNames.ContainsKey(key) ? dicNames[key] : selectCommand;
            var control = _view.FindName(nameKey);
            if (control is null)
                return;
            RadioButton radioButton = control as RadioButton;
            radioButton.IsChecked = true;
            if (key.Equals("CALL"))
            {
                AlarmStr = selectCommand.Replace("CALL ","");
                IsAlarmVisibility = Visibility.Visible;
            }
            else if (key.Equals("ABORT"))
            {
                AbortStr = selectCommand.Replace("ABORT ", "");
                IsAbortVisibility = Visibility.Visible;
            }
            else if (key.Equals("JUMP"))
            {
                JumpStr= selectCommand.Replace("JUMP ", "");
                IsJumpVisibility = Visibility.Visible;
            }
        }
        public void SetCommand(string commandContent)
        {
           
            var windowManager = IoC.Get();
            AlarmErrorCallRecipeViewModel errorCallRecipeViewModel = new AlarmErrorCallRecipeViewModel();
            if (commandContent.Equals("Call Alarm Recipe"))
            {
                errorCallRecipeViewModel.IsAlarmVisibility = Visibility.Visible;
                errorCallRecipeViewModel.ResultParameterStr = AlarmStr;
                if ((bool)(windowManager as WindowManager)?.ShowDialogWithTitle(errorCallRecipeViewModel, null, "Call Alarm Recipe"))
                {
                    ResultParameterStr = errorCallRecipeViewModel.ResultParameterStr;
                    AlarmStr = ResultParameterStr;
                    IsAlarmVisibility = Visibility.Visible;
                    IsAbortVisibility = Visibility.Collapsed;
                    IsJumpVisibility = Visibility.Collapsed;
                }
                else
                {
                    ((Window)GetView()).DialogResult = false;
                }
                selectCommand = "CALL";
            }
            else if (commandContent.Equals("JumpStep"))
            {
                AlarmErrorJumpStepViewModel alarmErrorJumpStepView = new AlarmErrorJumpStepViewModel();
                alarmErrorJumpStepView.ResultParameterStr = JumpStr;
                if ((bool)(windowManager as WindowManager)?.ShowDialogWithTitle(alarmErrorJumpStepView, null, "Jump"))
                {
                    ResultParameterStr = alarmErrorJumpStepView.ResultParameterStr;
                    JumpStr = ResultParameterStr;
                    IsJumpVisibility = Visibility.Visible;
                    IsAbortVisibility = Visibility.Collapsed;
                    IsAlarmVisibility = Visibility.Collapsed;
                }
                else
                {
                    ((Window)GetView()).DialogResult = false;
                }
                selectCommand = "JUMP";
            }
            else if (commandContent.Equals("Call Abort Recipe"))
            {
                errorCallRecipeViewModel = new AlarmErrorCallRecipeViewModel();
                errorCallRecipeViewModel.IsAlarmVisibility = Visibility.Hidden;
                errorCallRecipeViewModel.ResultParameterStr = AbortStr;
                if ((bool)(windowManager as WindowManager)?.ShowDialogWithTitle(errorCallRecipeViewModel, null, "Call Abort Recipe"))
                {
                    ResultParameterStr = errorCallRecipeViewModel.ResultParameterStr;
                    AbortStr = ResultParameterStr;
                    IsAbortVisibility = Visibility.Visible;
                    IsAlarmVisibility = Visibility.Collapsed;
                    IsJumpVisibility = Visibility.Collapsed;
                }
                else {
                    ((Window)GetView()).DialogResult = false;
                }
                selectCommand = "ABORT";
            }
            else
            {
                ResultParameterStr = null;
                selectCommand = commandContent;
            }
        }
        public void CloseCommand()
        {
            ((Window)GetView()).DialogResult = false;
        }
        public void OKCommand()
        {
            ((Window)GetView()).DialogResult = true;
        }
    }
}