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

using FLY.Thick.Base.Client;
namespace ThickTcpUiInWindow
{
    /// <summary>
    /// Page_GetSample.xaml 的交互逻辑
    /// </summary>
    public partial class Page_GetSample : Page
    {
        GetSampleServiceClient mGetSampleService;
        InitParamServiceClient mInitParamService;
        GageInfoServiceClient mGageInfoService;
        IPEndPoint serverep;

        public Page_GetSample()
        {
            InitializeComponent();
        }
        public void Init(IPEndPoint serverep) 
        {
            mGetSampleService = new GetSampleServiceClient(FLY.Thick.Base.OBJ_INTERFACE.OBJ_INTERFACE_ID.GET_SAMPLE_ID);
             this.serverep = serverep;

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

            mInitParamService = new InitParamServiceClient(FLY.Thick.Base.OBJ_INTERFACE.OBJ_INTERFACE_ID.INITPARAM_ID);
            FObjBase.FObjSys.Current.Connect_to_Another_OBJSys(
                serverep, mInitParamService.ID);

            mGageInfoService = new GageInfoServiceClient(FLY.Thick.Base.OBJ_INTERFACE.OBJ_INTERFACE_ID.GAGEINFO_ID);
            FObjBase.FObjSys.Current.Connect_to_Another_OBJSys(
                serverep, mGageInfoService.ID);


            this.DataContext = mGetSampleService;
            this.grid_null.DataContext = mInitParamService;


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

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

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

        private void button_getorgad_Click(object sender, RoutedEventArgs e)
        {
            //if (mGageInfoService.DataOK)
            {
                if (mGageInfoService.ForwData == null || mGageInfoService.BackwData == null)
                {
                    FLY.ControlLibrary.Window_WarningTip.Show("失败",
                        "机架信息 为空",
                        TimeSpan.FromSeconds(2));
                    return;
                }
                int[] forwdata = mGageInfoService.ForwData.ToArray();
                int[] backwdata = mGageInfoService.BackwData.ToArray();

                for (int i = 0; i < mGetSampleService.Samples.Count(); i++)
                {
                    if (mGetSampleService.Samples[i].Enable)
                    {

                        int grid = mGetSampleService.Samples[i].Position / mGageInfoService.PosOfGrid;
                        int range = mGetSampleService.Range / mGageInfoService.PosOfGrid;

                        int bg = grid - range;
                        int eg = grid + range;

                        if (bg < 0 || bg >= forwdata.Length || bg > backwdata.Length)
                        {
                            FLY.ControlLibrary.Window_WarningTip.Show("失败",
                                "位置出错",
                                TimeSpan.FromSeconds(2));
                            return;
                        }
                        if (eg < 0 || eg >= forwdata.Length || eg > backwdata.Length)
                        {
                            FLY.ControlLibrary.Window_WarningTip.Show("失败",
                                "位置出错",
                                TimeSpan.FromSeconds(2));
                            return;
                        }

                        int ad1 = Misc.MyMath.Avg(forwdata, bg, eg);
                        int ad2 = Misc.MyMath.Avg(backwdata, bg, eg);
                        if (!Misc.MyBase.ISVALIDATA(ad1) || !Misc.MyBase.ISVALIDATA(ad2))
                        {
                            FLY.ControlLibrary.Window_WarningTip.Show("失败",
                                "位置出错",
                                TimeSpan.FromSeconds(2));
                            return;
                        }
                        mGetSampleService.Samples[i].OrgAD = (ad1+ad2)/2;
                    }
                }

                FLY.ControlLibrary.Window_Tip.Show("设置完成",
                    null,
                    TimeSpan.FromSeconds(2));
            }
            //else 
            //{
            //    FLY.ControlLibrary.Window_WarningTip.Show("失败",
            //        "机架信息 为空",
            //        TimeSpan.FromSeconds(2));
            //}
        }

        private void button_gageinfo_Click(object sender, RoutedEventArgs e)
        {
            Page_GageInfo p = new Page_GageInfo();
            p.Init(serverep);
            NavigationService.Navigate(p);
        }




    }
}
