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

namespace zComp.Core
{
    public class zSelectionItemBase : INotifyPropertyChanged
    {
        private bool _isSelected = false;

        /// <summary>Propriété de sélection</summary>
        public bool IsSelected
        {
            get
            {
                return _isSelected;
            }
            set
            {
                _isSelected = value;
                OnPropertyChanged(zComp.Core.Helpers.ExpressionHelper.GetPropertyName<zSelectionItemBase>(el => el.IsSelected));
                OnSelectionChanged();
            }
        }

        /// <summary>Libellé</summary>
        private string _label = null;


        /// <summary>Constructeur</summary>
        public zSelectionItemBase()
        {
        }

        /// <summary></summary>
        /// <returns></returns>
        public override string ToString()
        {
            return _label == null ? (ItemBase == null ? string.Empty : ItemBase.ToString()) : _label;
        }

        /// <summary>Libellé de l'élément (possibilité de le forcer)</summary>
        public virtual string Label
        {
            get
            {
                return this.ToString();
            }
            set
            {
                _label = value;
            }
        }

        /// <summary>Item à sélectionner</summary>
        public object ItemBase { get; set; }

        protected void OnSelectionChanged()
        {
            if (SelectionChanged != null)
                SelectionChanged(this, new EventArgs());
        }

        public event EventHandler SelectionChanged;

        #region Interface INotifyPropertyChanged

        protected void OnPropertyChanged(string properyName) 
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(properyName));
        }

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }

    /// <summary>Ajoute une propriété de sélection booléenne à un item</summary>
    /// <typeparam name="TItem">Type d'item</typeparam>
    public class zSelectionItem<TItem> : zSelectionItemBase
    {
        /// <summary>Constructeur</summary>
        public zSelectionItem()
            : base()
        {
        }

        /// <summary>Constructeur</summary>
        /// <param name="item">Item</param>
        public zSelectionItem(TItem item)
            : base()
        {
            ItemBase = item;
        }

        /// <summary>Constructeur</summary>
        /// <param name="item">Item</param>
        /// <param name="selected">Valeur de sélection</param>
        public zSelectionItem(TItem item, bool selected)
            : base()
        {
            ItemBase = item;
            IsSelected = selected;
        }

        public TItem ItemTyped 
        { 
            get
            {
                return (TItem)ItemBase;
            }
            set
            {
                ItemBase = value;
            }
        }
    }
}
