using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows;
using System.ComponentModel;
using System.Windows.Input;
using Aitex.UI.RecipeEditor.View;
namespace Aitex.UI.RecipeEditor
{
    /// 
    /// cell type enum
    /// 
    public enum CellType
    {
        ReadOnly,
        TextInput,
        EditableSelection,
        TimeInput,
        ReadOnlySelection,
        NumInput,
        CheckBox,
        EndPointSetting,
    }
    /// 
    /// reicpe variable definition
    /// 
    public class RecipeVariableDefine
    {
        public CellType CellType { get; set; }
        public string CatalogName { get; set; }
        public string GroupName { get; set; }
        public string FriendlyName { get; set; }
        public string TechnicalName { get; set; }
        public string DeviceType { get; set; }
        public string Description { get; set; }
        public double MinValue { get; set; }
        public double MaxValue { get; set; }
        public List> DropdownItemList { get; set; }
    }
    /// 
    /// cell data definition
    /// 
    public class SmartCellData : INotifyPropertyChanged
    {
        public RecipeVariableDefine RecipeVariableDefine { get; set; }
        public int RowIndex { get; set; }
        public int ColIndex { get; set; }
        public bool IsRunning { get; set; }
        public bool IsHidden { get; set; }
        public bool IsMasked { get; set; }        
        public Brush Foreground { get; set; }
        public Brush Background { get; set; }
        public FontWeight FontWeight { get; set; }
        public string Value { get; set; }
        public string ErrInfo { get; set; }
        public object Tag { get; set; }
        public string AdditionalInfo { get; set; }
        //public bool IsJump { get; set; }
        //public bool ShowsJumpControl { get; set; }
        public ICommand EndPointCommand { get; set; }
        //public EndPointConfigItem EndPointConfig { get; set; }
        //public SmartCellData()
        //{
        //    EndPointCommand = new DelegatedCommand((o) => true, (o) => EndPointAction(o));
        //    EndPointConfig = new EndPointConfigItem();
        //}
        //private void EndPointAction(object obj)
        //{
        //    EndPointDlg dlg = new EndPointDlg(){ Owner = Application.Current.MainWindow };
        //    EndPointConfig.SetValue(Value);
        //    dlg.ConfigItem = EndPointConfig;
             
        //    var result = dlg.ShowDialog();
        //    if (result.HasValue && result.Value)
        //    {
        //        Value = dlg.ConfigItem.ToValue();
        //    }
 
        //}
        public override string ToString()
        {
            return Display;
        }
        public string ToolTip
        {
            get
            {
                string str = "n/a";
                switch (RecipeVariableDefine.CellType)
                {
                    case CellType.NumInput:
                        str = string.Format("{0}\r\nMax:{1}\r\nValue:{2}", RecipeVariableDefine.FriendlyName, RecipeVariableDefine.MaxValue.ToString("N2").TrimEnd('0').TrimEnd('.').Replace(',', '\''), Display);
                        break;
                    default:
                        str = string.Format("{0}\r\nValue:{1}", RecipeVariableDefine.FriendlyName, string.IsNullOrEmpty(Display) ? "Empty" : Display);
                        break;
                }
                if (!string.IsNullOrEmpty(AdditionalInfo))
                    str += "\r\n" + AdditionalInfo; 
                if (!string.IsNullOrEmpty(ErrInfo))
                   str = str + "\r\n\r\nError:" + ErrInfo;
                return str;
            }
        }
        public string Display
        {
            get
            {
                try
                {
                    if (IsHidden)
                        return "";
                    if (IsMasked)
                        return "*";
                    if (RecipeVariableDefine.CellType == CellType.ReadOnlySelection && RecipeVariableDefine.DropdownItemList != null)
                    {
                        var v = RecipeVariableDefine.DropdownItemList.Find((o) => o.Item1 == Value);
                        if (v != null) return v.Item2;
                    }
                    else if (RecipeVariableDefine.CellType == CellType.CheckBox)
                    {
                        if (Tag != null && Tag is int)
                        {
                            if (bool.Parse(Value))
                                return "【√】 " + Tag.ToString();
                            else
                                return "       " + Tag.ToString();
                        }
                        if (bool.Parse(Value))
                            return "【√】";
                        return "      ";
                    }
                    else if (RecipeVariableDefine.CellType == CellType.EndPointSetting)
                    {
                        if (!string.IsNullOrEmpty(Value) && Value != "n/a")
                        {
                            return "【√】";
                        }
                        return "【...】";
                    }
                    else if (RecipeVariableDefine.CellType == CellType.NumInput)
                    {
                        double d;
                        if (double.TryParse(Value.ToString(), out d))
                        {
                            return d.ToString("N2").TrimEnd('0').TrimEnd('.').Replace(',', '\'');
                        }
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                }
                return Value;
            }
        }
        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        public void InvokePropertyChanged()
        {
            if (PropertyChanged != null)
            {
                foreach (var pro in this.GetType().GetProperties())
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs(pro.Name));
                }
            }
        }
        public void InvokePropertyChanged(string proName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(proName));
            }
        }
        #endregion
    }
}