using Microsoft.Extensions.DependencyInjection;
using SlnMesnac.Model.domain;
using SqlSugar;
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 SlnMesnac.WPF.Page
{
    /// <summary>
    /// LocationPage.xaml 的交互逻辑
    /// </summary>
    public partial class LocationPage : UserControl
    {

        private readonly ISqlSugarClient? sqlSugarClient;
        public LocationPage()
        {
            InitializeComponent();
            sqlSugarClient = App.ServiceProvider.GetService<ISqlSugarClient>();
            this.WareHouseId.ItemsSource = new List<string>
            {
                "二楼仓库","三楼仓库"
                //,"五楼仓库"
            };
            WareHouseId.SelectedItem = "三楼仓库";
            loadLocations(GetLocations());
        }

        private List<WmsBaseLocation> GetLocations()
        {
            try
            {
               
                string? wareHouse = WareHouseId.SelectedItem.ToString();
                var warehouseId = new List<long>();
                if (wareHouse == "二楼仓库")
                {
                    warehouseId.Add(231);
                }
                else if (wareHouse == "三楼仓库")
                {
                    warehouseId.Add(311);
                }
                else if (wareHouse == "五楼仓库")
                {
                    warehouseId.Add(511);
                    warehouseId.Add(521);
                    warehouseId.Add(531);
                }
                List<WmsBaseLocation> list = sqlSugarClient.AsTenant().GetConnection("mes").Queryable<WmsBaseLocation>().Where(t => warehouseId.Contains(t.WarehouseId)).ToList();
               
                return list;
                
            }catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }
        }


        private void Refulsh_Click(object sender, RoutedEventArgs e)
        {
            loadLocations(GetLocations());
        }


        private void WarehouseChanged(object sender, SelectionChangedEventArgs e)
        {
            loadLocations(GetLocations());
          
        }


        private void loadLocations(List<WmsBaseLocation> wmsBaseLocations)
        {
            try
            {
                if (wmsBaseLocations == null || wmsBaseLocations.Count == 0)
                {
                    return;
                }

                List<BaseEquipment> baseEquips = new List<BaseEquipment>();
                if (wmsBaseLocations.First().WarehouseFloor == 2)
                {

                    baseEquips =  sqlSugarClient.AsTenant().GetConnection("mes").Queryable<BaseEquipment>().Where(t => t.EquipType==20).OrderBy(t => t.AgvPositionCode).ToList();

                   
                }
                else if (wmsBaseLocations.First().WarehouseFloor == 3)
                {
 
                    baseEquips = sqlSugarClient.AsTenant().GetConnection("mes").Queryable<BaseEquipment>().Where(t => t.EquipType == 15).OrderBy(t => t.AgvPositionCode).ToList();
                }


                Dispatcher.Invoke(() =>
                {
                    this.LocaltionGrid.Children.Clear();
                    this.LocaltionGrid.RowDefinitions.Clear();
                    this.LocaltionGrid.ColumnDefinitions.Clear();
                    List<long> list = wmsBaseLocations.Select(t => t.WarehouseId).Distinct().ToList();
                    int? column = 0;

                    for (var i = 0; i < list.Count; i++)
                    {
                        this.LocaltionGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1.0, GridUnitType.Star) });
                        var grid = new Grid();
                        List<WmsBaseLocation> locations = wmsBaseLocations.Where(t => t.WarehouseId == list[i]).ToList();
                        var row = locations.Max(t => t.LocRow);
                        column = locations.Max(t => t.LocColumn);


                        if (list[0] == 231 && i == 0)
                        {
                            grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1.0, GridUnitType.Star) });
                            grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1.0, GridUnitType.Star) });
                            grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1.0, GridUnitType.Star) });
                            grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1.0, GridUnitType.Star) });

                            for(int h=0; h < 25; h++)
                            {
                                var button2 = new Button()
                                {
                                    Content = "过道",
                                    Name = "road1",
                                    Width = 40,
                                    Height = 25,
                                    FontSize = 15,
                                    Margin = new Thickness(0),
                                    Background = new SolidColorBrush(Colors.Transparent),
                                    Foreground = new SolidColorBrush(Colors.White),
                                };

                                var button3 = new Button()
                                {
                                    Content = "过道",
                                    Name = "road2",
                                    Width = 40,
                                    Height = 25,
                                    FontSize = 15,
                                    Margin = new Thickness(0),
                                    Background = new SolidColorBrush(Colors.Transparent),
                                    Foreground = new SolidColorBrush(Colors.White),
                                };

                                Grid.SetColumn(button2, h );
                                Grid.SetRow(button2, 2);
                                Grid.SetColumnSpan(button2, 2);
                                grid.Children.Add(button2);


                                Grid.SetColumn(button3, h);
                                Grid.SetRow(button3, 5);
                                Grid.SetColumnSpan(button3, 2);
                                grid.Children.Add(button3);
                            }
                        }else if(list[0] == 311 && i == 0)
                        {
                            grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1.0, GridUnitType.Star) });
                            grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1.0, GridUnitType.Star) });
                            grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1.0, GridUnitType.Star) });

                            for(int h=0; h < 25; h++)
                            {
                                var button2 = new Button()
                                {
                                    Content = "过道",
                                    Name = "road1",
                                    Width = 40,
                                    Height = 25,
                                    FontSize = 15,
                                    Margin = new Thickness(0),
                                    Background = new SolidColorBrush(Colors.Transparent),
                                    Foreground = new SolidColorBrush(Colors.White),
                                };

                                var button3 = new Button()
                                {
                                    Content = "过道",
                                    Name = "road2",
                                    Width = 40,
                                    Height = 25,
                                    FontSize = 15,
                                    Margin = new Thickness(0),
                                    Background = new SolidColorBrush(Colors.Transparent),
                                    Foreground = new SolidColorBrush(Colors.White),
                                };

                                var button4 = new Button()
                                {
                                    Content = "过道",
                                    Name = "road2",
                                    Width = 40,
                                    Height = 25,
                                    FontSize = 15,
                                    Margin = new Thickness(0),
                                    Background = new SolidColorBrush(Colors.Transparent),
                                    Foreground = new SolidColorBrush(Colors.White),
                                };

                                Grid.SetColumn(button2, h );
                                Grid.SetRow(button2, 2);
                                Grid.SetColumnSpan(button2, 2);
                                grid.Children.Add(button2);


                                Grid.SetColumn(button3, h);
                                Grid.SetRow(button3, 5);
                                Grid.SetColumnSpan(button3, 2);
                                grid.Children.Add(button3);

                                Grid.SetColumn(button4, h);
                                Grid.SetRow(button4, 8);
                                Grid.SetColumnSpan(button4, 2);
                                grid.Children.Add(button4);
                            }
                        }
                        for (var j = 0; j < row+1; j++)
                        {
                            grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1.0, GridUnitType.Star) });
                        }
                        for (var j = 0; j < column; j++)
                        {
                            grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1.0, GridUnitType.Star) });
                        }
                        foreach (var location in locations)
                        {
                            var button = new Button()
                            {
                                Name = $"Location{location.LocationCode}",
                                Content = location.LocationCode,
                                Width = 50,
                                Height = 50,
                                Margin = new Thickness(2),
                                Tag = location.LocationId,
                                Background = string.IsNullOrEmpty(location.ContainerCode) ? new SolidColorBrush((Color)ColorConverter.ConvertFromString("#4789AE")) : new SolidColorBrush((Color)ColorConverter.ConvertFromString("#75F76D"))
                            };
                            
                            Grid.SetColumn(button, location.LocColumn.Value - 1);
                            if (list[0] == 231)
                            {
                                if(location.LocRow.Value <= 2)
                                {
                                    Grid.SetRow(button, -(location.LocRow.Value - row.Value) + 4);
                                }
                                else
                                {
                                    Grid.SetRow(button, -(location.LocRow.Value - row.Value) + 3);
                                }
                             


                                for (var k = 0; k < 8; k++)
                                {
                                    var button2 = new Button()
                                    {
                                        Content = baseEquips[k].AgvPositionCode + $"({baseEquips[k].EmptyCount})",
                                        Name = baseEquips[k].AgvPositionCode,
                                        Width = 100,
                                        Height = 50,
                                        FontSize = 15,
                                        Margin = new Thickness(2),
                                        Background = baseEquips[k].EmptyCount > 0 ? new SolidColorBrush((Color)ColorConverter.ConvertFromString("#75F76D")) : new SolidColorBrush((Color)ColorConverter.ConvertFromString("#4789AE")),
                                    };
                                    Grid.SetColumn(button2, k % 4 * 2 + 1);
                                    Grid.SetRow(button2, -((k / 4) - 1));
                                    Grid.SetColumnSpan(button2, 2);
                                    grid.Children.Add(button2);
                                }
                            }
                            else
                            {
                                if(location.LocRow.Value <= 2)
                                {
                                    Grid.SetRow(button, -(location.LocRow.Value - row.Value)+3);
                                }else if(location.LocRow.Value <= 4 && location.LocRow.Value > 2)
                                {
                                    Grid.SetRow(button, -(location.LocRow.Value - row.Value) +2);
                                }else if(location.LocRow.Value<=6 && location.LocRow.Value > 4)
                                {
                                    Grid.SetRow(button, -(location.LocRow.Value - row.Value)+1);
                                }
                                else
                                {
                                    Grid.SetRow(button, -(location.LocRow.Value - row.Value));
                                }
                               

                                if (list[0] == 311)
                                {
                                    for (var k = 0; k < 3; k++)
                                    {
                                        var button2 = new Button()
                                        {
                                            Content = baseEquips[k].AgvPositionCode + $"({baseEquips[k].EmptyCount})",
                                            Name = baseEquips[k].AgvPositionCode,
                                            Width = 100,
                                            Height = 50,
                                            FontSize = 15,
                                            Margin = new Thickness(2),
                                            Background = baseEquips[k].EmptyCount > 0 ? new SolidColorBrush((Color)ColorConverter.ConvertFromString("#75F76D")) : new SolidColorBrush((Color)ColorConverter.ConvertFromString("#4789AE")),
                                        };
                                        Grid.SetColumn(button2, k % 4 * 2 + 17);
                                        Grid.SetRow(button2, -((k / 4) - 1));
                                        Grid.SetColumnSpan(button2, 2);
                                        grid.Children.Add(button2);
                                    }
                                }
                            }
                            grid.Children.Add(button);
                        }
                        Grid.SetRow(grid, i);
                        this.LocaltionGrid.Children.Add(grid);
                    }
                });
            }
            catch
            {

            }

        }
    }
}
