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 FLY.ControlLibrary;
using System.Collections.ObjectModel;

using FLY.Thick.BulkDataModule;
using FLY.Thick.Base.Client;
using FLY.Thick.Base.Common;
using ThickTcpUiInWindow;
using FLY.Thick.BlowingScan.Client;
using FLY.Thick.Blowing.Client;

namespace FLY.Thick.BlowingScan.UI.Client
{
    /// <summary>
    /// Page_Main.xaml 的交互逻辑
    /// </summary>
    public partial class Page_Main : Page
    {
        private ThickTcpUiInWindow.UIModule.FLYLayout mLayout;
        private FLYLayoutManager mManager;
        TDGage gage;
        public Page_Main()
        {
            InitializeComponent();
        }
        public void Init(TDGage gage) 
        {
            LoadingProgress lp = Application.Current.Properties["LoadingProgress"] as LoadingProgress;

            System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
            {
                this.gage = gage;
                stackpanel_dynarea.DataContext = gage.mDynArea;
                grid_initparam.DataContext = gage.mInitParamService;
                Init_Border_Blowing();
                Init_Border_Width();
                Init_Border_GetSample();
                lp.Progress += 10;
            }));
            System.Threading.Thread.Sleep(10);
            System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
            {
                mLayout = new ThickTcpUiInWindow.UIModule.FLYLayout();
                mLayout.Load();
                grid_layout.DataContext = mLayout;
                mManager = new FLYLayoutManager();
                mManager.myUIModules.Add("scangraph",
                new FLY.UI.Module.UIModuleInfo()
                {
                    UIModule = new ThickTcpUiInWindow.UIModule.UIModule_ScanGraph(),
                    Param = new FLY.UI.Module.UIModuleParam()
                    {
                        IsGraphCompnonent = true
                    }
                });
                mManager.myUIModules.Add("trendgraph",
                    new FLY.UI.Module.UIModuleInfo()
                    {
                        UIModule = new ThickTcpUiInWindow.UIModule.UIModule_TrendGraph(),
                        Param = new FLY.UI.Module.UIModuleParam()
                        {
                            IsGraphCompnonent = true
                        }
                    });
                mManager.myUIModules.Add("fixgraph",
                    new FLY.UI.Module.UIModuleInfo()
                    {
                        UIModule = new ThickTcpUiInWindow.UIModule.UIModule_FixGraph(),
                        Param = new FLY.UI.Module.UIModuleParam()
                        {
                            IsGraphCompnonent = true
                        }
                    });
                mManager.myUIModules.Add("scangraph2", new FLY.UI.Module.UIModuleInfo()
                {
                    UIModule = new UIModule.UIModule_ScanGraph2(),
                    Param = new FLY.UI.Module.UIModuleParam()
                    {
                        IsGraphCompnonent = true
                    }
                });
                mManager.myUIModules.Add("trendgraphcircular", new FLY.UI.Module.UIModuleInfo()
                {
                    UIModule = new UIModule.UIModule_ScanGraphCircular(),
                    Param = new FLY.UI.Module.UIModuleParam()
                    {
                        IsGraphCompnonent = true
                    }
                });
                mManager.Init(
                    tabControl1,
                    stackpanel_dynarea,
                    stackpanel_menu,
                    mLayout);
                lp.Progress += 10;
            }));

            System.Threading.Thread.Sleep(10);


            System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
            {
                mManager.LoadModule();
                lp.Progress += 10;
            }));
            System.Threading.Thread.Sleep(10);


            System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
            {
                Misc.BindingOperations.SetBinding(gage.mDynArea, "ControllerState", UpdateControllerState);
                
                lp.Progress = 100;
            }));
            System.Threading.Thread.Sleep(10);
        }
        void UpdateControllerState()
        {
            var st = mLayout.ControllerStates.Find(_st => _st.ControllerState == gage.mDynArea.ControllerState.ToString());
            if (st != null)
            {
                if (st.No >= 0 && st.No < this.tabControl1.Items.Count)
                    this.tabControl1.SelectedIndex = st.No;
            }
        }


        BlowingScanServiceClient mRenZiJiaService;
        BlowingDetectServiceClient mBDetect;
        void Init_Border_Blowing()
        {
            mRenZiJiaService = new BlowingScanServiceClient();
            mBDetect = new BlowingDetectServiceClient();

            FObjBase.FObjSys.Current.Connect_to_Another_OBJSys(
                gage.mSysParam.ServerIPEP, mRenZiJiaService.ID, mBDetect.ID);

            Border_Blowing.DataContext = mBDetect;
        }

        BorderSearchService mBorderSearchService;
        void Init_Border_Width()
        {
            mBorderSearchService = new BorderSearchService();

            FObjBase.FObjSys.Current.Connect_to_Another_OBJSys(
                gage.mSysParam.ServerIPEP, mBorderSearchService.ID);
        }
        GetSampleService mGetSampleService;
        void Init_Border_GetSample()
        {
            mGetSampleService = new GetSampleService();

            FObjBase.FObjSys.Current.Connect_to_Another_OBJSys(
                gage.mSysParam.ServerIPEP, mGetSampleService.ID);

            Border_Sample.DataContext = mGetSampleService;
        }


        private void button_sysparam_click(object sender, RoutedEventArgs e)
        {
            if (stackpanel_menu.Children.Count > 0)
            {
                mLayout.IsInMenuSelect = true;
            }
            else
            {
                button_menu_click(sender, e);
            }
        }
        private void button_backmenu_click(object sender, RoutedEventArgs e)
        {
            mLayout.IsInMenuSelect = false;
        }
        private void button_menu_click(object sender, RoutedEventArgs e)
        {
            Page_Menu p = new Page_Menu();
            p.Init(gage);
            NavigationService.Navigate(p);
        }


        private void button_profile_click(object sender, RoutedEventArgs e)
        {
            Page_ProfileBlowing p = new Page_ProfileBlowing();
            p.Init(gage.mSysParam.ServerIPEP);
            NavigationService.Navigate(p);
        }

        private void Border_AD_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Border_Sample_Click(object sender, RoutedEventArgs e)
        {
            Page_GetSample p = new Page_GetSample();
            p.Init(gage.mSysParam.ServerIPEP);
            NavigationService.Navigate(p);
        }

        private void Border_Width_Click(object sender, RoutedEventArgs e)
        {
            Page_BorderSearch p = new Page_BorderSearch();
            p.Init(gage.mSysParam.ServerIPEP);
            NavigationService.Navigate(p);
        }


        private void Border_FlyAD_Click(object sender, RoutedEventArgs e)
        {
            Window_FlyADAccess w = new Window_FlyADAccess();
            w.Init(gage.mSysParam.ServerIPEP);
            w.ShowDialog();
        }

        private void Border_IO_Click(object sender, RoutedEventArgs e)
        {
            Window_IOTip w = new Window_IOTip();
            w.ShowDialog();
        }

        private void button_option_click(object sender, RoutedEventArgs e)
        {
            ThickTcpUiInWindow.MainEdit.Page_MainEdit p = new ThickTcpUiInWindow.MainEdit.Page_MainEdit();
            p.Init(mManager);
            NavigationService.Navigate(p);
        }

        private void Border_Blowing_Click(object sender, RoutedEventArgs e)
        {
            Page_Blowing p = new Page_Blowing();
            p.Init(gage.mSysParam.ServerIPEP);
            NavigationService.Navigate(p);
        }
    }




}