using FLY.OBJComponents.Client;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;

namespace FLY.Weight.UI.Client.UIModule
{
    /// <summary>
    /// Page_Main.xaml 的交互逻辑
    /// </summary>
    public partial class Page_Main : Page
    {
        FLY.Weight.Client.WeightSystemClient mWeighterCsService;
        SysParam sysparam;
        SetPLCUpdatePlan setPlan_accessory;
        List<SetPLCUpdatePlan> setPlan_items = new List<SetPLCUpdatePlan>();
        public Page_Main()
        {
            InitializeComponent();

        }

        public void Init(int id)
        {
            sysparam = SysParam.Current;
            mWeighterCsService = TDGage.Current.mWeighterCsService;
            root_grid.DataContext = mWeighterCsService;

            RebuildPlans();

            mWeighterCsService.ResetItemsEvent += MWeighterCsService_ResetItemsEvent;
        }

        private void MWeighterCsService_ResetItemsEvent()
        {
            RebuildPlans();
        }

        /// <summary>
        /// 由于数量改变,重新构造更新计划
        /// </summary>
        void RebuildPlans()
        {
            if (setPlan_accessory != null)
                setPlan_accessory.Dispose();

            setPlan_accessory = new SetPLCUpdatePlan(
                mWeighterCsService.PLCos,
                mWeighterCsService.Accessory,
                new string[] {
                                "TotalFlowSetting",
                                "TotalFlow",
                                "TotalProduction",

                                "ALast",
                                "ACurrent",
                                "ACurrentLen",
                                "BLast",
                                "BCurrent",
                                "BCurrentLen",
                                "TotalFilmWidth",
                                "IsRimNoRecycle",
                                "RimWidth",
                                "ActFilmWidth",
                                "SetThickness",
                                "TargetVelocity",
                                "CurrentVelocity"
                });


            foreach (SetPLCUpdatePlan plan in setPlan_items)
                plan.Dispose();
            setPlan_items.Clear();
            

            for (int i = 0; i < mWeighterCsService.Items.Count(); i++)
            {
                
                SetPLCUpdatePlan plan = new SetPLCUpdatePlan(
                    mWeighterCsService.PLCos,
                    mWeighterCsService.Items[i],
                    new string[] {
                        "CurrentFlow",
                        "CumulativeProduction",

                        "FlowSetting",
                        "CurrentFlow",
                        "ScrewPDisp",
                        "ScrewPSet",
                        "ScrewPDisp",
                        "MixBucketWeight",
                        "BinWeight",
                        "MixIsOn",
                        "BucketValveIsOpen",
                        "ScrewMotorFreq",
                        "ScrewMotorIsOn",
                        "ScrewIsAutoMode"
                    });
                setPlan_items.Add(plan);
            }
        }

        private void button_frequency_Click(object sender, RoutedEventArgs e)
        {
            Window_Frequency w = new Window_Frequency();
            FLY.Weight.Common.WeighterC weight = ((Button)sender).Tag as FLY.Weight.Common.WeighterC;
            w.FrequencySet = weight.ScrewManualFreq;

            DependencyObject dobj_window = this;
            //一直向上找,直到找到window
            do
            {
                if (dobj_window is Window)
                {
                    break;
                }
                dobj_window = System.Windows.Media.VisualTreeHelper.GetParent(dobj_window);
            } while (dobj_window != null);

            w.Owner = dobj_window as Window;
            if (w.ShowDialog()==true)
            {
                //TODO
                weight.ScrewManualFreq = (float)w.FrequencySet;
            }
        }

        private void button_ratio_Click(object sender, RoutedEventArgs e)
        {
            Window_RatioSet w = new Window_RatioSet();
            //w.DataContext = root_grid.DataContext;
            w.Init(mWeighterCsService);
            w.ShowDialog();
        }

        private void button_info_Click(object sender, RoutedEventArgs e)
        {
            //NavigationService ns = this.NavigationService;// Application.Current.Properties["NavigationService"] as NavigationService;
            NavigationService ns = Application.Current.Properties["NavigationService"] as NavigationService;
            if (ns != null)
            {
                Page_Menu p = new Page_Menu();
                ns.Navigate(p);
            }
        }

