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

using FLY.Thick.Base.Common;
using FLY.Thick.Base.Client;
using FLY.Thick.Base.IService;
using FObjBase;
namespace ThickTcpUiInWindow
{
    /// <summary>
    /// Page_Profile.xaml 的交互逻辑
    /// </summary>
    public partial class Page_Profile : Page, INotifyPropertyChanged
    {
        private double alarmpercent = 3;
        public double AlarmPercent
        {
            get
            {
                return alarmpercent;
            }
            set
            {
                if (alarmpercent != value)
                {
                    alarmpercent = value;
                    NotifyPropertyChanged("AlarmPercent");
                }
            }
        }
        private double alarm = 3;
        public double Alarm
        {
            get
            {
                return alarm;
            }
            set
            {
                if (alarm != value)
                {
                    alarm = value;
                    NotifyPropertyChanged("Alarm");
                }
            }
        }
        private double target = 30;
        public double Target
        {
            get
            {
                return target;
            }
            set
            {
                if (target != value)
                {
                    target = value;
                    NotifyPropertyChanged("Target");
                }
            }
        }
        private bool setbypercent=false;
        public bool SetByPercent
        {
            get { return setbypercent; }
            set
            {
                if (setbypercent != value)
                {
                    setbypercent = value;
                    NotifyPropertyChanged("SetByPercent");
                }
            }
        }
        private bool profileneedpw=false;
        public bool ProfileNeedPw
        {
            get { return profileneedpw; }
            set
            {
                if (profileneedpw != value)
                {
                    profileneedpw = value;
                    NotifyPropertyChanged("ProfileNeedPw");
                }
            }
        }
        public ObservableCollection<string> mList = new ObservableCollection<string>();
        public ProfileService mProfile = null;
        bool NeedKeepContext = false;
        public Page_Profile()
        {
            InitializeComponent();
        }
        public void Init(IPEndPoint serverep) 
        {
            mProfile = new ProfileService();
            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)
        {
            if (ProfileNeedPw) 
            {
                if (!Window_Password.Authorize(1))
                    return;
            }

            mProfile.PropertyChanged -= handler_profile;
            mProfile.Target = (int)(Target * 100.0);
            mProfile.Alarm = (int)(Alarm * 100.0);
            mProfile.PropertyChanged += handler_profile;

            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_ABHelper w = new Window_ABHelper();
            double comp;
            double shift;
            comp = mProfile.Comp;
            shift = mProfile.Shift / 100.0;
            w.Init(comp, shift);

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

        }


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

        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propertyname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyname));
            }
        }

        #endregion

    }
}
