using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace FLY.Thick.Base.UI.UiModule
{
    /// <summary>
    /// Window_graphfix.xaml 的交互逻辑
    /// </summary>
    public partial class FixGraphConfig : FLY.ControlLibrary.WindowBigClose
    {
        private ObservableCollection<FixInterval> intervalopt = new ObservableCollection<FixInterval>();
        public ObservableCollection<FixInterval> IntervalOpt
        {
            get{
                return intervalopt;
            }
        }
        private ObservableCollection<int> datelenopt = new ObservableCollection<int>();
        public ObservableCollection<int> DataLenOpt
        {
            get{
                return datelenopt;
            }
        }

        FixGraphItemParam mGraphParam;
        FixGraphItemParam graphparam_clone = new FixGraphItemParam();
        public FixGraphConfig()
        {
            InitializeComponent();
            this.grid_null.DataContext = this;
        }
        public void Init(FixGraphItemParam graphparam) 
        {
            mGraphParam = graphparam;
            mGraphParam.CopyTo(graphparam_clone);
            updateIntervalOpt();
            updateDataLenOpt();
            this.DataContext = graphparam_clone;
            this.graphparam_clone.PropertyChanged += new PropertyChangedEventHandler(graphparam_clone_PropertyChanged);
        }

        void graphparam_clone_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(graphparam_clone.Interval)) 
            {
                updateDataLenOpt();

                //刷新一下，不然会出错
                int len = graphparam_clone.Len;
                graphparam_clone.Len = 10;
                graphparam_clone.Len = len;
            }
            else if(e.PropertyName == nameof(graphparam_clone.IsADMode))
            {
                graphparam_clone.IsAutoTarget = true;
                graphparam_clone.IsAutoY = false;
                if (graphparam_clone.IsADMode)
                    graphparam_clone.YRange = 1000;
                else
                    graphparam_clone.YRange = 5;
            }
        }

        void updateIntervalOpt() 
        {
            IntervalOpt.Clear();
            IntervalOpt.Add(new FixInterval() { Interval = 1, Title = "1ms" });
            IntervalOpt.Add(new FixInterval() { Interval = 5, Title = "5ms" });
            IntervalOpt.Add(new FixInterval() { Interval = 10, Title = "10ms" });
            IntervalOpt.Add(new FixInterval() { Interval = 50, Title = "50ms" });
            IntervalOpt.Add(new FixInterval() { Interval = 100, Title = "0.1s" });
            IntervalOpt.Add(new FixInterval() { Interval = 1000, Title = "1s" });

            //确保间隔在 intervalopt 内
            if((from i in IntervalOpt where i.Interval == graphparam_clone.Interval select i).Count()==0)
            {
                graphparam_clone.Interval = IntervalOpt.First().Interval;
            }
        }

        void updateDataLenOpt()
        {
            DataLenOpt.Clear();

            if (graphparam_clone.Interval == 1)
            {
                DataLenOpt.Add(5000);//5s
                DataLenOpt.Add(10000);//10s
                DataLenOpt.Add(20000);//20s
                DataLenOpt.Add(50000);//50s
                DataLenOpt.Add(100000);//100s
            }
            else if (graphparam_clone.Interval <= 5)
            {
                DataLenOpt.Add(1000);//5s
                DataLenOpt.Add(2000);//10s
                DataLenOpt.Add(5000);//25s
                DataLenOpt.Add(10000);//50s
                DataLenOpt.Add(20000);//100s
                DataLenOpt.Add(50000);//250s
            }
            else if (graphparam_clone.Interval <= 10)
            {
                DataLenOpt.Add(500);//5s
                DataLenOpt.Add(1000);//10s
                DataLenOpt.Add(2000);//20s
                DataLenOpt.Add(5000);//50s
                DataLenOpt.Add(10000);//100s
                DataLenOpt.Add(20000);//200s
                DataLenOpt.Add(50000);//500s
            }
            else if (graphparam_clone.Interval <= 50)
            {
                DataLenOpt.Add(500);//25s
                DataLenOpt.Add(1000);//50s
                DataLenOpt.Add(2000);//100s
                DataLenOpt.Add(5000);//250s
                DataLenOpt.Add(10000);//500s
                DataLenOpt.Add(20000);//16.6min
            }
            else if (graphparam_clone.Interval <= 100)
            {
                DataLenOpt.Add(500);//50s
                DataLenOpt.Add(1000);//100s
                DataLenOpt.Add(2000);//200s
                DataLenOpt.Add(5000);//500s
                DataLenOpt.Add(10000);//16.6min
            }
            else 
            {
                DataLenOpt.Add(50);//50s
                DataLenOpt.Add(100);//100s
                DataLenOpt.Add(200);//200s
                DataLenOpt.Add(500);//500s
                DataLenOpt.Add(1000);//16.6min
            }
            

            //确保长度在 datalenopt 内
            if (!DataLenOpt.Contains(graphparam_clone.Len))
            {
                graphparam_clone.Len = DataLenOpt.First();
            }
        }

        private void button_save_Click(object sender, RoutedEventArgs e)
        {
            if (!WdPassword.Authorize("GraphConfig"))
                return;

            graphparam_clone.CopyTo(mGraphParam);
            FixGraphParams.Current.Save();
            
        }

        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propertyname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyname));
            }
        }
        #endregion
    }
    public class FixInterval
    {
        public int Interval { set; get; }
        public string Title { set; get; }

    }

    /// <summary>
    /// 时间间隔转换 ms 转 string
    /// </summary>
    public class IntMsConverter : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int ms = (int)value;
            if (ms < 10)
                return (1.28 * ms).ToString() + "ms";
            else 
            {
                if (ms >= 1000)
                {
                    return (ms / 1000.0).ToString() + "s";
                }
                else 
                {
                    return ms.ToString() + "ms";
                }
            }
        }

        #endregion


        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 时间长度 转 string
    /// </summary>
    public class TimeLenConverter : IMultiValueConverter
    {
        #region IMultiValueConverter 成员
        /// <summary>
        /// 参数0: int ms ;
        /// 参数1: int len
        /// </summary>
        /// <param name="values"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
           
            if ((values.Length == 2) && (values[0] is int) && (values[1] is int))//必须要检查，不然 界面生成器 会错误，提示转换异常
            {
                int ms = (int)values[0];
                int len = (int)values[1];

                double total_ms;

                total_ms = ms *len;
                TimeSpan ts = TimeSpan.FromTicks((long)(TimeSpan.TicksPerMillisecond * total_ms));
                return ts.TotalSeconds.ToString("F1") + "s";
                //DateTime dt = new DateTime((long)(TimeSpan.TicksPerMillisecond * total_ms));
                //return dt.ToString("HH:mm:ss");
            }
            else
            {
                return "null";
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }

        #endregion
    }


    /// <summary>
    /// 数据量 转 文件大小 MB
    /// </summary>
    public class TimeLen2FileSizeConverter : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int len = (int)value;
            return len;
        }

        #endregion


        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
