using MainShell.Common;
using MainShell.Log;
using MainShell.Models;
using MainShell.ParaSetting.Model;
using MaxwellFramework.Core.Interfaces;
using MwFramework.ManagerService;
using System;
using System.IO;
using System.Windows;
using Forms = System.Windows.Forms;

namespace MainShell.ParaSetting.ViewModel
{
    public class SaveSettingViewModel : BaseScreen
    {
        private readonly IParamList _paramList;

        private DeviceFoundationSetting _deviceFoundationSetting;

        public SaveSettingViewModel(IParameterManager parameterManager)
        {
            _paramList = parameterManager as IParamList;
            if (_paramList == null)
            {
                throw new ArgumentNullException(nameof(parameterManager));
            }
        }

        private DeviceSaveSettingItem _saveSettingItem;

        public DeviceSaveSettingItem SaveSettingItem
        {
            get
            {
                return _saveSettingItem;
            }
            set
            {
                SetAndNotify(ref _saveSettingItem, value);
            }
        }

        protected override void OnViewLoaded()
        {
            base.OnViewLoaded();
            _deviceFoundationSetting = _paramList.GetParameter<DeviceFoundationSetting>();
            RefreshState();
        }

        public void BtnSave()
        {
            try
            {
                _deviceFoundationSetting.Write();
                MwMessageBox.Show("保存完成", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            catch (Exception ex)
            {
                LogManager.LogSysError(ex.ToString());
                MwMessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        public void BrowseFileSaveFolder()
        {
            string selectedPath = BrowseFolder(SaveSettingItem == null || SaveSettingItem.FileSaveSetting == null ? null : SaveSettingItem.FileSaveSetting.SaveFolder);
            if (string.IsNullOrWhiteSpace(selectedPath) || SaveSettingItem == null || SaveSettingItem.FileSaveSetting == null)
            {
                return;
            }

            SaveSettingItem.FileSaveSetting.SaveFolder = selectedPath;
        }

        public void BrowseImageSaveFolder()
        {
            string selectedPath = BrowseFolder(SaveSettingItem == null || SaveSettingItem.ImageSaveSetting == null ? null : SaveSettingItem.ImageSaveSetting.SaveFolder);
            if (string.IsNullOrWhiteSpace(selectedPath) || SaveSettingItem == null || SaveSettingItem.ImageSaveSetting == null)
            {
                return;
            }

            SaveSettingItem.ImageSaveSetting.SaveFolder = selectedPath;
        }

        private void RefreshState()
        {
            if (_deviceFoundationSetting.SaveSettingItem == null)
            {
                _deviceFoundationSetting.SaveSettingItem = new DeviceSaveSettingItem();
            }

            SaveSettingItem = _deviceFoundationSetting.SaveSettingItem;
        }

        private static string BrowseFolder(string currentPath)
        {
            using (Forms.FolderBrowserDialog dialog = new Forms.FolderBrowserDialog())
            {
                if (!string.IsNullOrWhiteSpace(currentPath) && Directory.Exists(currentPath))
                {
                    dialog.SelectedPath = currentPath;
                }

                Forms.DialogResult result = dialog.ShowDialog();
                if (result != Forms.DialogResult.OK)
                {
                    return null;
                }

                return dialog.SelectedPath;
            }
        }
    }
}
