using FLY.OBJComponents.Client;
using FLY.Weight2.Client;
using FLY.Weight2.IService;
using System;
using System.Collections.Generic;
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;
using Unity;

namespace FLY.Weight2.UI.Client
{
    /// <summary>
    /// Page_Screw.xaml 的交互逻辑
    /// </summary>
    public partial class PgScrew : Page
    {
        IWeightSystemService weightSystemService;
        List<SetPLCUpdatePlan> setPlan_items = new List<SetPLCUpdatePlan>();

        public PgScrew()
        {
            InitializeComponent();
        }


        [InjectionMethod]
        public void Init(
            IWeightSystemService weightSystemService)
        {
            this.weightSystemService = weightSystemService;
            root_grid.DataContext = this.weightSystemService;

            RebuildPlans();

            if (weightSystemService is WeightSystemServiceClient)
            {
                (weightSystemService as WeightSystemServiceClient).ResetItemsEvent += MWeighterCsService_ResetItemsEvent;
            }
        }

        private void MWeighterCsService_ResetItemsEvent()
        {
            RebuildPlans();
        }
        /// <summary>
        /// 由于数量改变,重新构造更新计划
        /// </summary>
        void RebuildPlans()
        {

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


            for (int i = 0; i < weightSystemService.Items.Count(); i++)
            {

                setPlan_items.Add(new SetPLCUpdatePlan(
                    weightSystemService.PLCos,
                    weightSystemService.Items[i],
                    new string[] {

                        "MaxFreq",
                        "StableTime",
                        "SampleTime",
                        "ChargeTime",
                        "FastStandard",
                        "BinUpperLimit",
                        "BinLowerLimit",
                        "ScrewLowerLimit",


                        "StableLv",
                        "AdjustLv",
                        "HighFactor",
                        "LowFactor",
                        "AdjustLimit",
                        "Pc",
                        "It",
                        "Dg",
                        "Dt",
                        "Filter",
                        "FastFreq",
                        "FastTime",
                        "Cal",
                        "Tolerance",
                        "PIDResult",
                        "PIDAdjust"
                    }));
            }
        }

        private void button_back_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.GoBack();
        }

        private void Page_Unloaded(object sender, RoutedEventArgs e)
        {
            foreach (var setPlan in setPlan_items)
            {
                setPlan.Dispose();
            }
        }
    }
}