using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;

namespace ThickTcpUiInWindow.UIModule
{
    /// <summary>
    /// 
    /// </summary>
    public class FLYLayout : Misc.ISaveToXml, INotifyPropertyChanged
    {
        /// <summary>
        /// 设置目录 图标
        /// </summary>
        public List<MenuComponent> MenuItems { get; } = new List<MenuComponent>();
        /// <summary>
        /// 动态区 控件组
        /// </summary>
        public List<DynAreaComponent> DynAreaItems { get; } = new List<DynAreaComponent>();
        /// <summary>
        /// 图表区，tabitem
        /// </summary>
        public List<GraphTabItem> Items { get; } = new List<GraphTabItem>();


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


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

        /// <summary>
        /// 隐藏 logo
        /// </summary>
        public bool IsLogoHidden { get; set; }
        /// <summary>
        /// 当前选了menu， 这个不需要保存
        /// </summary>
        public bool IsInMenuSelect { get; set; }
        /// <summary>
        /// 测厚仪的状态不同状态，显示的页面
        /// </summary>
        public List<ControllerStateTabItem> ControllerStates { get; } = new List<ControllerStateTabItem>();


        #region 上一次界面状态
        public int Left { get; set; }
        public int Top { get; set; }
        public int Width { get; set; } = 1024;
        public int Height { get; set; } = 768;
        public WindowState WindowState { get; set; } = WindowState.Maximized;
        #endregion
        /// <summary>
        /// 文件路径
        /// </summary>
        public string Path;
        static FLYLayout()
        {
            Misc.SaveToXmlHepler.Regist(typeof(GraphTabItem));
            Misc.SaveToXmlHepler.Regist(typeof(MenuComponent));
            Misc.SaveToXmlHepler.Regist(typeof(DynAreaComponent));
            Misc.SaveToXmlHepler.Regist(typeof(ControllerStateTabItem));

            if (!System.IO.Directory.Exists(BasePath))
            {
                System.IO.Directory.CreateDirectory(BasePath);
            }
        }

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

        /// <summary>
        /// 加载
        /// </summary>
        public void Load()
        {
            string path = System.IO.Path.Combine(FLYLayout.BasePath, "graphcustom.xml");
            Misc.SaveToXmlHepler.Load(path, this);
        }
        /// <summary>
        /// 保存
        /// </summary>
        public void Save()
        {
            string path = System.IO.Path.Combine(FLYLayout.BasePath, "graphcustom.xml");
            Misc.SaveToXmlHepler.Save(path, this);
        }


        #region ISaveToXml 成员
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public virtual string[] GetSavePropertyNames()
        {
            return new string[]{
                "Items",
                "ControllerStates",
                "DynAreaItems",
                "DynAreaWidth","IsDynAreaVisible",
                "Left","Top","Width","Height","WindowState",
                "IsLogoHidden"
            };
        }

        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="propertyName"></param>
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;
    }

    public class ControllerStateTabItem
    {
        public string ControllerState;
        public int No;
    }
    /// <summary>
    /// 
    /// </summary>
    public abstract class FLYComponent : Misc.ISaveToXml, INotifyPropertyChanged
    {

        /// <summary>
        /// 模块
        /// </summary>
        public string Module { get; set; }


        /// <summary>
        /// 全局唯一ID
        /// </summary>
        public int ID { get; set; }

        #region ISaveToXml 成员
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public virtual string[] GetSavePropertyNames()
        {
            return new string[]{
                "ID",
                "Module"
            };
        }

        #endregion

        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
    /// <summary>
    /// 动态区控件
    /// </summary>
    public class DynAreaComponent : FLYComponent
    {
        static DynAreaComponent()
        {
            Misc.SaveToXmlHepler.Regist(typeof(DynAreaComponent));
        }
        public DynAreaComponent()
        {

        }
    }

    /// <summary>
    /// 设置目录图标
    /// </summary>
    public class MenuComponent : FLYComponent
    {
        static MenuComponent()
        {
            Misc.SaveToXmlHepler.Regist(typeof(MenuComponent));
        }
        public MenuComponent()
        {

        }
    }
    /// <summary>
    /// 
    /// </summary>
    public class GraphComponent : FLYComponent
    {
        /// <summary>
        /// 列
        /// </summary>
        public int Column { get; set; } = 0;
        /// <summary>
        /// 行
        /// </summary>
        public int Row { get; set; } = 0;

        /// <summary>
        /// 跨列
        /// </summary>
        public int ColumnSpan { get; set; } = 1;

        /// <summary>
        /// 跨行
        /// </summary>
        public int RowSpan { get; set; } = 1;

        static GraphComponent()
        {
            Misc.SaveToXmlHepler.Regist(typeof(GraphComponent));
        }
        public GraphComponent()
        {

        }

        #region ISaveToXml 成员
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string[] GetSavePropertyNames()
        {
            List<string> names = new List<string>();
            names.AddRange(base.GetSavePropertyNames());
            names.AddRange(new string[] {
                "Row","Column","RowSpan","ColumnSpan"
            });
            return names.ToArray();
        }

        #endregion
    }

    /// <summary>
    /// 图表区 tabitem
    /// </summary>
    public class GraphTabItem : Misc.ISaveToXml, INotifyPropertyChanged
    {
        /// <summary>
        /// 图表
        /// </summary>
        public List<GraphComponent> Graphs { get; } = 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; } = new List<bool>();
        /// <summary>
        /// 每列定义, 当为auto时，为true
        /// </summary>
        public List<bool> ColumnDefinitions { get; } = new List<bool>();
        /// <summary>
        /// 标题
        /// </summary>
        public string Header { get; set; } = "Test";

        static GraphTabItem()
        {
            Misc.SaveToXmlHepler.Regist(typeof(GraphTabItem));
            Misc.SaveToXmlHepler.Regist(typeof(GraphComponent));
        }

        #region ISaveToXml 成员

        public string[] GetSavePropertyNames()
        {
            return new string[]{
                "Header",
                "ColumnCount",
                "RowCount",
                "ColumnDefinitions",
                "RowDefinitions",
                "Graphs"
            };
        }

        #endregion

        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
