using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.WindowsAPICodePack.Dialogs;
using SlnMesnac.Config;
using SlnMesnac.Generate;
using SlnMesnac.WPF.Attribute;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Windows;

#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* 版权所有 (c) 2024 WenJY 保留所有权利。
* CLR版本：4.0.30319.42000
* 机器名称：LAPTOP-E0N2L34V
* 命名空间：SlnMesnac.WPF.ViewModel.Generate
* 唯一标识：7f1ddac5-3ff3-4974-ac90-d6eb684167c8
*
* 创建者：WenJY
* 电子邮箱：wenjy@mesnac.com
* 创建时间：2024-04-11 09:31:46
* 版本：V1.0.0
* 描述：
*
*--------------------------------------------------------------------
* 修改人：
* 时间：
* 修改说明：
*
* 版本：V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
namespace SlnMesnac.WPF.ViewModel.Generate
{
    [RegisterAsSingleton]
    public class GenerateControlViewModel : ObservableObject
    {
        private readonly AppConfig _appConfig;

        private readonly GenerateCode _generateCode;

        public GenerateControlViewModel(AppConfig appConfig, GenerateCode generateCode)
        {
            _appConfig = appConfig;
            _generateCode = generateCode;

            var configIds = _appConfig.sqlConfig.Select(x => x.configId).ToList();

            // 初始化选项列表
            Options = new ObservableCollection<string>();

            foreach (var configId in configIds)
            {
                Options.Add(configId);
            }

            QuerySearchCommand = new RelayCommand<string>(Query);

            CreateCodeCommand = new RelayCommand<string>(CreateCode);
        }

        #region 参数定义
        private ObservableCollection<string> _options;
        public ObservableCollection<string> Options
        {
            get => _options;
            set => SetProperty(ref _options, value);
        }

        private string _selectedOption;
        public string SelectedOption
        {
            get => _selectedOption;
            set => SetProperty(ref _selectedOption, value);
        }

        private ObservableCollection<DbTableInfo> tablesDataGrid;

        public ObservableCollection<DbTableInfo> TablesDataGrid
        {
            get => tablesDataGrid;
            set => SetProperty(ref tablesDataGrid, value);

        }
        #endregion

        #region 事件定义
        public RelayCommand<string> QuerySearchCommand { get; set; }

        public RelayCommand<string> CreateCodeCommand { get; set; }
        #endregion

        /// <summary>
        /// 查询事件
        /// </summary>
        /// <param name="search"></param>
        private void Query(string search)
        {
            var configId = _selectedOption;

            if (!string.IsNullOrEmpty(configId))
            {
                var db = App.ServiceProvider.GetService<ISqlSugarClient>();
                var scope = db.AsTenant().GetConnectionScope(configId);

                List<DbTableInfo> tables = scope.DbMaintenance.GetTableInfoList(false);

                if (tables != null)
                {
                    TablesDataGrid = new ObservableCollection<DbTableInfo>();
                    tables.ForEach(t => { TablesDataGrid.Add(t); });
                }
            }


        }

        private void CreateCode(string tableName)
        {
            var info = tableName;
            var configId = _selectedOption;

            string nameSpace = "SlnMesnac.Repository";

            try
            {
                using (CommonOpenFileDialog dialog = new CommonOpenFileDialog())
                {
                    dialog.IsFolderPicker = true; // 设置为选择文件夹
                    if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
                    {
                        string selectedPath = dialog.FileName;

                        var res = _generateCode.CreateCode(configId, tableName, selectedPath, nameSpace);

                        if (res)
                        {
                            MessageBox.Show($"{tableName}代码生成成功");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"{tableName}代码生成失败：{ex.Message}");
            }
        }

    }
}
