using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using SciChart.Charting.Model.DataSeries;
using SciChart.Charting.Visuals.RenderableSeries;
using System.Windows.Input;
using Aitex.Core.Equipment.SusceptorDefine;
using Aitex.Core.UI.MVVM;
using System.Collections.ObjectModel;
using System.Windows;
using Aitex.Core.UI.View.Smart;

namespace Aitex.Core.UI.ControlDataContext
{
    public class ProcessChartDataItem
    {
        public double[] averaged_temperature_on_wafer;
        public double[] averaged_temperature_off_wafer;
        public double[] averaged_reflection_on_wafer;
        public double[] averaged_curvature_on_wafer;
        public double[] reflection_each_wafer;
        public double[] temperature_each_wafer;
        public double[] curvature_each_wafer;
    };

    public class PocketProcessDataChartDataItem : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void InvokePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        //public GridLength DataConfigPanelWidth { get; set; }
        //public bool IsDataConfigPanelVisable
        //{
        //    get
        //    {
        //        return DataConfigPanelWidth.Value > 0;
        //    }
        //    set
        //    {
        //        if (value)
        //            DataConfigPanelWidth = new GridLength(400, GridUnitType.Pixel);
        //        else
        //            DataConfigPanelWidth = new GridLength(0, GridUnitType.Pixel);
        //        InvokePropertyChanged("IsDataConfigPanelVisable");
        //        InvokePropertyChanged("DataConfigPanelWidth");
        //    }
        //}

        public DelegateCommand<object> SeriesSelectAllCommand { get; private set; }
        public DelegateCommand<object> SeriesSelectNoneCommand { get; private set; }
        public DelegateCommand<object> SeriesSelectDefaultCommand { get; private set; }

        public ObservableCollection<IRenderableSeries> RenderableSeries { get; set; }

        private Susceptor _susceptorInfo;
        public Susceptor SusceptorInfo
        {
            get { return _susceptorInfo; }
            set
            {
                UpdateSusceptorInfo(value);             
            }
        }

        //update data by value array
        ProcessChartDataItem _processData;
        Dictionary<string, Tuple<double[], int>> _dataMap = new Dictionary<string, Tuple<double[], int>>();

        //update data by table field

        public string ProcessInfo
        {
            get; 
            set;
        }

        public bool EnableCurvature { get; set; }

        public PocketProcessDataChartDataItem()
        {
            EnableCurvature = true;

            SeriesSelectAllCommand = new DelegateCommand<object>(new Action<object>(OnSeriesSelectAll), null);
            SeriesSelectNoneCommand = new DelegateCommand<object>(new Action<object>(OnSeriesSelectNone), null);
            SeriesSelectDefaultCommand = new DelegateCommand<object>(new Action<object>(OnSeriesSelectDefault), null);

            RenderableSeries = new ObservableCollection<IRenderableSeries>();

            _processData = new ProcessChartDataItem()
            {
                averaged_temperature_on_wafer = new double[5],
                averaged_temperature_off_wafer = new double[5],
                averaged_reflection_on_wafer = new double[5],
                averaged_curvature_on_wafer = new double[5],
                reflection_each_wafer = new double[200],
                temperature_each_wafer = new double[200],
                curvature_each_wafer = new double[200]
            };

            _susceptorInfo = new Susceptor();

            ProcessInfo = "";
        }

        public void UpdateData(ProcessChartDataItem data)
        {
            if (data == null)
                return;

            data.averaged_temperature_on_wafer.CopyTo(_processData.averaged_temperature_on_wafer, 0);
            data.averaged_temperature_off_wafer.CopyTo(_processData.averaged_temperature_off_wafer, 0);
            data.averaged_reflection_on_wafer.CopyTo(_processData.averaged_reflection_on_wafer, 0);
            data.averaged_curvature_on_wafer.CopyTo(_processData.averaged_curvature_on_wafer, 0);
            data.reflection_each_wafer.CopyTo(_processData.reflection_each_wafer, 0);
            data.temperature_each_wafer.CopyTo(_processData.temperature_each_wafer, 0);
            data.curvature_each_wafer.CopyTo(_processData.curvature_each_wafer, 0);

                foreach (var item in RenderableSeries)
                {
                    var series = item as SmartDataLine;
                    if (series == null)
                        continue;
                    var dataSeries = series.DataSeries as XyDataSeries<DateTime, float>;

                    dataSeries.Append(DateTime.Now, (float)(_dataMap[series.DisplayName].Item1[_dataMap[series.DisplayName].Item2]));
                }
        }

        public void ClearData()
        {
            foreach (var item in RenderableSeries)
            {
                var series = item as SmartDataLine;
                if (series == null)
                    continue;
                var dataSeries = series.DataSeries as XyDataSeries<DateTime, float>;

                dataSeries.Clear();
            }
        }