        private void button_clear_Click(object sender, RoutedEventArgs e)
        {
            foreach (FLY.Weight.Common.WeighterC weight in mWeighterCsService.Items)
            {
                weight.ClearProduction = true;
            }
            FLY.ControlLibrary.Window_Tip.Show("", "清空成功", TimeSpan.FromSeconds(2));
        }

        private void button_ingredient_Click(object sender, RoutedEventArgs e)
        {
            Window_Ingredient w = new Window_Ingredient();
            FLY.Weight.Common.WeighterC weight = ((Button)sender).Tag as FLY.Weight.Common.WeighterC;
            w.Init(weight);


            DependencyObject dobj_window = this;
            //一直向上找,直到找到window
            do
            {
                if (dobj_window is Window)
                {
                    break;
                }
                dobj_window = System.Windows.Media.VisualTreeHelper.GetParent(dobj_window);
            } while (dobj_window != null);

            w.Owner = dobj_window as Window;
            w.ShowDialog();
        }

        private void button_thickness_Click(object sender, RoutedEventArgs e)
        {
            Window_Thickness w = new Window_Thickness();
            if (mWeighterCsService.Items.Count() < 1)
                return;

            w.SetThickness = (int)mWeighterCsService.Accessory.SetThickness;
            w.FilmWidth = (int)mWeighterCsService.Accessory.TotalFilmWidth;
            w.RimWidth = (int)mWeighterCsService.Accessory.RimWidth;
            w.IsRimNoRecycle = mWeighterCsService.Accessory.IsRimNoRecycle;

            
            DependencyObject dobj_window = this;
            //一直向上找,直到找到window
            do
            {
                if (dobj_window is Window)
                {
                    break;
                }
                dobj_window = System.Windows.Media.VisualTreeHelper.GetParent(dobj_window);
            } while (dobj_window != null);

            w.Owner = dobj_window as Window;
            if (w.ShowDialog() == true)
            {
                mWeighterCsService.Accessory.SetThickness = w.SetThickness;
                mWeighterCsService.Accessory.TotalFilmWidth = w.FilmWidth;
                mWeighterCsService.Accessory.RimWidth = w.RimWidth;
                mWeighterCsService.Accessory.IsRimNoRecycle = w.IsRimNoRecycle;

                FLY.ControlLibrary.Window_Tip.Show("","厚度参数设置成功", TimeSpan.FromSeconds(2));
            }
        }

        private void button_aclear_Click(object sender, RoutedEventArgs e)
        {
            if (mWeighterCsService.ItemsCnt > 0)
            {
                mWeighterCsService.Accessory.AClear = true;
                FLY.ControlLibrary.Window_Tip.Show("", "清空成功", TimeSpan.FromSeconds(2));
            }
        }

        private void button_bclear_Click(object sender, RoutedEventArgs e)
        {
            if (mWeighterCsService.ItemsCnt > 0)
            {
                mWeighterCsService.Accessory.BClear = true;
                FLY.ControlLibrary.Window_Tip.Show("", "清空成功", TimeSpan.FromSeconds(2));
            }
        }
    }




    public class UIModule_Main : FLY.UI.Module.IUIModule
    {
        /// <summary>
        /// 控件标题
        /// 它的值取决于culture
        /// </summary>
        public string Title
        {
            get
            {
                return "称重主界面";
            }
        }

        /// <summary>
        /// 控件
        /// 创建时,需要给它唯一ID,让加载自己的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public FrameworkElement GetComponent(int id)
        {
            Page_Main graph = new Page_Main();
            graph.Init(id);
            return graph;
        }

        /// <summary>
        /// 控件缩略图,用于编辑界面时,大致看看
        /// 创建时,需要给它唯一ID,让加载自己的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public FrameworkElement GetThumbnail(int id)
        {
            return new System.Windows.Controls.Grid();
        }


        /// <summary>
        /// 给出全部控件ID, 控件自行删除没有的参数
        /// </summary>
        /// <param name="IDs"></param>
        public void MatchParam(int[] IDs)
        {
        }
    }
}