using Newtonsoft.Json;
using Org.BouncyCastle.Asn1.Pkcs;
using PSO2SERVER.Protocol.Packets;
using PSO2SERVER.Zone;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PSO2SERVER.Json
{
    public class MapData
    {
        [JsonProperty("map_data")]
        public LoadingLevelPacket Mapdata { get; set; }

        [JsonProperty("objects")]
        public List<ObjectData> Objects { get; set; }

        [JsonProperty("events")]
        public List<EventData> Events { get; set; }

        [JsonProperty("npcs")]
        public List<NPCData> Npcs { get; set; }

        [JsonProperty("transporters")]
        public List<TransporterData> Transporters { get; set; }

        [JsonProperty("luas")]
        public Dictionary<string, string> Luas { get; set; }

        [JsonProperty("init_map")]
        public uint InitMap { get; set; }//zone_id

        [JsonProperty("zones")]
        public List<ZoneData> Zones { get; set; }

        // 默认构造函数
        public MapData()
        {
            // 使用默认值初始化字段
            Mapdata = new LoadingLevelPacket();
            Objects = new List<ObjectData>();
            Events = new List<EventData>();
            Npcs = new List<NPCData>();
            Transporters = new List<TransporterData>();
            Luas = new Dictionary<string, string>();
            InitMap = new uint();
            Zones = new List<ZoneData>();
        }
    }

    public class ObjectData
    {
        [JsonProperty("zone_id")]
        public uint zone_id { get; set; }
        [JsonProperty("is_active")]
        public bool is_active { get; set; }
        [JsonProperty("data")]
        public ObjectSpawnPacket data { get; set; }
        [JsonProperty("lua_data")]
        public string lua_data { get; set; }
    }

    public class EventData
    {
        [JsonProperty("zone_id")]
        public uint zone_id { get; set; }
        [JsonProperty("is_active")]
        public bool is_active { get; set; }
        [JsonProperty("data")]
        public EventSpawnPacket data { get; set; }
        [JsonProperty("lua_data")]
        public string lua_data { get; set; }
    }

    public class NPCData
    {
        [JsonProperty("zone_id")]
        public uint zone_id { get; set; }
        [JsonProperty("is_active")]
        public bool is_active { get; set; }
        [JsonProperty("data")]
        public NPCSpawnPacket data { get; set; }
        [JsonProperty("lua_data")]
        public string lua_data { get; set; }
    }

    public class TransporterData
    {
        [JsonProperty("zone_id")]
        public uint zone_id { get; set; }
        [JsonProperty("is_active")]
        public bool is_active { get; set; }
        [JsonProperty("data")]
        public TransporterSpawnPacket data { get; set; }
        [JsonProperty("lua_data")]
        public string lua_data { get; set; }
    }

    public class EnemySpawn
    {
        public string EnemyName { get; set; }
        public uint SpawnCategory { get; set; }

        // 默认构造函数
        public EnemySpawn()
        {
            EnemyName = string.Empty;
            SpawnCategory = 0;
        }

        // 带参构造函数
        public EnemySpawn(string enemyName, uint spawnCategory)
        {
            EnemyName = enemyName;
            SpawnCategory = spawnCategory;
        }
    }

    public class ZoneData
    {
        [JsonProperty("name")]
        public string Name { get; set; }
        [JsonProperty("is_special_zone")]
        public bool Is_special_zone { get; set; }
        [JsonProperty("zone_id")]
        public uint Zone_id { get; set; }
        [JsonProperty("settings")]
        public ZoneSettings Settings { get; set; }
        [JsonProperty("default_location")]
        public PSOLocation Default_location { get; set; }
        [JsonProperty("enemies")]
        public List<EnemySpawn> Enemies { get; set; }
        [JsonProperty("chunks")]
        public List<ZoneChunk> Chunks { get; set; }

        // 默认构造函数
        public ZoneData()
        {
            Name = string.Empty;
            Is_special_zone = false;
            Zone_id = new uint();
            Settings = new ZoneSettings();
            Default_location = new PSOLocation();
            Enemies = new List<EnemySpawn>();
            Chunks = new List<ZoneChunk>();
        }
    }

    public class ZoneChunk
    {
        [JsonProperty("zone_id")]
        public uint ZoneId { get; set; }
        [JsonProperty("chunk_id")]
        public uint ChunkId { get; set; }
        [JsonProperty("enemy_spawn_type")]
        public EnemySpawnType EnemySpawnType { get; set; }
        [JsonProperty("enemy_spawn_points")]
        public List<PSOLocation> EnemySpawnPoints { get; set; }

        // 默认构造函数
        public ZoneChunk()
        {
            ZoneId = new uint();
            ChunkId = 0;
            EnemySpawnType = new Disabled();
            EnemySpawnPoints = new List<PSOLocation>();
        }
    }

    public abstract class EnemySpawnType
    {
        // 基类方法，用来获取默认值
        public static EnemySpawnType Default => new Disabled();
    }

    public class Disabled : EnemySpawnType { }

    public class Automatic : EnemySpawnType
    {
        public uint Min { get; set; }
        public uint Max { get; set; }

        public Automatic(uint min, uint max)
        {
            Min = min;
            Max = max;
        }
    }

    public class AutomaticWithRespawn : EnemySpawnType
    {
        public uint Min { get; set; }
        public uint Max { get; set; }
        public TimeSpan RespawnTime { get; set; }

        public AutomaticWithRespawn(uint min, uint max, TimeSpan respawnTime)
        {
            Min = min;
            Max = max;
            RespawnTime = respawnTime;
        }
    }

    public class Manual : EnemySpawnType { }
}
