using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using StartServerWPF.Modules.Main.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace StartServerWPF.Modules.Main.ViewModels
{
    public class SetParamViewModel : BindableBase
    {
        public SetParamViewModel(MainViewModel mainViewModel)
        {
            _systemConfig = mainViewModel.sc;
            MainModel = _systemConfig.vpnInfo;
            MoniTime = mainViewModel.MoniStartTime;
        }
        private string _title = "应用列表";
        public string Title => _title;

  

        private SystemConfig _systemConfig;
        private VpnInfo _mainModel = new VpnInfo();
        public VpnInfo MainModel
        {
            get => _mainModel;
            set { SetProperty(ref _mainModel, value); }
        }
        private DateTime _moniTime ;
        public DateTime MoniTime
        {
            get => _moniTime;
            set { SetProperty(ref _moniTime, value); }
        }

        public DelegateCommand FilePathSaveCommand=> new(()=>
        {
            updateJson = false;
            System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
            fbd.SelectedPath = MainModel.DataSavePath;
            fbd.ShowNewFolderButton = true;
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                MainModel.DataSavePath = fbd.SelectedPath;
                if (MainModel.DataSavePath != _systemConfig.vpnInfo.DataSavePath)
                {
                    updateJson = true;
                }
            }
        });
        public DelegateCommand LogPathSaveCommand=> new(() =>
        {
            System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
            fbd.SelectedPath = System.Environment.CurrentDirectory + "\\" + MainModel.SystemLogPath;
            fbd.ShowNewFolderButton = true;
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {

                MainModel.SystemLogPath = fbd.SelectedPath;
            }
        });
        bool updateJson = false;

        public ICommand ConfirmCommand
        {
            get => new DelegateCommand(() =>
            {
                // 确认操作
                // 数据校验（关键字段不能为空、年龄做数字区间的校验、做UserName的唯一检查（自定义特性检查）
                if (string.IsNullOrEmpty(MainModel.VpnName) || string.IsNullOrEmpty(MainModel.VpnIP))
                {
                    MessageBox.Show("内容不能为空", "提示", MessageBoxButton.OK);
                    return;
                }
                if (MessageBox.Show("确认修改？", "参数设置", MessageBoxButton.OKCancel,
                      MessageBoxImage.Exclamation) == MessageBoxResult.OK)
                {


                    JsonParser.WriteSystemConfigFile(JsonParser.systemConfigPath, _systemConfig);
                    if (updateJson)
                    {
                        JsonParser.UpdateRecvJson(_systemConfig.proRecv.ProPath + _systemConfig.proRecv.JsonPath, _systemConfig.vpnInfo.DataSavePath);
                        JsonParser.UpdateApmsJson(_systemConfig.proApms.ProPath + _systemConfig.proApms.JsonPath, _systemConfig.vpnInfo.DataSavePath);
                    }
                  //  RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
                }
            });
        }

    }
}
