using FLY.OBJComponents.Client;
using FLY.Weight.Client;
using FLY.Weight.Common;
using FLY.Weight.IService;
using MultiLayout;
using MultiLayout.UiModule;
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;
using Unity;

namespace FLY.Weight.UI.Client.UiModule
{
    /// <summary>
    /// Page_Main.xaml 的交互逻辑
    /// </summary>
    public partial class MainGraphV0_2 : UserControl
    {
        IWeightSystemService weightSystemService;

        List<SetPLCUpdatePlan> setPlan_items = new List<SetPLCUpdatePlan>();
        public MainGraphV0_2()
        {
            InitializeComponent();

        }

        [InjectionMethod]
        public void Init(
            IUnityContainer container,
            IWeightSystemService weightSystemService)
        {
            this.container_grid.DataContext = container;

            ucTotalFlow.Init(weightSystemService);
            this.weightSystemService = weightSystemService;
            root_grid.DataContext = this.weightSystemService;

            for (int i = 0; i < this.weightSystemService.Items.Count(); i++)
            {
                string objname = $"{nameof(IWeightSystemService.Items)}[{i}]";
                SetPLCUpdatePlan plan = new SetPLCUpdatePlan(
                    this.weightSystemService.PLCos,
                    objname,
                    UcWeighterItem.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,
                    UcWeighterItem.item_update_propertynames);
                    setPlan_items.Add(plan);
                }
            }
        }

    }



}