using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace MultiLayout
{
    /// <summary>
    /// 
    /// </summary>
    public class FlyLayout : INotifyPropertyChanged
    {
        public List<GageTabItem> GageTabItems { get; set; }


        private int dynareawidth = 250;
        /// <summary>
        /// 动态区宽度
        /// </summary>
        public int DynAreaWidth
        {
            get
            {
                return dynareawidth;
            }
            set
            {
                if (value < 100)
                    value = 100;
                if (dynareawidth != value)
                {
                    dynareawidth = value;
                }
            }
        }

        /// <summary>
        /// 动态区显示
        /// </summary>
        public bool IsDynAreaVisible { get; set; } = true;

        /// <summary>
        /// 隐藏 logo
        /// </summary>
        public bool IsLogoHidden { get; set; }


        #region 上一次界面状态

        public System.Drawing.Rectangle Rect { get; set; } = new System.Drawing.Rectangle(0, 0, 1024, 768);
        public WindowState WindowState { get; set; } = WindowState.Maximized;
        #endregion

        /// <summary>
        /// 基本路径，全部参数都应该放在这个文件夹内
        /// </summary>
        public const string BasePath = "layout";
        /// <summary>
        /// 
        /// </summary>
        public FlyLayout()
        {
        }

        /// <summary>
        /// 加载
        /// </summary>
        public void Load()
        {
            string path = System.IO.Path.Combine(BasePath, "graphcustom.json");
            if (!File.Exists(path))
                return;
            try
            {
                string json = File.ReadAllText(path);

                Newtonsoft.Json.JsonConvert.PopulateObject(json, this);
            }
            catch 
            {

            }

            if (GageTabItems == null)
                GageTabItems = new List<GageTabItem>();
            foreach (var gageTabItem in GageTabItems)
            {
                if (gageTabItem.Components == null)
                {
                    gageTabItem.Components = new List<FlyComponent>();
                }

                if (gageTabItem.DynAreaItems == null) 
                {
                    gageTabItem.DynAreaItems = new List<int>();
                }

                if (gageTabItem.MenuItems == null)
                {
                    gageTabItem.MenuItems = new List<int>();
                }

                if (gageTabItem.Items == null)
                {
                    gageTabItem.Items = new List<GraphTabItem>();
                }

                foreach (var item in gageTabItem.Items) 
                {
                    if (item.Graphs == null)
                    {
                        item.Graphs = new List<GraphComponent>();
                    }
                }
            }
        }
        /// <summary>
        /// 保存
        /// </summary>
        public void Save()
        {
            try
            {
                if (!System.IO.Directory.Exists(BasePath))
                    System.IO.Directory.CreateDirectory(BasePath);

                string path = System.IO.Path.Combine(FlyLayout.BasePath, "graphcustom.json");

                string json = Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
                File.WriteAllText(path, json);
            }
            catch 
            {
            
            }
        }


        /// <summary>
        /// 
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;
    }
    
    /// <summary>
    /// 控件基类
    /// </summary>
    public class FlyComponent : INotifyPropertyChanged
    {
        /// <summary>
        /// 全局唯一ID
        /// </summary>
        public int ID { get; set; }

        /// <summary>
        /// 模块的类名全称
        /// </summary>
        public string Module { get; set; }

        /// <summary>
        /// 容器名称
        /// </summary>
        public string ServiceContainerName { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        public override string ToString()
        {
            return Module;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public class GraphComponent : INotifyPropertyChanged
    {
        /// <summary>
        /// 编号
        /// </summary>
        public int ID { get; set; }
        public System.Drawing.Rectangle Rect { get; set; } = new System.Drawing.Rectangle(0, 0, 1, 1);

        public event PropertyChangedEventHandler PropertyChanged;
    }

    /// <summary>
    /// 图表区 tabitem
    /// </summary>
    public class GraphTabItem:INotifyPropertyChanged
    {

        /// <summary>
        /// 图表
        /// </summary>
        public List<GraphComponent> Graphs { get; set; } = new List<GraphComponent>();
        /// <summary>
        /// 列数量
        /// </summary>
        public int ColumnCount { get; set; } = 1;
        /// <summary>
        /// 行数量
        /// </summary>
        public int RowCount { get; set; } = 3;
        /// <summary>
        /// 每行定义, 当为auto时，为true
        /// </summary>
        public List<bool> RowDefinitions { get; set; } = new List<bool>();
        /// <summary>
        /// 每列定义, 当为auto时，为true
        /// </summary>
        public List<bool> ColumnDefinitions { get; set; } = new List<bool>();

        /// <summary>
        /// 标题
        /// </summary>
        public string Header { get; set; } = "Test";

        public event PropertyChangedEventHandler PropertyChanged;
    }

    /// <summary>
    /// 设备页面
    /// </summary>
    public class GageTabItem:INotifyPropertyChanged
    {
        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 容器名称
        /// </summary>
        public string ServiceContainerName { get; set; }
        /// <summary>
        /// 设置目录 图标
        /// </summary>
        public List<int> MenuItems { get; set; } = new List<int>();
        /// <summary>
        /// 动态区 控件组
        /// </summary>
        public List<int> DynAreaItems { get; set; } = new List<int>();
        /// <summary>
        /// 图表区，tabitem
        /// </summary>
        public List<GraphTabItem> Items { get; set; } = new List<GraphTabItem>();

        /// <summary>
        /// 根据配置 最后生成的 TabControl
        /// </summary>
        [JsonIgnore]
        public TabControl TabControl;
        /// <summary>
        /// 全部组件
        /// </summary>
        public List<FlyComponent> Components { get; set; } = new List<FlyComponent>();

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
