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

namespace StartServerWPF.Modules.Main.ViewModels
{
    public class AddWorkAreaDialogViewModel : BindableBase, IDialogAware
    {
        public AddWorkAreaDialogViewModel()
        {
        }
        private string _title = "添加工区";
        public string Title => _title;

        public event Action<IDialogResult> RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed() { }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            var _type = parameters.GetValue<int>("type");
        }

        private string _workAreaName;
        public string WorkAreaName
        {
            get => _workAreaName;
            set { SetProperty(ref _workAreaName, value); }
        }
        private string _workAreaPath;
        public string WorkAreaPath
        {
            get => _workAreaPath;
            set { SetProperty(ref _workAreaPath, value); }
        }
        public DelegateCommand FilePathSaveCommand=> new(()=>
        {
            System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
            fbd.ShowNewFolderButton = true;
            if(string.IsNullOrEmpty(WorkAreaName))
            {
                MessageBox.Show("工区名称不能为空");
                return;
            }
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string path = Path.Combine(fbd.SelectedPath, WorkAreaName);
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                WorkAreaPath = path;
            }
        });
        public ICommand ConfirmCommand
        {
            get => new DelegateCommand(() =>
            {
                // 确认操作
                // 数据校验（关键字段不能为空、年龄做数字区间的校验、做UserName的唯一检查（自定义特性检查）
                if (string.IsNullOrEmpty(WorkAreaName) || string.IsNullOrEmpty(WorkAreaPath))
                {
                    MessageBox.Show("工区名称和目录不能为空", "提示", MessageBoxButton.OK);
                    return;
                }
                if (MessageBox.Show("确认添加？", "提示", MessageBoxButton.OKCancel,
                      MessageBoxImage.Exclamation) == MessageBoxResult.OK)
                {
                    string str = File.ReadAllText(Path.Combine(JsonParser.workareaPath, "Workarea.json"));
                    WorkareaModel workareaModel = JsonConvert.DeserializeObject<WorkareaModel>(str);
                    var dialog = new DialogResult(ButtonResult.OK);
                    Workareas workareas = new Workareas();
                    workareas.workareaname = WorkAreaName;
                    workareas.filepath = WorkAreaPath;
                    workareaModel.savepath = WorkAreaPath;
                    workareaModel.workareaname = WorkAreaName;
                    //生成工区默认配置文件
                    File.WriteAllText(Path.Combine(WorkAreaPath, Path.GetFileName(WorkAreaPath) + ".json"), JsonConvert.SerializeObject(workareaModel, Formatting.Indented));
                    dialog.Parameters.Add("param1", workareas);
                    RequestClose?.Invoke( dialog);
                }
            });
        }
        public ICommand CancelCommand
        {
            get => new DelegateCommand(() =>
            {
                RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
            });
        }

    }
}
