using System;
using System.Collections.Generic;
using System.Linq;
using SimpleCore.Library;
using SimpleLite.RCS.CarTypes;
using StandardScene.Signal.Plc;

namespace StandardScene.Signal.Logic
{
    /// <summary>
    /// 对齐反编译 DefaultPlugin.RateFlowControl。
    /// 独立类型用分号：区域;车组;限额;点1,点2。JGControl 第三段用短横：区域-车组-限额-点1,点2。
    /// </summary>
    public static class RateFlowHandler
    {
        public static void OnStopped(Car car, SignalStopSpec spec, IDictionary<string, int> logByArea)
        {
            if (AllowPass(car, spec.SignalParams, logByArea, sep: ';'))
            {
                SignalCarAdapter.StopCharge(car);
                SignalCarAdapter.Start(car);
            }
        }

        public static bool AllowPass(Car car, string rateFlowString, IDictionary<string, int> logByArea) =>
            AllowPass(car, rateFlowString, logByArea, sep: '-');

        private static bool AllowPass(Car car, string raw, IDictionary<string, int> logByArea, char sep)
        {
            try
            {
                var parts = (raw ?? "").Split(sep);
                var area = parts.Length > 0 ? parts[0] : "";
                var group = parts.Length > 1 ? parts[1] : "";
                var limit = 0;
                if (parts.Length > 2)
                    int.TryParse(parts[2], out limit);
                var sites = new List<int>();
                if (parts.Length > 3)
                {
                    foreach (var s in parts[3].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        if (int.TryParse(s.Trim(), out var id))
                            sites.Add(id);
                    }
                }

                var others = SignalCarAdapter.CarsAt(sites)
                    .Where(x => x.id != car.id && string.Equals(SignalCarAdapter.GroupId(x), group, StringComparison.Ordinal))
                    .ToList();
                var count = others.Count;
                if (count < limit)
                {
                    Diagnosis.Log(
                        $"限流区域{area}限制AGV组{group}流量{limit}辆AGV，当前停放{count}，放行AGV{car.id}",
                        "RateFlow", true);
                    Note(logByArea, area, car, raw);
                    return true;
                }

                Diagnosis.Log(
                    $"限流区域{area}限制AGV组{group}流量{limit}辆AGV，当前停放{count}，无法放行",
                    "RateFlow", true);
                Note(logByArea, area, car, raw);
                return false;
            }
            catch (Exception ex)
            {
                Diagnosis.Log($"处理区域限流业务异常：{ex.Message}", "RateFlow", true);
                return true;
            }
        }

        private static void Note(IDictionary<string, int> logByArea, string area, Car car, string raw)
        {
            if (logByArea == null) return;
            var changed = !logByArea.TryGetValue(area, out var last) || last != car.id;
            logByArea[area] = car.id;
            if (changed)
                Diagnosis.Log($"AGV{car.id}停在地标{SignalCarAdapter.PositionId(car)}，配置参数：【{raw}】", "RateFlow", true);
        }
    }
}
