using Khd.Core.Domain.Models;
using Khd.Core.EntityFramework;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Media;

namespace Khd.Core.Wpf
{
    public static class SystemData
    {
        public static IEnumerable<Visual> GetChildren(this Visual parent, bool recurse = true)
        {
            if (parent != null)
            {
                int count = VisualTreeHelper.GetChildrenCount(parent);
                for (int i = 0; i < count; i++)
                {
                    // Retrieve child visual at specified index value.
                    var child = VisualTreeHelper.GetChild(parent, i) as Visual;

                    if (child != null)
                    {
                        yield return child;// 聚集函数，调用到这不会立刻结束方法，而是在集合中添加元素，直到方法结束，将集合返回

                        if (recurse)
                        {
                            foreach (var grandChild in child.GetChildren(true))
                            {
                                yield return grandChild;
                            }
                        }
                    }
                }
            }
        }

        public static string _message { get; set; }

        public static object _lock = new object();

        public static string message
        {
            get
            {
                lock (_lock)
                {
                    return _message;
                }
            }
            set
            {
                lock (_lock)
                {
                    _message = value;
                }
            }
        }

        public static bool _isUpdate { get; set; }

        public static object _updateLock = new object();

        public static bool isUpdate
        {
            get
            {
                lock (_updateLock)
                {
                    return _isUpdate;
                }
            }
            set
            {
                lock (_updateLock)
                {
                    _isUpdate = value;
                }
            }
        }

        public static List<BasePlcpoint> BasePlcpoints { get; set; }
        public static List<BaseEquip> BaseEquip { get; internal set; }
        public static List<WmsBaseWarehouse> BaseWareHouse { get; internal set; }
        public static List<MesBaseMaterialInfo> MesBaseMaterialInfo { get; set; }

        public static List<BaseDictionary> BaseDictionary = new List<BaseDictionary>();
        public static List<PlcConfig> PlcConfigs = new List<PlcConfig>();
        public static Dictionary<int, Plc.S7.Plc> PlcDic = new Dictionary<int, Plc.S7.Plc>();

        public static readonly Dictionary<string, string> LocationStatusDic = new Dictionary<string, string>()
        {
            {"1","正常" },
            {"2","自动锁定" },
            {"3","人工锁定" },
            {"4","移库锁定" },
            {"5","合库锁定" },
            {"6","出库锁定" },
            {"8","因外侧库位异常导致内侧库位无法使用" },
            {"9","异常库位" },
        };

        /// <summary>
        /// 报警库位
        /// </summary>
        public static void AlarmLocation(WmsBaseLocation wmsBaseLocation, DefaultDbContext dbContext)
        {
            if (wmsBaseLocation.locDeep == 2)
            {
                WmsBaseLocation? wmsLocation = dbContext.WmsBaseLocation.Where(t => t.locDeep == 1 && t.locRow == wmsBaseLocation.locRow && t.locColumn == (wmsBaseLocation.locColumn % 2 == 0 ? wmsBaseLocation.locColumn - 1 : wmsBaseLocation.locColumn + 1)).FirstOrDefault();
                if (wmsLocation != null&&wmsLocation.locationStatus!="3")
                {
                    wmsLocation.locationStatus = "8";
                    wmsLocation.ExceptionDesc = "外侧库位异常导致内测库位异常";
                    dbContext.Update(wmsLocation);
                }
            }
        }

        /// <summary>
        /// 解除报警库位
        /// </summary>
        public static void UnAlarmLocation(WmsBaseLocation wmsBaseLocation, DefaultDbContext dbContext)
        {
            if (wmsBaseLocation.locDeep == 2)
            {
                WmsBaseLocation? wmsLocation = dbContext.WmsBaseLocation.Where(t => t.locDeep == 1 && t.locRow == wmsBaseLocation.locRow && t.locColumn == (wmsBaseLocation.locColumn % 2 == 0 ? wmsBaseLocation.locColumn - 1 : wmsBaseLocation.locColumn + 1)).FirstOrDefault();
                if (wmsLocation != null && wmsLocation.locationStatus == "8")
                {
                    wmsLocation.locationStatus = "1";
                    dbContext.Update(wmsLocation);
                }
            }
        }
    }
}
