using BBWY.Client.Helpers;
using BBWY.Client.Models.PackTask;
using BBWY.Client.ViewModels;
using BBWY.Common.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using WebSocketSharp;

namespace BBWY.Client.Views.PackerTask
{
    /// <summary>
    /// PackerWaitPackageControl.xaml 的交互逻辑
    /// </summary>
    public partial class PackerWaitPackageControl : UserControl
    {
        public PackerWaitPackageControl()
        {
            InitializeComponent();
            this.Loaded += Load;
        }
        GlobalContext globalContext;

        public void LoadShops(GlobalContext globalContext)
        {
            this.globalContext = globalContext;
            shops = globalContext.User.ShopList.Select(s => s.ShopName).ToList();
            departments = globalContext.User.DepartmentList.Select(s => s.Name).ToList();

        }

        private void Load(object sender, RoutedEventArgs e)
        {

            try
            {
                var model = (App.Current.Resources["Locator"] as ViewModelLocator).Main;

                LoadShops(model.GlobalContext);
            }
            catch
            {


            }
        }
        public string QKApiHost { get; set; }
        public void SelectionChangeCommand(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                var list = (ListBox)sender;
                if (list.Items.Count <= 0)
                {
                    return;
                }
                var value = (ListBoxItem)list.SelectedValue;
                var content = (Label)value.Content;
                tb.Text = content.Content.ToString();
                tipBox.Visibility = Visibility.Collapsed;
            }
            catch (Exception)
            {


            }

        }
        List<string> departments = new List<string>();

        List<string> shops = new List<string>();
        private void tb_TextChanged(object sender, TextChangedEventArgs e)
        {

            if (tipBox != null)
                try
                {
                    var textBoxt = (TextBox)sender;
                    //创建一个ListBox

                    if (tipBox != null && tipBox.Items.Count > 0)
                    {
                        tipBox.Items.Clear();

                    }

                    if (departments.Count <= 0)
                    {
                        if (QKApiHost.IsNullOrEmpty()) QKApiHost = "http://qiku.qiyue666.com";
                        HttpClientHelper helper = new HttpClientHelper(QKApiHost);

                        string url = $"{QKApiHost}/api/PackTask/GetAllDepartment";//获取所有数据
                        var data = helper.Get(url);

                        var res = JsonConvert.DeserializeObject<ApiResponse<UserDepartment[]>>(data);
                        //创建一个ListBoxIem
                        if (res.Success)
                        {
                            if (res.Data != null && res.Data.Any())
                            {
                                foreach (var department in res.Data)
                                {
                                    if (!departments.Contains(department.DePartmentName))
                                    {
                                        departments.Add(department.DePartmentName);
                                    }

                                }
                            }
                        }
                    }

                    if (string.IsNullOrEmpty(textBoxt.Text))
                    {
                        tipBox.Visibility = Visibility.Collapsed;
                        return;
                    }
                    foreach (var department in departments)
                    {
                        if (department.Contains(textBoxt.Text))
                        {
                            ListBoxItem item = new ListBoxItem();
                            Label lb = new Label();
                            lb.Content = department;
                            item.Content = lb;
                            tipBox.Items.Add(item);
                        }

                    }

                    tipBox.Visibility = Visibility.Visible;
                }
                catch (Exception)
                {


                }

        }

        private void tbShop_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                var textBoxt = (TextBox)sender;
                //创建一个ListBox

                if (tipBoxShop != null && tipBoxShop.Items.Count > 0)
                {
                    tipBoxShop.Items.Clear();

                }

                if (shops.Count <= 0)
                {
                    if (globalContext != null)
                        LoadShops(globalContext);
                }

                if (string.IsNullOrEmpty(textBoxt.Text))
                {
                    tipBoxShop.Visibility = Visibility.Collapsed;
                    return;
                }
                foreach (var department in shops)
                {
                    if (department.Contains(textBoxt.Text))
                    {
                        ListBoxItem item = new ListBoxItem();
                        Label lb = new Label();
                        lb.Content = department;
                        item.Content = lb;
                        tipBoxShop.Items.Add(item);
                    }

                }
                if (tipBoxShop != null)
                    tipBoxShop.Visibility = Visibility.Visible;
            }
            catch (Exception)
            {


            }
        }

        private void tipBoxShop_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                var list = (ListBox)sender;
                if (list.Items.Count <= 0)
                {
                    return;
                }
                var value = (ListBoxItem)list.SelectedValue;
                var content = (Label)value.Content;
                tbShop.Text = content.Content.ToString();
                tipBoxShop.Visibility = Visibility.Collapsed;
            }
            catch (Exception)
            {


            }
        }

        private void tb_LostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                if (tipBox != null && tipBox.Items.Count > 0)
                {
                    tipBox.Items.Clear();

                }
            }
            catch (Exception)
            {


            }
        }

        private void tbShop_LostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                if (tipBoxShop != null && tipBoxShop.Items.Count > 0)
                {
                    tipBoxShop.Items.Clear();

                }
            }
            catch (Exception)
            {


            }
        }
    }
}