using SimpleCore.PropType;
using System;
using System.Collections.Generic;
using System.Globalization;

namespace StandardScene.Magnetic.Tasking
{
    internal sealed class Fass2TrackMotionData
    {
        public float Speed { get; set; } = 0.2f;
        public bool Reverse { get; set; }
        public float CarDirectionBias { get; set; }
        public bool EnableCarAbsoluteDirection { get; set; }
        public float CarAbsoluteDirection { get; set; }
        public byte? Byroad { get; set; }
        public byte? Direction { get; set; }
        public byte? Orientation { get; set; }
    }

    internal static class Fass2TrackFieldReader
    {
        public static Fass2TrackMotionData Read(Track track)
        {
            var data = new Fass2TrackMotionData();
            if (track?.fields == null)
            {
                return data;
            }

            if (track.fields.TryGetValue("Speed", out var speedText) &&
                float.TryParse(speedText, NumberStyles.Float, CultureInfo.InvariantCulture, out var speed))
            {
                data.Speed = speed;
            }

            if (track.fields.TryGetValue("Reverse", out var reverseText) &&
                bool.TryParse(reverseText, out var reverse))
            {
                data.Reverse = reverse;
            }

            if (track.fields.TryGetValue("CarDirectionBias", out var biasText) &&
                float.TryParse(biasText, NumberStyles.Float, CultureInfo.InvariantCulture, out var bias))
            {
                data.CarDirectionBias = bias;
            }

            if (track.fields.TryGetValue("EnableCarAbsoluteDirection", out var enableAbsText) &&
                bool.TryParse(enableAbsText, out var enableAbs))
            {
                data.EnableCarAbsoluteDirection = enableAbs;
            }

            if (track.fields.TryGetValue("CarAbsoluteDirection", out var absText) &&
                float.TryParse(absText, NumberStyles.Float, CultureInfo.InvariantCulture, out var absDir))
            {
                data.CarAbsoluteDirection = absDir;
            }

            data.Byroad = TryReadByte(track.fields, "Fass2_Byroad");
            if (data.Byroad == null && track.fields.TryGetValue("magSelect", out var magSelect) &&
                byte.TryParse(magSelect, NumberStyles.Integer, CultureInfo.InvariantCulture, out var magValue))
            {
                data.Byroad = magValue;
            }

            data.Direction = TryReadByte(track.fields, "Fass2_Direction");
            data.Orientation = TryReadByte(track.fields, "Fass2_Orientation");
            return data;
        }

        private static byte? TryReadByte(Dictionary<string, string> fields, string key)
        {
            if (!fields.TryGetValue(key, out var text) || string.IsNullOrWhiteSpace(text))
            {
                return null;
            }

            return byte.TryParse(text, NumberStyles.Integer, CultureInfo.InvariantCulture, out var value)
                ? value
                : (byte?)null;
        }
    }
}
