using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Microsoft.Extensions.DependencyInjection;
using SlnMesnac.Business;
using SlnMesnac.Model.domain;
using SlnMesnac.Repository.service;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* 版权所有 (c) 2024 WenJY 保留所有权利。
* CLR版本：4.0.30319.42000
* 机器名称：LAPTOP-E0N2L34V
* 命名空间：SlnMesnac.WPF.ViewModel
* 唯一标识：a5944d3a-5653-4b07-bc01-6782c056b849
*
* 创建者：WenJY
* 电子邮箱：wenjy@mesnac.com
* 创建时间：2024-04-15 10:42:25
* 版本：V1.0.0
* 描述：
*
*--------------------------------------------------------------------
* 修改人：
* 时间：
* 修改说明：
*
* 版本：V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
namespace SlnMesnac.WPF.ViewModel
{
    public class BaseConfigInfoViewModel : ViewModelBase
    {

        private readonly ConfigInfoBusiness _configInfoBusiness;

        public BaseConfigInfoViewModel()
        {
            _configInfoBusiness = App.ServiceProvider.GetService<ConfigInfoBusiness>();

            QuerySearchCommand = new RelayCommand<string>(Query);

            DeleteCommand = new RelayCommand<int>(Delete);

            ResetSearchCommand = new RelayCommand(ResetSearch);

            Query(string.Empty);
        }



        private string _searchStr = string.Empty;
        public string SearchStr
        {
            get { return _searchStr; }
            set
            {
                _searchStr = value;
                RaisePropertyChanged(nameof(SearchStr));
            }
        }

        private ObservableCollection<BaseConfigInfo> tablesDataGrid;

        public ObservableCollection<BaseConfigInfo> TablesDataGrid
        {
            get { return tablesDataGrid; }
            set { tablesDataGrid = value; RaisePropertyChanged(() => TablesDataGrid); }

        }
        public RelayCommand<string> QuerySearchCommand { get; set; }

        public RelayCommand<int> DeleteCommand { get; set; }

        public RelayCommand ResetSearchCommand { get;set; }

        /// <summary>
        /// 查询事件
        /// </summary>
        /// <param name="search"></param>
        private void Query(string search)
        {
            List<BaseConfigInfo> configInfos = _configInfoBusiness.GetConfigInfos();
            
            if (configInfos != null)
            {

                if (!string.IsNullOrEmpty(search))
                {
                    configInfos = configInfos.Where(x=>x.ConfigName ==  search).ToList();
                }

                TablesDataGrid = new ObservableCollection<BaseConfigInfo>();
                configInfos.ForEach(t => {  
                    TablesDataGrid.Add(t);  
                });
            }
        }

        public void Save(string configKey, string configName, string configValue)
        {
            BaseConfigInfo baseConfig = new BaseConfigInfo()
            {
                ConfigKey = configKey,
                ConfigName = configName,
                ConfigValue = configValue,
                IsFlag = 1,
                CreateTime = DateTime.Now,
            };

            _configInfoBusiness.SaveConfigInfo(baseConfig);

            Query(string.Empty);
        }
        public void Edit(string configId,string configKey, string configName, string configValue)
        {
            BaseConfigInfo baseConfig = new BaseConfigInfo()
            {
                ConfigId = Convert.ToInt32(configId),
                ConfigKey = configKey,
                ConfigName = configName,
                ConfigValue = configValue,
                IsFlag = 1,
                CreateTime = DateTime.Now,
            };

            _configInfoBusiness.UpdateConfigInfo(baseConfig);
            Query(string.Empty);
        }

        private void Delete(int configId)
        {
            _configInfoBusiness.DeleteConfigInfo(configId);
            Query(string.Empty);
        }

        private void ResetSearch()
        {
            SearchStr = string.Empty;

            Query(string.Empty);
        }

    }
}
