using MWS.Data.InterFace;
using MWS.Data.Manager;
using MWS.Model;
using MWSDomain.Common.CoreLib;
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.Shapes;

namespace MWS.Domain.View.History
{
    /// <summary>
    /// SelectVarCodeWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SelectVarCodeWindow : Window
    {
        private ITag tagManager;
        private IDevice deviceManage;
        private List<T_DeviceTag> deviceTags;
        public List<T_DeviceTag> selectedTags;
        private List<T_DeviceTag> tempTags;

        private string keyWords = "";
        private int deviceId = 0;
        public SelectVarCodeWindow(int deviceId)
        {
            InitializeComponent();
            tagManager = new TagManager();
            deviceManage = new DeviceManager();
            selectedTags = new List<T_DeviceTag>();
            tempTags = new List<T_DeviceTag>();
            this.deviceId = deviceId;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            BindDeviceComboBox();
            BindData();
        }

        private void search_Click(object sender, RoutedEventArgs e)
        {

            BindData();
        }
        public void BindData()
        {
            keyWords = tbkeyword.Text.Trim();
            deviceId = Convert.ToInt32(device.SelectedValue);
            deviceTags = tagManager.GetTagsByKeyWord(keyWords, deviceId);
            if (selectedTags!=null||selectedTags.Count==0)
            {
                foreach (var item in selectedTags)
                {
                    var varwhereRemove = deviceTags.FirstOrDefault(t => t.Id == item.Id);
                    deviceTags.Remove(varwhereRemove);
                }
            }

            dglist.ItemsSource = deviceTags;
        }
        /// <summary>
        /// 获取车辆
        /// </summary>
        /// <param name="selectIndex"></param>
        private void BindDeviceComboBox()
        {
            try
            {
                List<T_Device> devices = deviceManage.GetAll();

                List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();

                foreach (var item in devices)
                {
                    list.Add(new KeyValuePair<string, string>(item.Id.ToString(), item.Name));
                }
                list.Insert(0, new KeyValuePair<string, string>("0", "全部"));

                device.ItemsSource = list;
                device.SelectedValuePath = "Key";
                device.DisplayMemberPath = "Value";
                device.SelectedIndex = deviceId;
            }
            catch (Exception ex)
            {
                List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>
                {
                     new KeyValuePair<string, string>("0", "全部"),
                    new KeyValuePair<string, string>("1", "1号车"),
                    new KeyValuePair<string, string>("2", "2号车"),
                    new KeyValuePair<string, string>("3", "3号车")
                 };
                device.ItemsSource = list;
                device.SelectedValuePath = "Key";
                device.DisplayMemberPath = "Value";
                device.SelectedIndex = deviceId;
            }

        }

        private void close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void select_Click(object sender, RoutedEventArgs e)
        {
            //if (dglist.SelectedItems.Count ==0)
            //{
            //    Msg.Info("请选择至少一个变量！");
            //    //return;
            //}
            //else if (dglist.SelectedItems.Count > 6)
            //{
            //    Msg.Info("最多选择六个变量！");
            //    this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            //    //return;
            //}
            //else
            //{
            //    this.selectedTags = dglist.SelectedItems.Cast<T_DeviceTag>().ToList();
            //    this.Close();
            //}
            this.Close();
            //this.SelectedVar = lvVar.SelectedItems[0] as VarDict;
        }

        private void addVarCode_Click(object sender, RoutedEventArgs e)
        {
            if (dglist.SelectedItems.Count == 0)
            {
                //Msg.Info("请选择至少一个变量！");
                MessageBox.Show("请选择至少一个变量!");
                return;
            }
            else if (dglist.SelectedItems.Count > 6)
            {
                //Msg.Info("最多选择六个变量！");
                MessageBox.Show("最多选择六个变量!");
                //this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                return;
            }
            else
            {
                //selectedTags.Add

                tempTags=dglist.SelectedItems.Cast<T_DeviceTag>().ToList();
                foreach (var item in tempTags)
                {
                    deviceTags.Remove(item);
                    if (!selectedTags.Contains(item))
                    {
                        selectedTags.Add(item);
                    }
                }
                if (selectedTags.Count>6)
                {
                    selectedTags.RemoveRange(6, selectedTags.Count-6);
                    //Msg.Info("最多选择六个变量！");
                    MessageBox.Show("最多选择六个变量!");
                    return;
                    //this.ShowDialog();
                }
                deviceTags = deviceTags.ToList();
                selectedTags = selectedTags.ToList();
                dglist.ItemsSource = deviceTags;
                dglistSelect.ItemsSource = selectedTags;

                
            }

        }

        private void delectVarCode_Click(object sender, RoutedEventArgs e)
        {
            if (dglistSelect.SelectedItems.Count == 0)
            {
                //Msg.Info("请选择至少一个变量！");
                MessageBox.Show("请选择至少一个变量!");
                //return;
            }
            else if (dglistSelect.SelectedItems.Count > 6)
            {
                //Msg.Info("最多选择六个变量！");
                MessageBox.Show("最多选择六个变量!");
                this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                //return;
            }
            else
            {
                //selectedTags.Add

                tempTags = dglistSelect.SelectedItems.Cast<T_DeviceTag>().ToList();
                foreach (var item in tempTags)
                {
                    selectedTags.Remove(item);
                    if (!deviceTags.Contains(item))
                    {
                        deviceTags.Add(item);
                    }
                }
                //if (selectedTags.Count > 6)
                //{
                //    selectedTags.RemoveRange(6, selectedTags.Count - 1);
                //}
                deviceTags = deviceTags.OrderBy(c=>c.Id).ToList();
                selectedTags = selectedTags.ToList();
                dglist.ItemsSource = deviceTags;
                dglistSelect.ItemsSource = selectedTags;


            }
        }
    }
}
