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 System.ComponentModel;
using System.Collections.ObjectModel;

using System.Net;


using ThickTcpUiInWindow;
using FObjBase;
using FLY.Thick.Blowing.IService;
using FLY.Thick.Blowing.Client;
using FLY.Thick.Blowing.Common;

namespace FLY.Thick.Blowing.UI.Fix.Client
{
    /// <summary>
    /// Page_ProfileBlowing.xaml 的交互逻辑
    /// </summary>
    public partial class Page_ProfileBlowing : Page, INotifyPropertyChanged
    {
        public double AlarmPercent { get; set; } = 3;

        public double Alarm { get; set; } = 3;

        public double Target { get; set; } = 30;

        public ObservableCollection<string> mList = new ObservableCollection<string>();
        public BlowingFixProfileServiceClient mProfile = null;

        public Page_ProfileBlowing()
        {
            InitializeComponent();
        }
        public void Init(IPEndPoint serverep)
        {
            mProfile = new BlowingFixProfileServiceClient();

            FObjBase.FObjSys.Current.Connect_to_Another_OBJSys(
                serverep, mProfile.ID);

            this.DataContext = mProfile.Param;
            this.listview_profile.ItemsSource = mList;
            this.stackpanel_technological.DataContext = this;

            AlarmPercent = (double)mProfile.Param.Alarm / mProfile.Param.Target;
            Alarm = mProfile.Param.Alarm / 100.0;
            Target = mProfile.Param.Target / 100.0;


            handler_profile = new PropertyChangedEventHandler(mProfile_PropertyChanged);
            handler_page_profile = new PropertyChangedEventHandler(Page_Profile_PropertyChanged);

            this.PropertyChanged += handler_page_profile;
            mProfile.Param.PropertyChanged += new PropertyChangedEventHandler(mProfile_PropertyChanged);

            mProfile.GetList(
                new AsyncCBHandler(
                    delegate(object AsyncState, object retData)
                    {
                        List<string> list = (List<string>)retData;
                        mList.Clear();
                        for (int i = 0; i < list.Count(); i++)
                            mList.Add(list[i]);

                        if (mList.Contains(mProfile.Param.PName))
                        {
                            listview_profile.SelectedItem = mList.First(s => s == mProfile.Param.PName);
                        }
                    }), null);
        }
        PropertyChangedEventHandler handler_page_profile;
        PropertyChangedEventHandler handler_profile;
        void Page_Profile_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "AlarmPercent")
            {
                Alarm = Math.Round(Target * AlarmPercent, 2);

            }
            else if (e.PropertyName == "Target")
            {
                Alarm = Math.Round(Target * AlarmPercent,2);
            }
        }

        void mProfile_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if ((e.PropertyName == "Target") || (e.PropertyName == "Alarm"))
            {
                FObjBase.PollModule.Current.Poll_JustOnce(delegate()
                {
                    this.PropertyChanged -= handler_page_profile;
                    AlarmPercent = (double)mProfile.Param.Alarm / mProfile.Param.Target;
                    Alarm = mProfile.Param.Alarm / 100.0;
                    Target = mProfile.Param.Target / 100.0;
                    this.PropertyChanged += handler_page_profile;
                }, this, 0);
            }
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
        }

        private void button_read_Click(object sender, RoutedEventArgs e)
        {
            if (listview_profile.SelectedItem != null)
            {
                string productname = listview_profile.SelectedItem as string;
                mProfile.Read(
                    productname,
                    new AsyncCBHandler(
                    delegate(object AsyncState, object retData)
                    {
                        BlowingFixProfileParam p = (BlowingFixProfileParam)retData;
                        Misc.PropertiesManager.CopyTo<BlowingFixProfileParam>(p, mProfile.Param);
                        
                    }), null);
            }
        }

        private void button_apply_Click(object sender, RoutedEventArgs e)
        {
            mProfile.Param.PropertyChanged -= handler_profile;
            mProfile.Param.Target = (int)(Target * 100.0);
            mProfile.Param.Alarm = (int)(Alarm * 100.0);
            mProfile.Param.PropertyChanged += handler_profile;

            //有效性检查
            switch (mProfile.Param.MMode)
            {
                case MeasureMode.Edge:
                    break;
                case MeasureMode.Normal:
                    {
                        if (mProfile.Param.FilmWidth < 500)
                        {
                            FLY.ControlLibrary.Window_WarningTip.Show("参数出错", "膜宽<500mm 异常");
                            return;
                        }
                        else if (mProfile.Param.FilmPosH > mProfile.Param.FilmWidth / 3)
                        {
                            FLY.ControlLibrary.Window_WarningTip.Show("参数出错", "探头位置>(膜宽/3) 异常");
                            return;
                        }
                        else if (mProfile.Param.FilmPosH < 0)
                        {
                            FLY.ControlLibrary.Window_WarningTip.Show("参数出错", "探头位置<0 异常");
                            return;
                        }
                    }break;
                default:
                    //case MeasureMode.Bag:
                    {
                        if (mProfile.Param.FilmWidth < 500)
                        {
                            FLY.ControlLibrary.Window_WarningTip.Show("参数出错", "膜宽<500mm 异常");
                            return;
                        }
                        else if (mProfile.Param.FilmPosH > mProfile.Param.FilmWidth / 3)
                        {
                            FLY.ControlLibrary.Window_WarningTip.Show("参数出错", "探头位置>(膜宽/3) 异常");
                            return;
                        }
                        else if (mProfile.Param.FilmPosH < 0)
                        {
                            FLY.ControlLibrary.Window_WarningTip.Show("参数出错", "探头位置<0 异常");
                            return;
                        }
                        else if (mProfile.Param.BagFold0 < mProfile.Param.FilmPosH)
                        {
                            FLY.ControlLibrary.Window_WarningTip.Show("参数出错", "探头下的折边 < 探头位置");
                            return;
                        }
                        else if (mProfile.Param.BagFold1 < mProfile.Param.FilmPosH)
                        {
                            FLY.ControlLibrary.Window_WarningTip.Show("参数出错", "另一边的折边 < 探头位置");
                            return;
                        }
                        else if (mProfile.Param.FilmWidth < (mProfile.Param.BagFold0 + mProfile.Param.BagFold1))
                        {
                            FLY.ControlLibrary.Window_WarningTip.Show("参数出错", "折边 >(膜宽/2) 异常");
                            return;
                        }
                    }
                    break;
            }

            if (mProfile.Param.Target <= 0)
            {
                FLY.ControlLibrary.Window_WarningTip.Show("参数出错", "目标值<=0 异常");
                return;
            }

            if (mProfile.Param.Alarm <= 0)
            {
                FLY.ControlLibrary.Window_WarningTip.Show("参数出错", "公差<=0 异常");
                return;
            }

            mProfile.Apply();
            if (!mList.Contains(mProfile.Param.PName))
            {
                mList.Add(mProfile.Param.PName);
            }
            FLY.ControlLibrary.Window_Tip.Show("应用 & 保存成功",
                mProfile.Param.PName,
                TimeSpan.FromSeconds(2));

        }

        private void button_del_Click(object sender, RoutedEventArgs e)
        {
            if (listview_profile.SelectedItem != null)
            {
                string productname = listview_profile.SelectedItem as string;
                mProfile.Del(productname);
                mList.Remove(productname);
                FLY.ControlLibrary.Window_Tip.Show("删除成功",
                    productname,
                    TimeSpan.FromSeconds(2));
            }
        }

        private void button_abhelper_Click(object sender, RoutedEventArgs e)
        {

            Window_AHelper w = new Window_AHelper();
            w.Init(mProfile.Param.Comp);
            w.Owner = FLY.ControlLibrary.COMMON.GetWindow(this);
            if (w.ShowDialog() == true)
            {
                mProfile.Param.Comp = w.A;
            }
        }


        private void Page_Unloaded(object sender, RoutedEventArgs e)
        {
            mProfile.Dispose();
        }
        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;


        #endregion

        private void button_legend_Click(object sender, RoutedEventArgs e)
        {
            Window_ProfileLegend w = new Window_ProfileLegend();
            w.DataContext = mProfile.Param;
            w.Owner = FLY.ControlLibrary.COMMON.GetWindow(this);
            w.ShowDialog();
        }
    }
}