using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Microsoft.Extensions.DependencyInjection;
using Quartz.Util;
using SlnMesnac.Model.domain;
using SlnMesnac.Repository.service;
using SlnMesnac.WPF.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace SlnMesnac.WPF.ViewModel
{
    public class SetKindWindowViewModel : ObservableObject
    {
      
        private ILogoFormulaService? formulaService;
        private ILogoConfigService? logoConfigService;
        private string CachMaterialType;
        // 缓存第一次查到的海康配方
        public Dictionary<string, int> TypeCache = new Dictionary<string, int>();
        public LogoConfig record = null;

        public delegate void SetKindWindowDelegate();

        public SetKindWindowViewModel(string materialType)
        {
            formulaService = App.ServiceProvider.GetService<ILogoFormulaService>();
            logoConfigService = App.ServiceProvider.GetService<ILogoConfigService>();
            SaveCommand = new RelayCommand(Save);
            // 初始加载内容
            Init(materialType);


        }

        public  void Init(string materialType)
        {
            List<LogoFormula> list = formulaService.GetListAsync().Result;
            record =  logoConfigService.GetByMaterialType(materialType);
            if (record == null) return;
            _MaterialType = record.MaterialName;
            _IsDetectionChecked = record.IsChecked == 0 ? false : true;
            Ch1HightText = record.Ch1Hight.ToString();
            Ch2HightText = record.Ch2Hight.ToString();
            _selectedMaterialType = list.FirstOrDefault(x=>x.Key==record.CheckKind)?.Value;
            _selectedLightType = record.LightType == 0 ? "All" : (record.LightType == 1 ? "Ch1" : "Ch2");
            if(list!=null && list.Count > 0)
            {
                foreach (var item in list)
                {
                    MaterialTypes.Add(item.Value);
                    TypeCache.Add(item.Value, item.Key);  
                }
            }

            _lightTypes.Add("All");
            _lightTypes.Add("Ch1");
            _lightTypes.Add("Ch2");
           
        }

        public  void Save()
        {
            if (string.IsNullOrEmpty(_selectedMaterialType) && IsDetectionChecked)
            {
                MessageBox.Show("请选择型号");
                return;
            }
            if(int.TryParse(Ch1HightText, out  int ch1High) && int.TryParse(Ch2HightText, out int ch2High) && ch1High>=0 && ch1High<=255 && ch2High >= 0 && ch2High <= 255)
            {
                record.Ch1Hight = ch1High;
                record.Ch2Hight = ch2High;
            }
            else
            {
                MessageBox.Show("灯光亮度ch1和ch2请设置在0-255之间");
                return;

            }

            if (!string.IsNullOrEmpty(_selectedMaterialType)){
                //需要检测
                string select = _selectedMaterialType;
                TypeCache.TryGetValue(select, out int key);
                record.CheckKind = key;
            }
            #region 修改配方
            if (record != null)
            {
                record.IsChecked = IsDetectionChecked ? 1 : 0;
                if(record.IsChecked == 0)
                {
                    // 不检测，复位0
                    record.CheckKind = 0;
                }
                if (!string.IsNullOrEmpty(_selectedLightType))
                {
                    //需要修改灯光配置
                    string select = _selectedLightType;
                    switch (select)
                    {
                        case "All": record.LightType = 0;  break;
                        case "Ch1": record.LightType = 1; break;
                        case "Ch2": record.LightType = 2; break;
                    }
   
                }
                logoConfigService.updateByMaterialType(record);
            }
            #endregion
            MessageBox.Show("修改完成");
            
        }

        #region 属性
        /// <summary>
        /// 型号
        /// </summary>
        private string _MaterialType;
        public string MaterialType
        {
            get { return _MaterialType; }
            set { _MaterialType = value; RaisePropertyChanged(); }
        }

       
        /// <summary>
        /// ch1亮度
        /// </summary>
        private string _Ch1HightText;
        public string Ch1HightText
        {
            get { return _Ch1HightText; }
            set { _Ch1HightText = value; RaisePropertyChanged(); }
        }

        /// <summary>
        /// ch2亮度
        /// </summary>
        private string _Ch2HightText;
        public string Ch2HightText
        {
            get { return _Ch2HightText; }
            set { _Ch2HightText = value; RaisePropertyChanged(); }
        }



        /// <summary>
        /// 单选框
        /// </summary>
        private bool _IsDetectionChecked;
        public bool IsDetectionChecked
        {
            get { return _IsDetectionChecked; }
            set { _IsDetectionChecked = value; RaisePropertyChanged(); }
        }



        private string _selectedMaterialType;
        public string SelectedMaterialType
        {
            get { return _selectedMaterialType; }
            set
            {
                _selectedMaterialType = value;
                RaisePropertyChanged();
                // 可以在这里处理选中项变化后的逻辑
            }
        }
        /// <summary>
        /// 识别特征下拉框集合
        /// </summary>
        private ObservableCollection<string> _materialTypes = new ObservableCollection<string>();
        public ObservableCollection<string> MaterialTypes
        {
            get { return _materialTypes; }
            set
            {
                _materialTypes = value;
                RaisePropertyChanged();
            }
        }


        private string _selectedLightType;
        public string SelectedLightType
        {
            get { return _selectedLightType; }
            set
            {
                _selectedLightType = value;
                RaisePropertyChanged();
                // 可以在这里处理选中项变化后的逻辑
            }
        }


        /// <summary>
        /// 灯光特征下拉框集合
        /// </summary>
        private ObservableCollection<string> _lightTypes = new ObservableCollection<string>();
        public ObservableCollection<string> LightTypes
        {
            get { return _lightTypes; }
            set
            {
                _lightTypes = value;
                RaisePropertyChanged();
            }
        }


        


        #endregion

        public RelayCommand SaveCommand { get; set; }
    }
}
