using FLY.OBJComponents.Client;
using FLY.Weight2.Client;
using FLY.Weight2.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.Weight2.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();
        }

        [InjectionMethod]
        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.TotalFlowSetOut),
                        nameof(Common.WeighterAccessory.TotalFlowSetIn),
                        nameof(Common.WeighterAccessory.TotalFlowSetInIsSet),
                        nameof(Common.WeighterAccessory.TotalFlow),

                        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),
        };

        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_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));
            }
        }
    }

    public class UiModule2_UcTotalFlow : IUiModule2
    {
        /// <summary>
        /// 控件标题
        /// 它的值取决于culture
        /// </summary>
        public string Title => "单组称重.总流量状态";
        public ComponentType Type => ComponentType.Graph;

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

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


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