using FLY.OBJComponents.Client;
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;

namespace FLY.Weight.UI.Client.UIModule
{
    /// <summary>
    /// UcWeighterItem.xaml 的交互逻辑
    /// </summary>
    public partial class UcWeighterItem : UserControl
    {
        FLY.Weight.Client.WeightSystemClient mWeighterCsService;
        List<SetPLCUpdatePlan> setPlan_items = new List<SetPLCUpdatePlan>();
        int Index = 0;

        public UcWeighterItem()
        {
            InitializeComponent();
        }
        public void Init(int id)
        {
            mWeighterCsService = TDGage.Current.mWeighterCsService;

            //查找参数
            if (WeighterItemParams.Current.Indexs.ContainsKey(id))
            {
                Index = WeighterItemParams.Current.Indexs[id];
            }
            else 
            {
                for (int i = 0; i < mWeighterCsService.Items.Count(); i++) 
                {
                    if (!WeighterItemParams.Current.Indexs.ContainsValue(i)) 
                    {
                        WeighterItemParams.Current.Indexs.Add(id, i);
                        Index = i;
                        break;
                    }
                }
            }

            int idx = Index;
            if (idx >= mWeighterCsService.Items.Count())
                idx = mWeighterCsService.Items.Count() - 1;
            else if (idx < 0)
                idx = 0;

            this.DataContext = mWeighterCsService.Items[idx];

            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[] {
           // "CumulativeProduction",
            "FlowSetting",//√
            "CurrentFlow",//√
            "ScrewPDisp",//√
            "ScrewPSet",//√
            "ScrewPDisp",//√
            "MixBucketWeight",//√
            "BinWeight",//√
            "MixIsOn",//√
            "BucketValveIsOpen",//√
            "ScrewMotorFreq",//√
            "ScrewMotorIsOn",//√
            "ScrewIsAutoMode",//√
            "ScrewManualFreq"//√
        };

        private async void button_frequency_Click(object sender, RoutedEventArgs e)
        {
            Window_Frequency w = new Window_Frequency();
            var 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;
                weight.ScrewManualFreqIsSet = true;
                //不用写下降沿!!!!!
            }
        }

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

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

    /// <summary>
    /// 全部扫描图的参数
    /// </summary>
    public class WeighterItemParams 
    {
        public Dictionary<int, int> Indexs = new Dictionary<int, int>();
        public WeighterItemParams()
        {
        }
        public static WeighterItemParams Current { get; } = new WeighterItemParams();
    }

    public class UIModule_WeighterItem : 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)
        {
            UcWeighterItem graph = new UcWeighterItem();
            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)
        {
        }
    }
}