        void UpdateSusceptorInfo(Susceptor susceptor)
        {
            if (susceptor.Type == _susceptorInfo.Type)
                return;

            _susceptorInfo = susceptor;

            RenderableSeries.Clear();

            _dataMap.Clear();


            //pyro
            string[] zone = _susceptorInfo.GetZoneNameList();
            int waferZoneIndex = 0;
            string susceptorType = "2Inch9"; 

            for (int i = 0; i < zone.Length; i++)
            {
                string seriesName = string.Format("温度_{0}区_Wafer温度", zone[i]);
                RenderableSeries.Add(new SmartDataLine(seriesName, System.Windows.Media.Colors.Red, string.Format("ReactorA.Smart200.WaferTempAvg{0}", zone[i]), true) { YAxisId = "PyroAxisId" });
                _dataMap.Add(seriesName, new Tuple<double[], int>(_processData.averaged_temperature_on_wafer, i));

                seriesName = string.Format("温度_{0}区_托盘温度", zone[i]);
                RenderableSeries.Add(new SmartDataLine(seriesName, System.Windows.Media.Colors.Red, string.Format("ReactorA.Smart200.SuscTempAvg{0}", zone[i]), true) { YAxisId = "PyroAxisId" });
                _dataMap.Add(seriesName, new Tuple<double[], int>(_processData.averaged_temperature_off_wafer, i));

                Notch[] notch = _susceptorInfo.GetZoneNotch(i);
                if (notch != null)
                {
                    for (int j = 0; j < notch.Length; j++)
                    {
                        seriesName = string.Format("温度_{0}区_Wafer温度_{1}", zone[i], notch[j].DisplayIndex);
                        RenderableSeries.Add(new SmartDataLine(seriesName, System.Windows.Media.Colors.Red, string.Format("ReactorA.Smart200.{0}.Temp.Temp{1}_{2}", susceptorType, zone[i], notch[j].DisplayIndex.ToString("00")), false) { YAxisId = "PyroAxisId" });
                        _dataMap.Add(seriesName, new Tuple<double[], int>(_processData.temperature_each_wafer, waferZoneIndex++));
                    }
                }
            }

            waferZoneIndex = 0;
            for (int i = 0; i < zone.Length; i++)
            {
                string seriesName = string.Format("反射率_{0}区_平均", zone[i]);
                RenderableSeries.Add(new SmartDataLine(seriesName, System.Windows.Media.Colors.Blue, string.Format("ReactorA.Smart200.ReflectAvg{0}", zone[i]), true) { YAxisId = "ReflectAxisId" });
                _dataMap.Add(seriesName, new Tuple<double[], int>(_processData.averaged_reflection_on_wafer, i));

                Notch[] notch = _susceptorInfo.GetZoneNotch(i);
                if (notch != null)
                {
                    for (int j = 0; j < notch.Length; j++)
                    {
                        seriesName = string.Format("反射率_{0}区_{1}", zone[i], notch[j].DisplayIndex);
                        RenderableSeries.Add(new SmartDataLine(seriesName, System.Windows.Media.Colors.Blue, string.Format("ReactorA.Smart200.{0}.Reflect.Reflect{1}_{2}", susceptorType, zone[i], notch[j].DisplayIndex.ToString("00")), false) { YAxisId = "ReflectAxisId" });
                        _dataMap.Add(seriesName, new Tuple<double[], int>(_processData.reflection_each_wafer, waferZoneIndex++));
                    }
                }
            }

            if (EnableCurvature)
            {
                waferZoneIndex = 0;

                for (int i = 0; i < zone.Length; i++)
                {
                    string seriesName = string.Format("翘曲_{0}区_平均", zone[i]);
                    RenderableSeries.Add(new SmartDataLine(seriesName, System.Windows.Media.Colors.Green, string.Format("ReactorA.Smart200.CurvatureAvg{0}", zone[i]), true) { YAxisId = "CurvatureAxisId" });
                    _dataMap.Add(seriesName, new Tuple<double[], int>(_processData.averaged_curvature_on_wafer, i));

                    Notch[] notch = _susceptorInfo.GetZoneNotch(i);
                    if (notch != null)
                    {
                        for (int j = 0; j < notch.Length; j++)
                        {
                            seriesName = string.Format("翘曲_{0}区_{1}", zone[i], notch[j].DisplayIndex);
                            RenderableSeries.Add(new SmartDataLine(seriesName, System.Windows.Media.Colors.Green, string.Format("ReactorA.Smart200.{0}.Curvature.Curvature{1}_{2}", susceptorType, zone[i], notch[j].DisplayIndex.ToString("00")),
                                                                   false) { YAxisId = "CurvatureAxisId" });
                            _dataMap.Add(seriesName,
                                         new Tuple<double[], int>(_processData.curvature_each_wafer, waferZoneIndex++));
                        }
                    }
                }
            }
        }

        public void OnSeriesSelectAll(object param)
        {
            foreach (var item in RenderableSeries)
            {
                var series = item as SmartDataLine;
                if (series == null) continue;
                series.IsVisible = true;
            }
        }

        public void OnSeriesSelectNone(object param)
        {
            foreach (var item in RenderableSeries)
            {
                var series = item as SmartDataLine;
                if (series == null) continue;
                series.IsVisible = false;
            }
        }

        public void OnSeriesSelectDefault(object param)
        {
            foreach (var item in RenderableSeries)
            {
                var series = item as SmartDataLine;
                if (series == null) continue;
                series.IsVisible = series.IsDefaultVisable;
                series.Stroke = series.DefaultSeriesColor;
                series.LineThickness = 1;
            }
        }
    }
}