using FLY.OBJComponents.Client;
using FLY.Weight.Client;
using FLY.Weight.IService;
using MultiLayout.UiModule;
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.Weight.UI.Client.UiModule
{
    /// <summary>
    /// UcTotalFlow.xaml 的交互逻辑
    /// </summary>
    public partial class UcTotalFlow : UserControl
    {
        IWeightSystemService weightSystemService;

        SetPLCUpdatePlan setPlan_accessory;
        List<SetPLCUpdatePlan> setPlan_items = new List<SetPLCUpdatePlan>();

        public UcTotalFlow()
        {
            InitializeComponent();
        }
        public void Init(IWeightSystemService weightSystemService)
        {
            this.weightSystemService = weightSystemService;
            this.DataContext = weightSystemService;
            string objname = nameof(IWeightSystemService.Accessory);
            setPlan_accessory = new SetPLCUpdatePlan(
                weightSystemService.PLCos,
                objname,
                new string[] {
                        nameof(Common.WeighterAccessory.TotalFlowSetting),//√
                        nameof(Common.WeighterAccessory.TotalFlow),//√
                        nameof(Common.WeighterAccessory.TotalProduction),//√

                        nameof(Common.WeighterAccessory.ALast),
                        nameof(Common.WeighterAccessory.ACurrent),
                        nameof(Common.WeighterAccessory.ACurrentLen),
                        nameof(Common.WeighterAccessory.AClear),

                        nameof(Common.WeighterAccessory.BLast),
                        nameof(Common.WeighterAccessory.BCurrent),
                        nameof(Common.WeighterAccessory.BCurrentLen),
                        nameof(Common.WeighterAccessory.BClear)
                });

            for (int i = 0; i < weightSystemService.Items.Count(); i++)
            {
                objname = $"{nameof(IWeightSystemService.Items)}[{i}]";
                SetPLCUpdatePlan plan = new SetPLCUpdatePlan(
                    weightSystemService.PLCos,
                    objname,
                    item_update_propertynames);
                setPlan_items.Add(plan);
            }

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

        private void MWeighterCsService_ResetItemsEvent()
        {
            //把多出来的删除
            int remove_cnt = setPlan_items.Count() - weightSystemService.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++)
                {
                    string objname = $"{nameof(IWeightSystemService.Items)}[{start_idx+ i}]";
                    SetPLCUpdatePlan plan = new SetPLCUpdatePlan(
                    weightSystemService.PLCos,
                    objname,
                    item_update_propertynames);
                    setPlan_items.Add(plan);
                }
            }
        }
        string[] item_update_propertynames = new string[] {
            nameof(Common.WeighterC.CurrentFlow),//√
            nameof(Common.WeighterC.CumulativeProduction),//√
        };

        private void button_ratio_Click(object sender, RoutedEventArgs e)
        {
            WdRatioSet w = new WdRatioSet();
            w.Owner = FLY.ControlLibrary.COMMON.GetWindow(this);
            w.Init(weightSystemService);
            w.ShowDialog();
        }

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

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

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

}