using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using PSO2SERVER.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PSO2SERVER.Json
{
    // JSON 序列化和反序列化的自定义处理
    public class FixedListConverter<T> : JsonConverter<FixedList<T>>
    {
        // 读取 JSON 转换为 FixedList<T>
        public override FixedList<T> ReadJson(JsonReader reader, Type objectType, FixedList<T> existingValue, bool hasExistingValue, JsonSerializer serializer)
        {
            var list = serializer.Deserialize<List<T>>(reader);
            var fixedList = new FixedList<T>(list.Count);

            // 将 list 中的元素添加到 FixedList 中
            fixedList.SetListItem(list);

            return fixedList;
        }

        // 将 FixedList<T> 转换为 JSON
        public override void WriteJson(JsonWriter writer, FixedList<T> value, JsonSerializer serializer)
        {
            var list = new List<T>(value.Length);
            for (int i = 0; i < value.Length; i++)
            {
                list.Add(value[i]);
            }
            serializer.Serialize(writer, list);
        }
    }
    public class EnumJsonConverter
    {
        public static string EnumToJson<T>(T enumValue)
        {
            // 序列化枚举值为 JSON 字符串
            return JsonConvert.SerializeObject(enumValue, Formatting.Indented, new StringEnumConverter());
        }

        public static T JsonToEnum<T>(string json)
        {
            // 反序列化 JSON 字符串为枚举值
            return JsonConvert.DeserializeObject<T>(json);
        }
    }
    public class QuestPartyTypeConverter : JsonConverter<QuestPartyType>
    {
        public override void WriteJson(JsonWriter writer, QuestPartyType value, JsonSerializer serializer)
        {
            // Optionally, you can use the enum name or custom string
            writer.WriteValue(value.ToString());
        }

        public override QuestPartyType ReadJson(JsonReader reader, Type objectType, QuestPartyType existingValue, bool hasExistingValue, JsonSerializer serializer)
        {
            // Read the string and convert it back to the enum value
            var value = reader.Value.ToString();
            return Enum.TryParse(value, out QuestPartyType result) ? result : QuestPartyType.Solo; // Default to Solo if parsing fails
        }
    }

    public class ObjectTypeConverter : JsonConverter<ObjectType>
    {
        public override ObjectType ReadJson(JsonReader reader, Type objectType, ObjectType existingValue, bool hasExistingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.String)
            {
                string value = reader.Value.ToString();
                ObjectType objectTypeResult;

                switch (value)
                {
                    case "Player":
                        objectTypeResult = ObjectType.Player;
                        break;
                    case "Map":
                        objectTypeResult = ObjectType.Map;
                        break;
                    case "Object":
                        objectTypeResult = ObjectType.Object;
                        break;
                    case "StaticObject":
                        objectTypeResult = ObjectType.StaticObject;
                        break;
                    case "PartyQuest":
                        objectTypeResult = ObjectType.Quest;
                        break;
                    case "Party":
                        objectTypeResult = ObjectType.Party;
                        break;
                    case "World":
                        objectTypeResult = ObjectType.World;
                        break;
                    case "APC":
                        objectTypeResult = ObjectType.APC;
                        break;
                    default:
                        objectTypeResult = ObjectType.Unknown; // 默认返回 Unknown
                        break;
                }

                return objectTypeResult;
            }

            if (reader.TokenType == JsonToken.Integer)
            {
                // 如果是数字类型，直接转换为枚举
                return (ObjectType)reader.Value;
            }

            return ObjectType.Unknown; // 默认返回 Unknown
        }

        public override void WriteJson(JsonWriter writer, ObjectType value, JsonSerializer serializer)
        {
            string stringValue;

            switch (value)
            {
                case ObjectType.Player:
                    stringValue = "Player";
                    break;
                case ObjectType.Map:
                    stringValue = "Map";
                    break;
                case ObjectType.Object:
                    stringValue = "Object";
                    break;
                case ObjectType.StaticObject:
                    stringValue = "StaticObject";
                    break;
                case ObjectType.Quest:
                    stringValue = "PartyQuest";
                    break;
                case ObjectType.Party:
                    stringValue = "Party";
                    break;
                case ObjectType.World:
                    stringValue = "World";
                    break;
                case ObjectType.APC:
                    stringValue = "APC";
                    break;
                case ObjectType.Unknown:
                    stringValue = "Unknown";
                    break;
                default:
                    stringValue = "Undefined";
                    break;
            }

            writer.WriteValue(stringValue);
        }
    }

    // Custom converter for AsciiString, assuming it's a fixed length string.
    public class AsciiStringConverter : JsonConverter
    {
        public override bool CanConvert(System.Type objectType)
        {
            return objectType == typeof(string);
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            string str = value as string;
            if (str != null)
            {
                writer.WriteValue(str.PadRight(32, ' ')); // Ensure the string is 0x20 (32) bytes long.
            }
            else
            {
                writer.WriteNull();
            }
        }

        public override object ReadJson(JsonReader reader, System.Type objectType, object existingValue, JsonSerializer serializer)
        {
            string str = (string)reader.Value;
            return str?.TrimEnd(); // Removing padding spaces
        }
    }
}
