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_BoltMapEasy : Page
    {
        BoltMapEasyService mBoltMapService;
        InitParamService mInitParamService;

        public Page_BoltMapEasy()
        {
            InitializeComponent();
        }
        public void Init(IPEndPoint serverep) 
        {
            mBoltMapService = new BoltMapEasyService();
            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.Apply();
                FLY.ControlLibrary.Window_Tip.Show("应用成功",
                    null,
                    TimeSpan.FromSeconds(2));
            }
        }

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


    public class BoltCntConverter : IMultiValueConverter
    {
        /// <summary>
        /// values[0] = posLength values[1]= boltWidth
        /// </summary>
        /// <param name="values"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        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 posLength = (int)values[0];
                int boltWidth = (int)values[1];
                
                
                if (boltWidth > 0)
                    return posLength / boltWidth;
                else
                    return null;
            }
            else
            {
                return 90;
            }
        }

        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 };

        }
    }
}
