using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.DependencyInjection;
using SlnMesnac.Business;
using SlnMesnac.Model.dto;
using SlnMesnac.Repository;
using SlnMesnac.Repository.service;
using SlnMesnac.WPF.Page.CabinetInfo;

#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* 版权所有 (c) 2024 WenJY 保留所有权利。
* CLR版本：4.0.30319.42000
* 机器名称：T14-GEN3-7895
* 命名空间：SlnMesnac.WPF.ViewModel
* 唯一标识：03d78dae-fa26-4923-877c-f2bba859ee05
*
* 创建者：WenJY
* 电子邮箱：
* 创建时间：2024-11-08 14:30:02
* 版本：V1.0.0
* 描述：
*
*--------------------------------------------------------------------
* 修改人：
* 时间：
* 修改说明：
*
* 版本：V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
namespace SlnMesnac.WPF.ViewModel
{
    public partial class CabinetInfoViewModel : ObservableObject
    {
        private readonly Ibase_cabinet_infoServices _ibase_Cabinet_InfoServices;
        private readonly Ibase_busbar_infoServices _ibase_Busbar_InfoServices;

        private readonly CabinetInfoBusiness cabinetInfoBusiness;
        private readonly BusbarInfoBusiness busbarInfoBusiness;

        public CabinetInfoViewModel()
        {
            _ibase_Cabinet_InfoServices = App.ServiceProvider.GetService<Ibase_cabinet_infoServices>();
            _ibase_Busbar_InfoServices = App.ServiceProvider.GetService<Ibase_busbar_infoServices>();
            cabinetInfoBusiness = App.ServiceProvider.GetService<CabinetInfoBusiness>();
            busbarInfoBusiness = App.ServiceProvider.GetService<BusbarInfoBusiness>();
        }

        public string _cabinetCodeSearch = string.Empty;
        public string CabinetCodeSearch
        {
            get => _cabinetCodeSearch;
            set => SetProperty(ref _cabinetCodeSearch, value);
        }

        public string _cabinetAliasSearch = string.Empty;
        public string CabinetAliasSearch
        {
            get => _cabinetAliasSearch;
            set => SetProperty(ref _cabinetAliasSearch, value);
        }

        //public int _isCheckSearch = 0;
        //public int IsCheckSearch
        //{
        //    get => _isCheckSearch;
        //    set => SetProperty(ref _isCheckSearch, value);
        //}

        private ObservableCollection<base_cabinet_info> _cabinetInfoItems = new ObservableCollection<base_cabinet_info>();
        public ObservableCollection<base_cabinet_info> CabinetInfoItems
        {
            get => _cabinetInfoItems;
            set => SetProperty(ref _cabinetInfoItems, value);
        }

        private ObservableCollection<base_busbar_info> _busbarInfoItems = new ObservableCollection<base_busbar_info>();
        public ObservableCollection<base_busbar_info> BusbarInfoItems
        {
            get => _busbarInfoItems;
            set => SetProperty(ref _busbarInfoItems, value);
        }

        [RelayCommand]
        private void QueryCabinetInfo()
        {
            int cabinetCode = 0;
            if (IsNumber(_cabinetCodeSearch))
            {
                cabinetCode = Convert.ToInt32(_cabinetCodeSearch);
            }
            cabinetInfoBusiness.QueryCabinetInfo(cabinetCode, _cabinetAliasSearch, null, out List<base_cabinet_info> info);

            CabinetInfoItems = new ObservableCollection<base_cabinet_info>(info);
        }

        [RelayCommand]
        private void CabinetUpdate(base_cabinet_info cabinet_Info)
        {
            CabinetUpdateWindow cabinetUpdateWindow = new CabinetUpdateWindow(cabinet_Info);
            cabinetUpdateWindow.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
            cabinetUpdateWindow.Topmost = true;
            var res = cabinetUpdateWindow.ShowDialog();
            this.QueryCabinetInfo();

        }

        [RelayCommand]
        private void BusbarUpdate(base_busbar_info busbar_Info)
        {
            BusbarUpdateWindow busbarUpdateWindow = new BusbarUpdateWindow(busbar_Info);
            busbarUpdateWindow.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
            busbarUpdateWindow.Topmost = true;
            var res = busbarUpdateWindow.ShowDialog();
            this.QueryBusbarInfo(Convert.ToInt32(busbar_Info.cabinetCode));
        }

        public void QueryBusbarInfo(int cabinetCode)
        {
            busbarInfoBusiness.QueryBusbarInfo(cabinetCode, out List<base_busbar_info> info);
            BusbarInfoItems = new ObservableCollection<base_busbar_info>(info);

        }

        private bool IsNumber(string input)
        {
            string pattern = @"^[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?$";
            return Regex.IsMatch(input, pattern);
        }
    }
}
