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 FLY.Thick.Base.Client;
using FLY.Thick.Base.Common;
using FLY.Thick.Base.IService;
using FObjBase;

namespace ThickTcpUiInWindow
{
    /// <summary>
    /// Page_ProfilePlastic.xaml 的交互逻辑
    /// </summary>
    public partial class Page_ProfilePlastic : Page, INotifyPropertyChanged
    {

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

        public double Alarm { get; set; } = 3;
        
        public double Target { get; set; } = 30;
        public bool SetByPercent { get; set; }
        public ObservableCollection<string> mList = new ObservableCollection<string>();
        public ProfileServiceClient mProfile = null;
        bool NeedKeepContext = false;
        public Page_ProfilePlastic()
        {
            InitializeComponent();
        }
        public void Init(IPEndPoint serverep, UInt32 serverid)
        {
            mProfile = new ProfileServiceClient(serverid);
            FObjBase.FObjSys.Current.Connect_to_Another_OBJSys(
                serverep, mProfile.ID);

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

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


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

            this.PropertyChanged += handler_page_profile;
            mProfile.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.PName))
                        {
                            listview_profile.SelectedItem = mList.First(s => s == mProfile.PName);
                        }
                    }), null);
        }
        PropertyChangedEventHandler handler_page_profile;
        PropertyChangedEventHandler handler_profile;
        void Page_Profile_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "AlarmPercent")
            {
                if (SetByPercent)
                {
                    Alarm = Target * AlarmPercent;
                }
            }
            else if (e.PropertyName == "Alarm")
            {
                if (!SetByPercent)
                {
                    AlarmPercent = Alarm / Target;
                }
            }
            else if (e.PropertyName == "Target")
            {
                if (SetByPercent)
                {
                    Alarm = Target * AlarmPercent;
                }
                else
                {
                    AlarmPercent = Alarm / Target;
                }
            }
        }

        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.Alarm / mProfile.Target;
                    Alarm = mProfile.Alarm / 100.0;
                    Target = mProfile.Target / 100.0;
                    this.PropertyChanged += handler_page_profile;
                }, this, 0);
            }
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            if (NeedKeepContext)
            {
                NeedKeepContext = false;
                return;
            }
        }

        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)
                    {
                        ProfileParam p = (ProfileParam)retData;
                        mProfile.PName = p.pname;
                        mProfile.Target = p.target;
                        mProfile.Alarm = p.alarm;
                        mProfile.Comp = p.comp;
                        mProfile.Shift = p.shift;
                        mProfile.BeginNo = p.beginNo;
                        mProfile.EndNo = p.endNo;
                        mProfile.DataBeginNo = p.dataBeginNo;
                        mProfile.DataEndNo = p.dataEndNo;
                    }), null);
            }
        }

        private void button_apply_Click(object sender, RoutedEventArgs e)
        {
            mProfile.PropertyChanged -= handler_profile;
            mProfile.Target = (int)(Target * 100.0);
            mProfile.Alarm = (int)(Alarm * 100.0);
            mProfile.PropertyChanged += handler_profile;
            mProfile.Shift = 0;
            mProfile.Apply();
            if (!mList.Contains(mProfile.PName))
            {
                mList.Add(mProfile.PName);
            }
            FLY.ControlLibrary.Window_Tip.Show("应用 & 保存成功",
                mProfile.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();
            double comp;
            comp = mProfile.Comp;
            w.Init(comp);

            w.Owner = FLY.ControlLibrary.COMMON.GetWindow(this);
            if (w.ShowDialog() == true)
            {
                mProfile.Comp = w.A;
                mProfile.Shift = 0;
            }
        }


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

        public event PropertyChangedEventHandler PropertyChanged;


        #endregion

    }
}
