// /*****************************************************
// (c)2008-2013 Copy right www.Gboxt.com
// ×÷Õß:bull2
// ¹¤³Ì:CodeRefactor-Gboxt.Common.WpfMvvmBase
// ½¨Á¢:2014-11-24
// ÐÞ¸Ä:2014-11-29
// *****************************************************/

#region ÒýÓÃ

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.Serialization;
using Agebull.Common.Mvvm;

#endregion

namespace Agebull.EntityModel
{
    /// <summary>
    ///     ±íÊ¾Ò»¸öÊ÷µÄ¸ù½Úµã
    /// </summary>
    public sealed class TreeRoot : TreeItemBase
    {
        public static TreeRoot Root;

        private TreeItem _selectItem;

        /// <summary>
        ///     ¹¹Ôì
        /// </summary>
        public TreeRoot()
        {
            Root = this;
        }
        /// <summary>
        /// È«¾ÖÅäÖÃ
        /// </summary>
        public readonly Dictionary<string, object> GlobalConfig = new Dictionary<string, object>();

        /// <summary>
        /// ÕÒ¶ÔÓ¦½Úµã
        /// </summary>
        /// <returns></returns>
        public TreeItem Find(NotificationObject obj)
        {
            return Items.Select(child => child.Find(obj)).FirstOrDefault(item => item != null);
        }


        /// <summary>
        ///     ¹¹Ôì
        /// </summary>
        /// <param name="items">½Úµã</param>
        public TreeRoot(IEnumerable<TreeItem> items)
        {
            if (items == null)
            {
                return;
            }
            foreach (var item in items)
            {
                Items.Add(item);
            }
        }

        #region µ±Ç°Ñ¡ÖÐµÄ½Úµã

        /// <summary>
        ///     µ±Ç°Ñ¡ÖÐµÄ½Úµã
        /// </summary>
        public TreeItem SelectItem
        {
            get => _selectItem;
            set
            {
                if (_selectItem == value)
                {
                    return;
                }
                _selectItem?.ClearCommandList();
                _selectItem = value;
                _selectItem?.CreateCommandList();
                RaisePropertyChanged(() => SelectItem);
                SelectItemChanged?.Invoke(value, EventArgs.Empty);
            }
        }

        public event EventHandler SelectItemChanged;
        #endregion

        #region Ñ¡Ôñ

        /// <summary>
        ///     ×Ó¼¶Ñ¡Ôñ·¢Éú±ä»¯
        /// </summary>
        /// <param name="select">ÊÇ·ñÑ¡ÖÐ</param>
        /// <param name="child">×Ó¼¶</param>
        /// <param name="selectItem">Ñ¡ÖÐµÄ¶ÔÏó</param>
        protected internal override void OnChildIsSelectChanged(bool select, TreeItemBase child, TreeItemBase selectItem)
        {
            SelectPath = IsSelected ? null : child.SelectPath;
            if (isSelected != select)
            {
                isSelected = select;
                RaisePropertyChanged(() => IsSelected);
            }
            SelectItem = selectItem as TreeItem;
        }

        #endregion

        #region À©Õ¹·½·¨

        private NotificationList<CommandItemBase> _commands;

        private ModelFunctionDictionary<TreeRoot> _modelFunction;

        /// <summary>
        ///     ÒÀÀµ¶ÔÏó×Öµä
        /// </summary>
        [IgnoreDataMember]
        public ModelFunctionDictionary<TreeRoot> ModelFunction
        {
            get => _modelFunction ?? (_modelFunction = new ModelFunctionDictionary<TreeRoot>() );
            set => _modelFunction = value;
        }

        /// <summary>
        ///     ¶ÔÓ¦µÄÃüÁî¼¯ºÏ
        /// </summary>
        public NotificationList<CommandItemBase> Commands
        {
            get => _commands ?? (_commands = new NotificationList<CommandItemBase>() );
            set
            {
                if (_commands == value)
                {
                    return;
                }
                _commands = value;
                RaisePropertyChanged(() => Commands);
            }
        }

        #endregion
        
    }
}
