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;

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

                        "ALast",
                        "ACurrent",
                        "ACurrentLen",
                        "AClear",

                        "BLast",
                        "BCurrent",
                        "BCurrentLen",
                        "BClear",

                        "TotalFilmWidth",
                        "IsRimNoRecycle",
                        "RimWidth",
                        "ActFilmWidth",
                        "SetThickness",
                        "TargetVelocity",
                        "CurrentVelocity"
                });

            for (int i = 0; i < mWeighterCsService.Items.Count(); i++)
            {
                SetPLCUpdatePlan plan = new SetPLCUpdatePlan(
                    mWeighterCsService.PLCos,
                    mWeighterCsService.Items[i],
                    item_update_propertynames);
                setPlan_items.Add(plan);
            }

            mWeighterCsService.ResetItemsEvent += MWeighterCsService_ResetItemsEvent;
        }

        private void MWeighterCsService_ResetItemsEvent()
        {
            //把多出来的删除
            int remove_cnt = setPlan_items.Count() - mWeighterCsService.Items.Count();
            if (remove_cnt > 0)
            {
                for (int i = 0; i < remove_cnt; i++)
                {
                    SetPLCUpdatePlan plan = setPlan_items[setPlan_items.Count() - 1 - i];
                    plan.Dispose();
                }
                setPlan_items.RemoveRange(setPlan_items.Count() - remove_cnt, remove_cnt);
            }
            else
            {
                int start_idx = setPlan_items.Count();
                int add_cnt = -remove_cnt;
                for (int i = 0; i < add_cnt; i++)
                {
                    SetPLCUpdatePlan plan = new SetPLCUpdatePlan(
                    mWeighterCsService.PLCos,
                    mWeighterCsService.Items[start_idx+i],
                    item_update_propertynames);
                    setPlan_items.Add(plan);
                }
            }
        }
        string[] item_update_propertynames = new string[] {
            "CurrentFlow",
            "CumulativeProduction",
            "FlowSetting",
            "CurrentFlow",
            "ScrewPDisp",
            "ScrewPSet",
            "ScrewPDisp",
            "MixBucketWeight",
            "BinWeight",
            "MixIsOn",
            "BucketValveIsOpen",
            "ScrewMotorFreq",
            "ScrewMotorIsOn",
            "ScrewIsAutoMode",
            "ScrewManualFreq"
        };

        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;
            w.Owner = FLY.ControlLibrary.COMMON.GetWindow(this);
            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.Owner = FLY.ControlLibrary.COMMON.GetWindow(this);
            //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);


            w.Owner = FLY.ControlLibrary.COMMON.GetWindow(this);
            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;

            

            w.Owner = FLY.ControlLibrary.COMMON.GetWindow(this);

            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)
        {
        }
    }
}