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 Aitex.Core.Equipment.SusceptorDefine;
using Aitex.Core.Util;
namespace Aitex.Core.UI.Control
{
    /// 
    /// Interaction logic for SusceptorControl.xaml
    /// 
    public partial class SusceptorControl : UserControl
    {
        public WaferType DefaultWaferType { get; set; }
        Susceptor _susceptorInfo;
        bool _isShining = false;
        public SusceptorControl()
        {
            InitializeComponent();
        }
        public static readonly DependencyProperty SusceptorInfoProperty = DependencyProperty.Register(
            "SusceptorInfo", typeof(Susceptor), typeof(SusceptorControl),
            new FrameworkPropertyMetadata(new Susceptor(), FrameworkPropertyMetadataOptions.AffectsRender));
        public Susceptor SusceptorInfo
        {
            get
            {
                return (Susceptor)GetValue(SusceptorInfoProperty);
            }
            set
            {
                SetValue(SusceptorInfoProperty, value);
            }
        }
        public static readonly DependencyProperty IsActiveProperty = DependencyProperty.Register(
            "IsActive", typeof(bool), typeof(SusceptorControl),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
        public bool IsActive
        {
            get
            {
                return (bool)GetValue(IsActiveProperty);
            }
            set
            {
                SetValue(IsActiveProperty, value);
            }
        }
        public static readonly DependencyProperty IsReadonlyProperty = DependencyProperty.Register(
            "IsReadonly", typeof(Boolean), typeof(SusceptorControl),
            new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsRender));
        public bool IsReadonly
        {
            get
            {
                return (bool)GetValue(IsReadonlyProperty);
            }
            set
            {
                SetValue(IsReadonlyProperty, value);
            }
        }
        public void StartShining(SolidColorBrush brush, bool isShining)
        {
            if (layout.Children != null && layout.Children.Count > 0)
            {
                ((Ellipse)layout.Children[0]).Fill = brush;
            }
            _isShining = isShining;
        }
        public void UpdateWaferType(WaferType waferType)
        {
            for (int i = 0; i < SusceptorInfo.WaferTypeArray.Length && i < SusceptorInfo.Config.Notches.Count; i++)
            {
                if (SusceptorInfo.WaferTypeArray[i] != WaferType.Empty)
                    SusceptorInfo.WaferTypeArray[i] = waferType;
            }
            for (int i = 1; i < layout.Children.Count; i++)
            {
                SusceptorNotchControl control = layout.Children[i] as SusceptorNotchControl;
                control.CurrentWaferType = SusceptorInfo.WaferTypeArray[control.Index-1];
                control.Update();
            }
            DefaultWaferType = waferType;
        }
        public void SetAllHasWafer()
        {
            for (int i = 0; i < SusceptorInfo.WaferTypeArray.Length && i 0 && layout.Children[0].GetType() == typeof(Ellipse))
            {
                Ellipse ellipse = layout.Children[0] as Ellipse;
                ellipse.Fill = (SusceptorInfo == null || SusceptorInfo.Status <= SusceptorStatus.Unprocessed) ? SusceptorBrush.BackgroundBrush : SusceptorBrush.GetBrush(SusceptorInfo.Status);
            }
        }
        void DrawContour()
        {
            Ellipse contourEllipse = new Ellipse()
            {
                Fill = (SusceptorInfo == null || SusceptorInfo.Status <= SusceptorStatus.Unprocessed) ? SusceptorBrush.BackgroundBrush : SusceptorBrush.GetBrush(SusceptorInfo.Status),
                Stroke = SusceptorBrush.StrokeBrush,
                Width = SusceptorInfo.Config.Diameter,
                Height = SusceptorInfo.Config.Diameter,
                Opacity = 1,
            };
            layout.Children.Add(contourEllipse);
        }
        void DrawNotchControls()
        {
            double diameter = SusceptorInfo.Config.NotchDiameter;
            foreach (var notch in SusceptorInfo.Config.Notches)
            {
                double x = notch.X;
                double y = notch.Y;
                SusceptorNotchControl container = new SusceptorNotchControl()
                {
                    Width = diameter,
                    Height = diameter,
                    Index = notch.Index,
                    DisplayIndex = notch.DisplayIndex,
                    IsActive = IsActive,
                    IsReadonly = IsReadonly,
                    MouseDownCallback = (nc, isShiftDown) =>
                    {
                        if (nc.CurrentWaferType == WaferType.Empty)
                        {
                            nc.CurrentWaferType = DefaultWaferType;
                            if (isShiftDown)
                                nc.CurrentWaferStatus = WaferStatus.Dummy;
                            SusceptorInfo.WaferTypeArray[nc.Index-1] = DefaultWaferType;
                        }
                        else
                        {
                            nc.CurrentWaferType = WaferType.Empty;
                            nc.CurrentWaferStatus = WaferStatus.Normal;
                            SusceptorInfo.WaferTypeArray[nc.Index-1] = WaferType.Empty;
                        }
                    },
                };
                container.SetValue(Canvas.LeftProperty, y + SusceptorInfo.Config.Diameter / 2 - SusceptorInfo.Config.NotchDiameter / 2);
                container.SetValue(Canvas.TopProperty, x + SusceptorInfo.Config.Diameter / 2 - SusceptorInfo.Config.NotchDiameter / 2);
                layout.Children.Add(container);
            }
        }
    }
}