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.Navigation;
using System.Windows.Shapes;
using SciChart.Charting.Visuals.Annotations;
using Aitex.Core.UI.ControlDataContext;
using Aitex.Core.Equipment.SusceptorDefine;

namespace Aitex.Core.UI.Control
{
    /// <summary>
    /// Interaction logic for PocketRawDataChart.xaml
    /// </summary>
    public partial class PocketRawDataChart : UserControl
    {
        public static readonly DependencyProperty SetDummyCommandProperty = DependencyProperty.Register(
                        "SetDummyCommand", typeof(ICommand), typeof(PocketRawDataChart),
                        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));

        public ICommand SetDummyCommand
        {
            get
            {
                return (ICommand)this.GetValue(SetDummyCommandProperty);
            }
            set
            {
                this.SetValue(SetDummyCommandProperty, value);
            }
        }    

        public static readonly DependencyProperty PocketInfoProperty = DependencyProperty.Register(
        "PocketInfo", typeof(PocketItem[]), typeof(PocketRawDataChart), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
        
        public PocketItem[] PocketInfo
        {
            get
            {
                return (PocketItem[])this.GetValue(PocketInfoProperty);
            }
            set
            {
                this.SetValue(PocketInfoProperty, value);
            }
        }

        public static readonly DependencyProperty EnableTemperatureProperty = DependencyProperty.Register(
                "EnableTemperature", typeof(bool), typeof(PocketRawDataChart), new PropertyMetadata(false));

        public bool EnableTemperature
        {
            get { return (bool)GetValue(EnableTemperatureProperty); }
            set { SetValue(EnableTemperatureProperty, value); }
        }
        
        VerticalLineAnnotation[] _pocketLines;
        VerticalLineAnnotation[] _pocketNumbers;
        BoxAnnotation[] _waferBoxs;
        PocketItem[] _currentPocketInfo;

        public PocketRawDataChart()
        {
            InitializeComponent();

            SetBinding(PocketRawDataChart.PocketInfoProperty, new Binding("PocketInfo") { });
            SetBinding(PocketRawDataChart.SetDummyCommandProperty, new Binding("SetDummyCommand") { });
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);

            bool isPocketEqual = false;
            if (PocketInfo!=null && _currentPocketInfo != null && _currentPocketInfo.Length==PocketInfo.Length)
            {
                for (int i=0; i<_currentPocketInfo.Length; i++)
                {
                    if ((_currentPocketInfo[i].DisplayIndex != PocketInfo[i].DisplayIndex)
                        ||(_currentPocketInfo[i].Status != PocketInfo[i].Status)
                        || (_currentPocketInfo[i].Type != PocketInfo[i].Type))
                    {
                        isPocketEqual = false;
                        break;
                    }
                    isPocketEqual = true;
                }
            }
            if (isPocketEqual)
                return;
            
            

                if (_pocketLines != null)
                {
                    foreach (VerticalLineAnnotation ann in _pocketLines)
                        sciChart.Annotations.Remove(ann);
                }
                if (_pocketNumbers != null)
                {
                    foreach (VerticalLineAnnotation ann in _pocketNumbers)
                        sciChart.Annotations.Remove(ann);
                }
                if (_waferBoxs != null)
                {
                    foreach (BoxAnnotation ann in _waferBoxs)
                        sciChart.Annotations.Remove(ann);
                }

                if (PocketInfo!=null && PocketInfo.Length>0)
                {
                    _pocketLines = new VerticalLineAnnotation[PocketInfo.Length - 1];
                    for (int i = 0; i < _pocketLines.Length; i++)
                    {
                        _pocketLines[i] = new VerticalLineAnnotation();
                        _pocketLines[i].YAxisId = "TemperatureYAxis";

                        sciChart.Annotations.Add(_pocketLines[i]);
                    }

                    _pocketNumbers = new VerticalLineAnnotation[PocketInfo.Length];
                    for (int i = 0; i < PocketInfo.Length; i++)
                    {
                        _pocketNumbers[i] = new VerticalLineAnnotation();
                        _pocketNumbers[i].YAxisId = "TemperatureYAxis";
                        _pocketNumbers[i].ShowLabel = true;
                        _pocketNumbers[i].LabelValue = PocketInfo[i].DisplayIndex;
                        _pocketNumbers[i].StrokeThickness = 0;
                        _pocketNumbers[i].Stroke = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromRgb(214, 214, 214));

                        sciChart.Annotations.Add(_pocketNumbers[i]);
                    }

                    _waferBoxs = new BoxAnnotation[PocketInfo.Length];
                    for (int i = 0; i < PocketInfo.Length; i++)
                    {
                        _waferBoxs[i] = new BoxAnnotation();
                        _waferBoxs[i].YAxisId = "TemperatureYAxis";
                        _waferBoxs[i].Visibility = Visibility.Hidden;
                        _waferBoxs[i].Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromArgb(0x55, 0x55, 0x55, 0x55));
                        _waferBoxs[i].BorderBrush = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromRgb(0x55, 0x55, 0x55));
                        _waferBoxs[i].CornerRadius = new CornerRadius(3);
                        _waferBoxs[i].X1 = 0;
                    }

                    XAxis_Arranged(null, null);
                }

            _currentPocketInfo = PocketInfo;
            if (_waferBoxs != null)
            {
                for (int i = 0; i < _waferBoxs.Length; i++)
                {
                    if (_currentPocketInfo != null && (_currentPocketInfo[i].Status != WaferStatus.Dummy) && (_currentPocketInfo[i].Type != WaferType.Empty))
                    {
                        if (!sciChart.Annotations.Contains(_waferBoxs[i]))
                            sciChart.Annotations.Add(_waferBoxs[i]);
                    }
                    else
                    {
                        sciChart.Annotations.Remove(_waferBoxs[i]);
                    }
                }
            }
        }

        private void sciChart_MouseLeave(object sender, MouseEventArgs e)
        {
            if (DataContext == null)
                return;

            maskAnnotation.X1 = maskAnnotation.X2 = 0;
        }

        private void sciChart_MouseMove(object sender, MouseEventArgs e)
        {
            if (PocketInfo == null)
                return;

            int pocketIndex = (int)((e.GetPosition(sciChart).X - 40) / ((sciChart.ActualWidth - 40 - 35 - 35) / PocketInfo.Length)) + 1;

            double maskInterval = sciChart.XAxis.VisibleRange.AsDoubleRange().Diff / PocketInfo.Length;

            maskAnnotation.X1 = sciChart.XAxis.VisibleRange.AsDoubleRange().Min + maskInterval * (pocketIndex - 1) + maskInterval * 0.1;
            maskAnnotation.X2 = sciChart.XAxis.VisibleRange.AsDoubleRange().Min + maskInterval * pocketIndex - maskInterval * 0.1;
        }

        private void TemperatureYAxis_Arranged(object sender, EventArgs e)
        {
            if (PocketInfo == null)
                return;

            if (sciChart.YAxes[0].VisibleRange != null)
            {
                maskAnnotation.Y1 = sciChart.YAxes[0].VisibleRange.AsDoubleRange().Max;
                maskAnnotation.Y2 = sciChart.YAxes[0].VisibleRange.AsDoubleRange().Min;

                if (_waferBoxs != null)
                {
                    for (int i = 0; i < _waferBoxs.Length; i++)
                    {
                        _waferBoxs[i].Y1 = sciChart.YAxes[0].VisibleRange.AsDoubleRange().Max;
                        _waferBoxs[i].Y2 = sciChart.YAxes[0].VisibleRange.AsDoubleRange().Min;
                    }
                }
            }
        }

        private void XAxis_Arranged(object sender, EventArgs e)
        {
            if (PocketInfo == null)
                return;

            int pocketNumber = PocketInfo.Length;
            if (pocketNumber == 0)
                return;

            double pocketInterval = sciChart.XAxis.VisibleRange.AsDoubleRange().Diff / pocketNumber;

            if (_pocketLines != null)
            {
                for (int i = 0; i < _pocketLines.Length; i++)
                {
                    _pocketLines[i].X1 = sciChart.XAxis.VisibleRange.AsDoubleRange().Min + (i + 1) * pocketInterval;
                    _pocketLines[i].X2 = sciChart.XAxis.VisibleRange.AsDoubleRange().Min + (i + 1) * pocketInterval;
                    _pocketLines[i].Y1 = sciChart.YAxes[0].VisibleRange.AsDoubleRange().Max;
                    _pocketLines[i].Y2 = 0;
                }
            }

            if (_pocketNumbers != null)
            {
                for (int i = 0; i < _pocketNumbers.Length; i++)
                {
                    _pocketNumbers[i].X1 = sciChart.XAxis.VisibleRange.AsDoubleRange().Min + pocketInterval * i + pocketInterval / 2;
                }
            }

            if (_waferBoxs != null)
            {
                double maskInterval = sciChart.XAxis.VisibleRange.AsDoubleRange().Diff / pocketNumber;
                for (int i = 0; i < _waferBoxs.Length; i++)
                {
                    _waferBoxs[i].X1 = sciChart.XAxis.VisibleRange.AsDoubleRange().Min + maskInterval * i + maskInterval * 0.1;
                    _waferBoxs[i].X2 = sciChart.XAxis.VisibleRange.AsDoubleRange().Min + maskInterval * (i + 1) - maskInterval * 0.1;
                }
            }
        }

        private void sciChart_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (SetDummyCommand == null)
                return;

            if (!this.IsEnabled)
                return;

            if (PocketInfo == null)
                return;

            int pocketIndex = (int)((e.GetPosition(sciChart).X - 40) / ((sciChart.ActualWidth - 40 - 35 - 35) / PocketInfo.Length));

            if (PocketInfo[pocketIndex].Type == WaferType.Empty)
                return;

            if (MessageBox.Show(String.Format("请确认是否将第{0}片设置为{1}?", PocketInfo[pocketIndex].DisplayIndex, PocketInfo[pocketIndex].Status == WaferStatus.Dummy ? "正片" : "陪片"), "确认", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.No)
                return;

            SetDummyCommand.Execute(new object[] { PocketInfo[pocketIndex].Index, PocketInfo[pocketIndex].Status == WaferStatus.Dummy ? WaferStatus.Normal : WaferStatus.Dummy});
        }

    }
}