using LessokajiWeaverUtilities.Utilities;
using Newtonsoft.Json;
using Simple3.RCS;
using Simple3.RCS.CarTypes;
using SimpleCore;
using SimpleCore.Compiler;
using SimpleCore.PropType;
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using StandardScene.Utils;

namespace StandardScene.CarTypes
{
    [CarType(Name = "锂电双举升")]
    [I18N.DocumentTranslation(Name = "Dual Lift Car", locale = "en")]
    public class DualLiftingCar : GhostCar
    {
        public static async Task<DualLiftingCar> Create()
        {
            var car = new DualLiftingCar()
            {
                lstatus = "连接中",
                address = "127.0.0.1",
                name = "锂电双举升"
            };
            return car;
        }
        public override string SetDisplayInfo()
        {
            try
            {
                var str = $"{name}({id})\n";
                status.enums.TryGetValue("ChassisMode", out var manual);
                var ms = manual == "0" ? "自动" : "手动";
                status.enums.TryGetValue("Soc", out var soc);
                str = $"{str}{ms}|soc:{soc}";
                var alarmStr = Commons.GetCarStatus(this, "AlarmInfo");
                if (alarmStr != "")
                    str = $"{str}|{alarmStr}";
                if (status.enums.TryGetValue("ElectricCurrent", out var electricCurrent) &&
                    !string.IsNullOrEmpty(electricCurrent))
                {
                    var charging = float.Parse(electricCurrent) * 0.01f > 0 ? "充电中" : "未充电";
                    str = $"{str}|charge:{charging}";
                }

                var thresSpeed = Commons.CarValue(this, "ThresSpeed");
                str = $"{str}{((thresSpeed <= 0) ? "|避障触发" : "")}";
                return str;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return "bad car";
            }
        }

        [MethodMember(Name = "进入小车管理界面", Description = "在浏览器中打开小车 Web 页面")]
        [I18N.DocumentTranslation(Name = "Open vehicle management page", Description = "Open the car's management page in browser", locale = "en")]
        public void OpenVehicleManagementPage()
        {
            CarRemoteHelper.OpenVehicleWebPage(address);
        }

        private bool _running;
        [MethodMember(Name = "测试")]
        [I18N.DocumentTranslation(Name = "Start test", locale = "en")]
        public async void Test()
        {
            _running = true;
            var siteA = SimpleLib.GetAllSites().First(s => s.name == "A");
            var siteB = SimpleLib.GetAllSites().First(s => s.name == "B");
            while (_running)
            {
                var planA = new SegmentPlan() { usingCar = this };
                planA.FindRoute(SimpleLib.GetSite(GetLastSite()), siteA);
                await planA.Compile("A").Queue();
                var planB = new SegmentPlan(){usingCar = this };
                planB.FindRoute(siteA, siteB);
                await planB.Compile("B").Queue();
                Thread.Sleep(1000);
            }
        }

        [MethodMember(Name = "停止测试")]
        [I18N.DocumentTranslation(Name = "Stop test", locale = "en")]
        public void Stop()
        {
            _running = false;
        }
    }
}
