using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MeterVision.Config
{
    //配置项
    public class CfginiItem : INotifyPropertyChanged
    {
        public const string SECTION_FILE_PATH = "file_path";
        public const string SECTION_PAGE_SIZE = "page_size";
        public const string SECTION_DIALOG_SIZE = "dialog_size";
        public const string SECTION_LOG_VIEWER = "log_viewer";

        public const string KEY_FILE_PATH_ONNX = "onnx";
        public const string KEY_FILE_PATH_AIDLL = "ai_dll";
        public const string KEY_FILE_PATH_OUTPUT = "output";

        public const string KEY_PAGE_SIZE_SINGLE_DETAIL = "single_detail";
        public const string KEY_PAGE_SIZE_STAND_DETAIL = "stand_detail";
        public const string KEY_PAGE_SIZE_PATCH_DETAIL = "patch_detail";
        public const string KEY_PAGE_SIZE_STAND_CATALOG = "stand_catalog";
        public const string KEY_PAGE_SIZE_PATCH_CATALOG = "patch_catalog";

        //改成三种翻页的配置,1,2,3(1.最左侧，2中间，3右侧数据)
        public const string KEY_PAGE_SIZE_1 = "page_size_1";
        public const string KEY_PAGE_SIZE_2 = "page_size_2";
        public const string KEY_PAGE_SIZE_3 = "page_size_3";


        public const string KEY_DIALOG_SIZE_IMAGE = "dialog_image_size";
        public const string KEY_DIALOG_SIZE_STAND = "dialog_stand_size";

        //public const string KEY_LOGV_MAXIMIZED = "maximized";
        public const string KEY_LOGV_POSITION = "position";
        public const string KEY_LOGV_LEFT_WIDTH = "left_width";
        public const string KEY_LOGV_SIZE = "size";


        private static CfginiItem _cfginiItem;

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public event Action<int> OnStandPageSizeChanged;
        public event Action<int> OnSinglePageSizeChanged;
        public event Action<int> OnPatchPageSizeChanged;

        public event Action<int> OnStandCatalogPageSizeChanged;
        public event Action<int> OnPatchCatalogPageSizeChnaged;

        public event Action<int> OnPageSize1Changed;
        public event Action<int> OnPageSize2Changed;
        public event Action<int> OnPageSize3Changed;


        private string _onnxPath;
        public string OnnxPath
        {
            get => _onnxPath;
            set
            {
                if(_onnxPath != value)
                {
                    _onnxPath = value;
                    OnPropertyChanged(nameof(OnnxPath));
                    SaveOnnxPath(value);
                }
            }
        }

        private string _aiDll;
        public string AiDll
        {
            get => _aiDll;
            set
            {
                if(_aiDll != value)
                {
                    _aiDll = value;
                    OnPropertyChanged(nameof(AiDll));
                    SaveAiDll(value);
                }
            }
        }

        private string _output;
        public string Output
        {
            get => _output;
            set
            {
                if(_output != value)
                {
                    _output = value;
                    OnPropertyChanged(nameof(Output));
                    SaveOutput(value);
                }
            }
        }

        public string DstImgPath
        {
            get
            {
                return Path.Combine(Output, "dst_img");
            }
        }

        public string TransImgPath
        {
            get
            {
                return Path.Combine(Output, "trans_img");
            }
        }

        public string AiLogPath
        {
            get
            {
                return Path.Combine(Output, "ai_log");
            }
        }

        public string RealLogPath => Path.Combine(Output, "real_log");

        private int _singlePageSize;
        public int SinglePageSize
        {
            get => _singlePageSize;
            set
            {
                if (_singlePageSize != value)
                {
                    _singlePageSize = value;
                    OnPropertyChanged(nameof(SinglePageSize));
                    OnSinglePageSizeChanged?.Invoke(value);
                    SaveSinglePageSize(value);
                }
            }
        }

        private int _standPageSize;
        public int StandPageSize
        {
            get => _standPageSize;
            set
            {
                if (_standPageSize != value)
                {
                    _standPageSize = value;
                    OnPropertyChanged(nameof(StandPageSize));
                    OnStandPageSizeChanged?.Invoke(value);
                    SaveStandPageSize(value);
                }
            }
        }

        private int _patchPageSize;
        public int PatchPageSize
        {
            get => _patchPageSize;
            set
            {
                if (_patchPageSize != value)
                {
                    _patchPageSize = value;
                    OnPropertyChanged(nameof(PatchPageSize));
                    OnPatchPageSizeChanged?.Invoke(value);
                    SavePatchPageSize(value);
                }
            }
        }

        private int _standCatalogPageSize;
        public int StandCatalogPageSize
        {
            get => _standCatalogPageSize;
            set
            {
                if (_standCatalogPageSize != value)
                {
                    _standCatalogPageSize = value;
                    OnPropertyChanged(nameof(StandCatalogPageSize));
                    OnStandCatalogPageSizeChanged?.Invoke(value);
                    SaveStandCatlogPageSize(value);
                }
            }
        }

        private int _patchCatalogPageSize;
        public int PatchCatalogPageSize
        {
            get => _patchCatalogPageSize;
            set
            {
                if (_patchCatalogPageSize != value)
                {
                    _patchCatalogPageSize = value;
                    OnPropertyChanged(nameof(PatchCatalogPageSize));
                    OnPatchCatalogPageSizeChnaged?.Invoke(value);
                    SavePatchCatalogPageSize(value);
                }
            }
        }

        private int _pageSize1;
        public int PageSize1
        {
            get => _pageSize1;
            set
            {
                if (_pageSize1 != value)
                {
                    _pageSize1 = value;
                    OnPropertyChanged(nameof(PageSize1));
                    OnPageSize1Changed?.Invoke(value);
                    SavePageSize1(value);
                }
            }
        }

        private int _pageSize2;
        public int PageSize2
        {
            get => _pageSize2;
            set
            {
                if (_pageSize2 != value)
                {
                    _pageSize2 = value;
                    OnPropertyChanged(nameof(PageSize2));
                    OnPageSize2Changed?.Invoke(value);
                    SavePageSize2(value);
                }
            }
        }

        private int _pageSize3;
        public int PageSize3
        {
            get => _pageSize3;
            set
            {
                if (_pageSize3 != value)
                {
                    _pageSize3 = value;
                    OnPropertyChanged(nameof(PageSize3));
                    OnPageSize3Changed?.Invoke(value);
                    SavePageSize3(value);
                }
            }
        }



        private string _dialogImageSize;
        public string DialogImageSize
        {
            get => _dialogImageSize;
            set
            {
                if(_dialogImageSize != value)
                {
                    _dialogImageSize = value;
                    OnPropertyChanged(nameof(DialogImageSize));
                    SaveDialogImageSize(value);
                }
            }
        }

        public int DialogImageSize_Width
        {
            get
            {
                string[] dlgSizes = DialogImageSize.Split(',');
                if(dlgSizes.Length != 2)
                {
                    return 0;
                }
                return int.TryParse(dlgSizes[0], out int result) ? result : 0;
            }
        }

        public int DialogImageSize_Height
        {
            get
            {
                string[] dlgSizes = DialogImageSize.Split(',');
                if (dlgSizes.Length != 2)
                {
                    return 0;
                }
                return int.TryParse(dlgSizes[1], out int result) ? result : 0;
            }
        }


        private string _dialogStandSize;
        public string DialogStandSize
        {
            get => _dialogStandSize;
            set
            {
                if (_dialogStandSize != value)
                {
                    _dialogStandSize = value;
                    OnPropertyChanged(nameof(DialogStandSize));
                    SaveDialogStandSize(value);
                }
            }
        }

        public int DialogStandSize_Width
        {
            get
            {
                string[] dlgSizes = DialogStandSize.Split(',');
                if (dlgSizes.Length != 2)
                {
                    return 0;
                }
                return int.TryParse(dlgSizes[0], out int result) ? result : 0;
            }
        }

        public int DialogStandSize_Height
        {
            get
            {
                string[] dlgSizes = DialogStandSize.Split(',');
                if (dlgSizes.Length != 2)
                {
                    return 0;
                }
                return int.TryParse(dlgSizes[1], out int result) ? result : 0;
            }
        }


        private string _logvSize;
        public string LogvSize
        {
            get => _logvSize;
            set
            {
                if(_logvSize != value)
                {
                    _logvSize = value;
                    OnPropertyChanged(nameof(LogvSize));
                    SaveLogvSize(value);
                }
            }
        }

        public int LogvSize_Width
        {
            get
            {
                string[] dlgSizes = LogvSize.Split(',');
                if (dlgSizes.Length != 2)
                {
                    return 0;
                }
                return int.TryParse(dlgSizes[0], out int result) ? result : 0;
            }
        }

        public int LogvSize_Heigth
        {
            get
            {
                string[] dlgSizes = LogvSize.Split(',');
                if (dlgSizes.Length != 2)
                {
                    return 0;
                }
                return int.TryParse(dlgSizes[1], out int result) ? result : 0;
            }
        }

        private int _logvLeftWidth;
        public int LogvLeftWidth
        {
            get => _logvLeftWidth;
            set
            {
                if(_logvLeftWidth != value)
                {
                    _logvLeftWidth = value;
                    OnPropertyChanged(nameof(LogvLeftWidth));
                    SaveLogvLeftWidth(value);
                }
            }
        }


        //配置项
        public CfginiItem()
        {

        }

        //初始化各项
        public void InitItem()
        {
            _onnxPath = ConfigIni.ReadIniValue(SECTION_FILE_PATH, "", KEY_FILE_PATH_ONNX);
            _aiDll = ConfigIni.ReadIniValue(SECTION_FILE_PATH, "", KEY_FILE_PATH_AIDLL);
            _output = ConfigIni.ReadIniValue(SECTION_FILE_PATH, "", KEY_FILE_PATH_OUTPUT);

            //_singlePageSize = int.TryParse(ConfigIni.ReadIniValue(SECTION_PAGE_SIZE, KEY_PAGE_SIZE_SINGLE_DETAIL, "20"), out int result1) ? result1 : 20;
            //_standPageSize = int.TryParse(ConfigIni.ReadIniValue(SECTION_PAGE_SIZE, KEY_PAGE_SIZE_STAND_DETAIL, "20"), out int result2) ? result2 : 20;
            //_patchPageSize = int.TryParse(ConfigIni.ReadIniValue(SECTION_PAGE_SIZE, KEY_PAGE_SIZE_PATCH_DETAIL, "20"), out int result3) ? result3 : 20;

            //_standCatalogPageSize = int.TryParse(ConfigIni.ReadIniValue(SECTION_PAGE_SIZE, KEY_PAGE_SIZE_STAND_CATALOG, "20"), out int result4) ? result4 : 10;
            //_patchCatalogPageSize = int.TryParse(ConfigIni.ReadIniValue(SECTION_PAGE_SIZE, KEY_PAGE_SIZE_PATCH_CATALOG, "20"), out int result5) ? result5 : 10;

            _singlePageSize = int.TryParse(ConfigIni.ReadIniValue(SECTION_PAGE_SIZE, "20",KEY_PAGE_SIZE_SINGLE_DETAIL), out int result1) ? result1 : 20;
            _standPageSize = int.TryParse(ConfigIni.ReadIniValue(SECTION_PAGE_SIZE, "20", KEY_PAGE_SIZE_STAND_DETAIL), out int result2) ? result2 : 20;
            _patchPageSize = int.TryParse(ConfigIni.ReadIniValue(SECTION_PAGE_SIZE, "20", KEY_PAGE_SIZE_PATCH_DETAIL), out int result3) ? result3 : 20;

            _standCatalogPageSize = int.TryParse(ConfigIni.ReadIniValue(SECTION_PAGE_SIZE, "10",KEY_PAGE_SIZE_STAND_CATALOG), out int result4) ? result4 : 10;
            _patchCatalogPageSize = int.TryParse(ConfigIni.ReadIniValue(SECTION_PAGE_SIZE, "10",KEY_PAGE_SIZE_PATCH_CATALOG), out int result5) ? result5 : 10;

            //获取数据
            _pageSize1 = int.TryParse(ConfigIni.ReadIniValue(SECTION_PAGE_SIZE, "10", KEY_PAGE_SIZE_1), out int pageSize1) ? pageSize1 : 10;
            _pageSize2 = int.TryParse(ConfigIni.ReadIniValue(SECTION_PAGE_SIZE, "20", KEY_PAGE_SIZE_2), out int pageSize2) ? pageSize2 : 20;
            _pageSize3 = int.TryParse(ConfigIni.ReadIniValue(SECTION_PAGE_SIZE, "50", KEY_PAGE_SIZE_3), out int pageSize3) ? pageSize3 : 50;


            _dialogImageSize = ConfigIni.ReadIniValue(SECTION_DIALOG_SIZE, "", KEY_DIALOG_SIZE_IMAGE);
            _dialogStandSize = ConfigIni.ReadIniValue(SECTION_DIALOG_SIZE, "", KEY_DIALOG_SIZE_STAND);

            _logvSize = ConfigIni.ReadIniValue(SECTION_LOG_VIEWER, "", KEY_LOGV_SIZE);

            _logvLeftWidth = int.TryParse(ConfigIni.ReadIniValue(SECTION_LOG_VIEWER, "0", KEY_LOGV_LEFT_WIDTH), out int result6) ? result6 : 0;
        }

        public static CfginiItem GetConfigItem()
        {
            if(_cfginiItem != null)
            {
                return _cfginiItem;
            }

            _cfginiItem = new CfginiItem();
            _cfginiItem.InitItem();

            return _cfginiItem;
        }



        // 保存 onnx 路径
        public static void SaveOnnxPath(string onnxPath)
        {
            if (onnxPath == null) return;

            ConfigIni.WriteIniValue(SECTION_FILE_PATH, KEY_FILE_PATH_ONNX, onnxPath);
        }

        // 保存 ai_dll 路径
        public static void SaveAiDll(string aiDll)
        {
            if (aiDll == null) return;

            ConfigIni.WriteIniValue(SECTION_FILE_PATH, KEY_FILE_PATH_AIDLL, aiDll);
        }

        // 保存输出路径
        public static void SaveOutput(string outputPath)
        {
            if (outputPath == null) return;

            ConfigIni.WriteIniValue(SECTION_FILE_PATH, KEY_FILE_PATH_OUTPUT, outputPath);
        }

        // 保存 single 页大小
        public static void SaveSinglePageSize(int singlePageSize)
        {
            if (singlePageSize == 0) return;
            ConfigIni.WriteIniValue(SECTION_PAGE_SIZE, KEY_PAGE_SIZE_SINGLE_DETAIL, singlePageSize.ToString());
        }

        // 保存 stand 页大小
        public static void SaveStandPageSize(int standPageSize)
        {
            if (standPageSize == 0) return;
            ConfigIni.WriteIniValue(SECTION_PAGE_SIZE, KEY_PAGE_SIZE_STAND_DETAIL, standPageSize.ToString());
        }

        // 保存 patch 页大小
        public static void SavePatchPageSize(int patchPageSize)
        {
            if (patchPageSize == 0) return;
            ConfigIni.WriteIniValue(SECTION_PAGE_SIZE, KEY_PAGE_SIZE_PATCH_DETAIL, patchPageSize.ToString());
        }

        // 保存 stand_detail 页大小
        public static void SaveStandCatlogPageSize(int standCatalogPageSize)
        {
            if (standCatalogPageSize == 0) return;
            ConfigIni.WriteIniValue(SECTION_PAGE_SIZE, KEY_PAGE_SIZE_STAND_CATALOG, standCatalogPageSize.ToString());
        }

        // 保存 patch_detail 页大小
        public static void SavePatchCatalogPageSize(int patchCatalogPageSize)
        {
            if (patchCatalogPageSize == 0) return;
            ConfigIni.WriteIniValue(SECTION_PAGE_SIZE, KEY_PAGE_SIZE_PATCH_CATALOG, patchCatalogPageSize.ToString());
        }


        public static void SavePageSize1(int pageSize1)
        {
            if (pageSize1 == 0) return;
            ConfigIni.WriteIniValue(SECTION_PAGE_SIZE, KEY_PAGE_SIZE_1, pageSize1.ToString());
        }

        public static void SavePageSize2(int pageSize2)
        {
            if (pageSize2 == 0) return;
            ConfigIni.WriteIniValue(SECTION_PAGE_SIZE, KEY_PAGE_SIZE_2, pageSize2.ToString());
        }

        public static void SavePageSize3(int pageSize3)
        {
            if (pageSize3 == 0) return;
            ConfigIni.WriteIniValue(SECTION_PAGE_SIZE, KEY_PAGE_SIZE_3, pageSize3.ToString());
        }

        // 保存 show_image 窗口大小
        public static void SaveDialogImageSize(string dialogImageSize)
        {
            if (dialogImageSize == null) return;

            ConfigIni.WriteIniValue(SECTION_DIALOG_SIZE, KEY_DIALOG_SIZE_IMAGE, dialogImageSize);
        }

        // 保存 show_stand 窗口大小
        public static void SaveDialogStandSize(string dialogStandSize)
        {
            if (dialogStandSize == null) return;

            ConfigIni.WriteIniValue(SECTION_DIALOG_SIZE, KEY_DIALOG_SIZE_STAND, dialogStandSize);
        }

        public static void SaveLogvSize(string logvSize)
        {
            if (logvSize == null) return;
            ConfigIni.WriteIniValue(SECTION_LOG_VIEWER, KEY_LOGV_SIZE, logvSize);
        }

        public static void SaveLogvLeftWidth(int leftWidth)
        {
            if (leftWidth == 0) return;
            ConfigIni.WriteIniValue(SECTION_LOG_VIEWER, KEY_LOGV_LEFT_WIDTH, leftWidth.ToString());
        }

        //------------------------------------------------------------
    }
}
