using System.Collections.Generic;
using System.Linq;
using System.Windows.Media;
using Caliburn.Micro.Core;
using SciChart.Core.Extensions;

namespace MECF.Framework.UI.Client.CenterViews.Configs.SystemConfig
{
    public enum DataType
    {
        Unknown,
        Int,
        Double,
        Bool,
        String,
        Color
    };

    /// <summary>
    /// 配置界面树状节点
    /// </summary>
    public class ConfigNode : PropertyChangedBase
    {
        private string name = string.Empty;
        public string Name
        {
            get => name;
            set { name = value; NotifyOfPropertyChange("Name"); }
        }

        private string path = string.Empty;
        public string Path
        {
            get => path;
            set { path = value; NotifyOfPropertyChange("Path"); }
        }

        private string _display = string.Empty;
        public string Display
        {
            get => _display;
            set { _display = value; NotifyOfPropertyChange("Display"); }
        }

        private bool _isExpanded;
        public bool IsExpanded
        {
            get => _isExpanded;
            set { _isExpanded = value; NotifyOfPropertyChange("IsExpanded"); }
        }

        private bool _isMatch;
        public bool IsMatch
        {
            get => _isMatch;
            set { _isMatch = value; NotifyOfPropertyChange("IsMatch"); }
        }

        private bool _isHighLight;
        public bool IsHighLight
        {
            get => _isHighLight;
            set { _isHighLight = value; NotifyOfPropertyChange("IsHighLight"); }
        }

        private List<ConfigNode> _subNodes = null;
        public List<ConfigNode> SubNodes
        {
            get => _subNodes;
            set { _subNodes = value; NotifyOfPropertyChange("SubNodes"); }
        }

        private List<ConfigItem> _items = null;
        public List<ConfigItem> Items
        {
            get => _items;
            set { _items = value; NotifyOfPropertyChange("Items"); }
        }



        private bool IsCriteriaMatched(string criteria)
        {
            bool matched =  string.IsNullOrEmpty(criteria) || name.Contains(criteria) || name.ToLower().Contains(criteria.ToLower());

            if (matched)
                return true;

            foreach (var configItem in Items)
            {
                if (configItem.Name.Contains(criteria))
                    return true;

                if (configItem.Name.ToLower().Contains(criteria.ToLower()))
                    return true;
            }

            return false;
        }

        private void CheckChildren(string criteria, ConfigNode parent)
        {
            foreach (var child in parent.SubNodes)
            {
                if ( !child.IsCriteriaMatched(criteria))
                {
                    child.IsMatch = false;
                }
                CheckChildren(criteria, child);
            }
        }

        public void ApplyCriteria(string criteria, Stack<ConfigNode> ancestors)
        {
            if (IsCriteriaMatched(criteria))
            {
                IsMatch = true;
                foreach (var ancestor in ancestors)
                {
                    ancestor.IsMatch = true;
                    ancestor.IsExpanded = !string.IsNullOrEmpty(criteria);
                    //CheckChildren(criteria, ancestor);
                }
                IsExpanded = true;

            }
            else
                IsMatch = false;

            ancestors.Push(this);
            foreach (var child in SubNodes)
                child.ApplyCriteria(criteria, ancestors);

            ancestors.Pop();
        }
    }

    /// <summary>
    /// 配置界面节点参数
    /// </summary>
    public class ConfigItem : PropertyChangedBase
    {
        private string _name = string.Empty;
        public string Name
        {
            get => _name;
            set { _name = value; NotifyOfPropertyChange(); }
        }

        private string _oldValue = string.Empty;
        /// <summary>
        /// 储存变更前的最原始值
        /// </summary>
        public string OldValue
        {
            get => _oldValue;
            set { _oldValue = value; NotifyOfPropertyChange(); }
        }

        private string _description = string.Empty;
        public string Description
        {
            get => _description;
            set { _description = value; NotifyOfPropertyChange(); }
        }

        private string _display = string.Empty;
        public string Display
        {
            get => _display;
            set { _display = value; NotifyOfPropertyChange(); }
        }

        private DataType _type = DataType.Unknown;
        public DataType Type
        {
            get => _type;
            set { _type = value; NotifyOfPropertyChange(); }
        }

        private string _defaultValue = string.Empty;
        public string DefaultValue
        {
            get => _defaultValue;
            set { _defaultValue = value; NotifyOfPropertyChange(); }
        }

        private double _max = double.NaN;
        public double Max
        {
            get => _max;
            set { _max = value; NotifyOfPropertyChange(); }
        }

        private double _min = double.NaN;
        public double Min
        {
            get => _min;
            set { _min = value; NotifyOfPropertyChange(); }
        }

        private string _parameter = string.Empty;
        public string Parameter
        {
            get => _parameter;
            set { _parameter = value; NotifyOfPropertyChange(); }
        }

        private string _tag = string.Empty;
        public string Tag
        {
            get => _tag;
            set { _tag = value; NotifyOfPropertyChange(); }
        }

        private string _unit = string.Empty;
        public string Unit
        {
            get => _unit;
            set { _unit = value; NotifyOfPropertyChange(); }
        }

        private bool _visible = true;
        public bool Visible
        {
            get => _visible;
            set { _visible = value; NotifyOfPropertyChange(); }
        }

        private bool _isExpanded ;
        public bool IsExpanded
        {
            get => _isExpanded;
            set { _isExpanded = value; NotifyOfPropertyChange(); }
        }

 
        private string _cvalue = string.Empty;
        public string CurrentValue
        {
            get => _cvalue;
            set { _cvalue = value; NotifyOfPropertyChange(); }
        }
 
        private bool _bvalue = false;
        public bool BoolValue
        {
            get => _bvalue;
            set { _bvalue = value; NotifyOfPropertyChange(); }
        }

        private string _sValue = string.Empty;
        public string StringValue
        {
            get => _sValue;
            set { _sValue = value; NotifyOfPropertyChange(); }
        }
        
        private Color _colorValue = Colors.Transparent;

        public Color ColorValue
        {
            get => _colorValue;
            set { _colorValue = value; NotifyOfPropertyChange(); }
        }

        private bool _textSaved = true;
        public bool TextSaved
        {
            get => _textSaved;
            set { _textSaved = value; NotifyOfPropertyChange(); }
        }

        public List<string> Options => Parameter.IsNullOrEmpty() ? null : Parameter.Split(';').ToList();
    }
}
