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.ComponentModel;

using System.Net;

using FLY.Thick.Base.Common;
using FLY.Thick.Base.Client;
namespace ThickTcpUiInWindow
{
    /// <summary>
    /// Page_Boltmap.xaml 的交互逻辑
    /// </summary>
    public partial class Page_Boltmap : Page
    {
        BoltMapService mBoltMapService;
        InitParamService mInitParamService;

        ObservableCollection<Bolt> bolts = new ObservableCollection<Bolt>();
        private class Bolt : INotifyPropertyChanged
        {
            private int no;
            public int No { get { return no; } set { no = value; NotifyPropertyChanged("No"); } }

            private int begin;
            public int Begin { get { return begin; } set { begin = value; NotifyPropertyChanged("Begin"); } }

            private int end;
            public int End { get { return end; } set { end = value; NotifyPropertyChanged("End"); } }

            #region INotifyPropertyChanged 成员
            protected void NotifyPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
                }
            }
            public event PropertyChangedEventHandler PropertyChanged;

            #endregion

        }

        public Page_Boltmap()
        {
            InitializeComponent();
        }
        public void Init(IPEndPoint serverep) 
        {
            mBoltMapService = new BoltMapService();
            FObjBase.FObjSys.Current.Connect_to_Another_OBJSys(
                serverep, mBoltMapService.ID);

            mInitParamService = new InitParamService();

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

            this.grid_null.DataContext = mInitParamService;

            this.DataContext = mBoltMapService;
        }
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {

        }


        private void button_apply_Click(object sender, RoutedEventArgs e)
        {
            if (Window_Password.Authorize(1))
            {
                mBoltMapService.ApplyBorder();
                FLY.ControlLibrary.Window_Tip.Show("应用成功",
                    null,
                    TimeSpan.FromSeconds(2));
            }
        }

        private void button_createimg_Click(object sender, RoutedEventArgs e)
        {
            if (Window_Password.Authorize(1))
            {
                Window_BoltCreate w = new Window_BoltCreate();
                w.Init(mBoltMapService, mInitParamService);
                w.Owner = FLY.ControlLibrary.COMMON.GetWindow(this);
                w.ShowDialog();
            }
        }

        private void Page_Unloaded(object sender, RoutedEventArgs e)
        {
            mBoltMapService.Dispose();
            mInitParamService.Dispose();
        }
    }

    public class BorderType2BoolConverter : IValueConverter
    {

        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            BORDER_TYPE bordertype = (BORDER_TYPE)value;
            BORDER_TYPE p;
            Enum.TryParse<BORDER_TYPE>((string)parameter, out p);

            if (bordertype == p)
                return true;
            else
                return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            BORDER_TYPE p;
            Enum.TryParse<BORDER_TYPE>((string)parameter, out p);

            bool b = (bool)value;
            if (b)
                return p;
            else
                return null;
        }

        #endregion
    }
    public class BorderType2VisibilityConverter : IValueConverter
    {

        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            BORDER_TYPE bordertype = (BORDER_TYPE)value;
            BORDER_TYPE p;
            Enum.TryParse<BORDER_TYPE>((string)parameter, out p);

            if (bordertype == p)
                return Visibility.Visible;
            else
                return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }

        #endregion
    }
    public class BoltNoMidConverter : IMultiValueConverter
    {

        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if ((values.Length == 2) && (values[0] is int) && (values[1] is int))
            {
                int boltnno_b = (int)values[0];
                int boltnno_e = (int)values[1];
                return (boltnno_b + boltnno_e) / 2.0;
            }
            else
            {
                return 20;
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            double mid;
            if (value is string)
            {

                if (double.TryParse((string)value, out mid))
                {
                    int boltno_b = (int)(mid);
                    int boltno_e = (int)Math.Ceiling(mid);
                    return new object[2] { boltno_b, boltno_e };
                }
            }
            return new object[2] { 10, 10 };

        }
    }
}
