using Khd.Core.EntityFramework;
using Masuit.Tools;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace Khd.Core.Wpf.TaskForm
{
    /// <summary>
    /// SelectOutRawForm.xaml 的交互逻辑
    /// </summary>
    public partial class SelectOutRawForm : Window
    {
        private readonly IHost _host;
        private readonly string _ud3;
        private readonly int _isOut;
        private List<string> _endStationCodes = new List<string>();
        public static string StationCode = "";
        public List<string> _list;
        public SelectOutRawForm(IHost host, string ud3, int isOut, List<string> list = null)
        {
            InitializeComponent();
            _host = host;
            _ud3 = ud3;
            _isOut = isOut;
            this._list = list;
        }

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

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (this.endStationCode.SelectedItem == null)
            {
                MessageBox.Show("请选择出库站点！");
            }
            else
            {
                if (_isOut == 3 || _isOut == 4)
                {
                    StationCode = this.endStationCode.SelectedItem.ToString();
                }
                using var scope = _host.Services.CreateScope();
                var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
                var baseEquip = dbContext.BaseEquip.Where(t => t.objid == 10).First();
                if (_isOut == 1)
                {
                    _endStationCodes.Remove(this.endStationCode.SelectedItem.ToString());
                    _endStationCodes.Insert(0, this.endStationCode.SelectedItem.ToString());
                    baseEquip.endStationCode = _endStationCodes.ToJsonString();
                }
                else
                {
                    baseEquip.endStationCode = new List<string> { this.endStationCode.SelectedItem.ToString() }.ToJsonString();
                }
                baseEquip.ud3 = _ud3;
                baseEquip.IsOut = _isOut;
                dbContext.Update(baseEquip);
                dbContext.SaveChanges();
                this.Close();
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (_isOut == 3)
            {
                if (_list == null || _list.Count() == 0)
                {
                    return;
                }
                this.endStationCode.ItemsSource = _list;
                foreach (var item in startStationLabel.Children)
                {
                    if (item is CheckBox cb)
                    {
                        cb.Visibility = _list.Contains(cb.Content.ToString()) ? Visibility.Visible : Visibility.Collapsed;
                    }
                }
                if (_list.Count == 0)
                {
                    StationCode = _list.First();
                    return;
                }
            }
            else
            {
                List<string> endStationCodes = SystemData.BaseEquip.Where(t => t.equipType == 10).Select(t => t.agvPositionCode).ToList();
                if (_isOut == 4)
                {
                    _endStationCodes = endStationCodes;
                    this.endStationCode.ItemsSource = _endStationCodes;
                    return;
                }
                using var scope = _host.Services.CreateScope();
                var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
                var hasTask = dbContext.WcsTask.Where(t => (t.nextPointId == 11 && t.taskType == 30) || t.nextPointId == 10).Any();
                if (!hasTask)
                {
                    List<string> list = endStationCodes;
                    if (_isOut == 1)
                    {
                        var wmsRawOutstocks = dbContext.WmsRawOutstock
                            .Where(t => t.executeStatus == "0" || t.executeStatus == "1")
                            .Where(t => endStationCodes.Contains(t.endStationCode))
                            .Where(t => t.outstockAmount > t.realOutstockAmount)
                            .ToList();
                        list = wmsRawOutstocks.Select(t => t.endStationCode).Distinct().ToList();
                    }
                    else
                    {
                        var wmsRawInstocks = dbContext.WmsRawReturn
                            .Where(t => t.executeStatus == "0" || t.executeStatus == "1")
                            .Where(t => endStationCodes.Contains(t.endStationCode))
                            .Where(t => t.planAmount > t.returnAmount)
                            .ToList();
                        list = wmsRawInstocks.Select(t => t.endStationCode).Distinct().ToList();
                    }
                    if (_isOut == 1)
                    {
                        foreach (var item in startStationLabel.Children)
                        {
                            if (item is CheckBox cb)
                            {
                                cb.Visibility = list.Contains(cb.Content.ToString()) ? Visibility.Visible : Visibility.Collapsed;
                            }
                        }
                    }
                    else
                    {
                        this.endStationCode.ItemsSource = list;
                    }
                }
                else
                {
                    MessageBox.Show("您有未完成的出库任务，请先完成后再进行此操作！");
                    this.Close();
                }
            }
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            if (sender is CheckBox cb)
            {
                if (cb.IsChecked.Value)
                {
                    _endStationCodes.Add(cb.Content.ToString());
                }
                else
                {
                    _endStationCodes.Remove(cb.Content.ToString());
                }
            }
            this.endStationCode.ItemsSource = _endStationCodes.OrderBy(t => t);
            this.endStationCode.Items.Refresh();
            //if (_endStationCodes.Count >= 1)
            //{
            //    this.endStationCode.SelectedIndex = 0;
            //}
        }

        private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
        {

        }
    }
}
