using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using SimpleLite.RCS;
using SimpleLite.RCS.CarTypes;
using SimpleCore;
using Newtonsoft.Json;
using SimpleLite.CADTools;
using SimpleLite.Props;
using SimpleLite.UI;
using SimpleCore.Library;
using System.Threading.Tasks;
using System.Diagnostics;
using SimpleCore.Traffic;
using SimpleCore.PropType;
using System.Net.Http;
using System.Reflection;
using LessokajiWeaverUtilities.Utilities;
using SimpleLite.RCS.Signal;


namespace StandardScene.Scheduler
{
    [MissionType(Name = "锁点上传迷毂", editor = typeof(NodeIsEnableMission))]
    [I18N.DocumentTranslation(Name = "NodeState Upload Mission", locale = "en")]
    public class NodeIsEnableMission : Mission
    {
        public static Mission Create()
        {
            return new NodeIsEnableMission();
        }

        public int[] stringToInt(string str)
        {
            return str.Split(' ').Select(s => (int)Convert.ToInt16(s, 10)).ToArray();
        }

        //是否连接成功
        public bool iscon { get; set; }


        public class NOdeIsEnableMissionStatus : MissionStatus
        {
            public static List<string> staions = new List<string>();
           
            public float readOnceTime = 0;
            public float readAllDataTime = 0;
            public float writeOnceTime = 0;
            public float writeAllDataTime = 0;


        }

        public override MissionStatus status { get; set; } = new NOdeIsEnableMissionStatus();

     
        [JsonIgnore] public Thread myThread;

        [JsonIgnore] private bool started = false;

        [JsonIgnore] private int iterationComm = 0;


        [JsonIgnore] private object sync = new();
        [JsonIgnore] public static HttpClient hc = new HttpClient() { Timeout = TimeSpan.FromSeconds(2) };

        [MethodMember(Name = "启动进程", Description = "开始处理上下料队列")]
        public override void Execute()
        {
            started = true;
            Task.Factory.StartNew(() => {
                HttpPostData httpPostData = new HttpPostData();
                 Dictionary<string, Dictionary<string, bool>> Allsignal = new Dictionary<string, Dictionary<string, bool>>();
                //Dictionary<string, bool> stationSignals = new Dictionary<string, bool>();
                while (started)
                {
                    try
                    {
                        //获取所有锁点的车
                        var sites = SimpleLib
                                   .GetAllSites()
                                   .Where(site => site.tags.Contains("unavailable"))
                                   .ToList();
                        HashSet<int> staions = new HashSet<int>();
                        foreach (var site in sites)
                        {
                            staions.Add(site.id);
                            if (site.fields.ContainsKey("mustFree")&& site.mustFree !=null && site.mustFree.Length>0)
                            {
                               //遍历数组添加禁用点
                               for (int i = 0; i < site.mustFree.Length; i++)
                               {
                                    staions.Add(site.mustFree[i]) ;

                               }
                          
                            }
                        }      
                        Diagnosis.Log($"向迷毂提供禁用站点", "siteIsEnable", true);                     
                        httpPostData.UploadListNode(staions);
                        
                       
                        Thread.Sleep(500);
                    }
                    catch (Exception e)
                    {

                        Diagnosis.Post($"{ExceptionFormatter.FormatEx(e)}", "禁用站点");
                    }
                 
                }


            });
        }

        [MethodMember(Name = "关闭机构进程", Description = "关闭机构进程")]
        public void Stop()
        {
            started = false;
            status.status = $"已停止-循环";
        }


        public static string GetListsAsStrings(object obj)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            Type type = obj.GetType();
            FieldInfo[] fieldInfo= type.GetFields();
            foreach (FieldInfo property in fieldInfo)
            {
                if (property.FieldType== typeof(List<bool>))
                {
                    List<bool> list = (List<bool>)property.GetValue(obj);
                    result[property.Name] = String.Join(",", list.Select(b => b ? "1" : "0"));
                    
                }
            }
            return JsonConvert.SerializeObject(result);
        }

    }
}
