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

namespace PSO2SERVER.Zone
{
    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; }//ZoneId

        [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
    {
        public uint zone_id { get; set; }
        public bool is_active { get; set; }
        public ObjectSpawnPacket data { get; set; }
        public string lua_data { get; set; }
    }

    public class ZoneData
    {
        public string Name { get; set; }
        public bool IsSpecialZone { get; set; }
        public uint ZoneId { get; set; }
        public ZoneSettings Settings { get; set; }
        public PSOLocation DefaultLocation { get; set; }
        public List<EnemySpawn> Enemies { get; set; }
        public List<ZoneChunk> Chunks { get; set; }

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

    public class ZoneChunk
    {
        public uint ZoneId { get; set; }
        public uint ChunkId { get; set; }
        public EnemySpawnType EnemySpawnType { get; set; }
        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 { }

    public class EventData
    {
        public uint zone_id { get; set; }
        public bool is_active { get; set; }
        public EventSpawnPacket data { get; set; }
        public string lua_data { get; set; }
    }

    public class NPCData
    {
        public uint zone_id { get; set; }
        public bool is_active { get; set; }
        public NPCSpawnPacket data { get; set; }
        public string lua_data { get; set; }
    }

    public class TransporterData
    {
        public uint zone_id { get; set; }
        public bool is_active { get; set; }
        public TransporterSpawnPacket data { get; set; }
        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 AutomaticEnemySpawn
    {
        public uint Min { get; set; }
        public uint Max { get; set; }

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

    public class AutomaticWithRespawnEnemySpawn : AutomaticEnemySpawn
    {
        public TimeSpan RespawnTime { get; set; }

        public AutomaticWithRespawnEnemySpawn() { }
        public AutomaticWithRespawnEnemySpawn(uint min, uint max, TimeSpan respawnTime)
            : base(min, max)
        {
            RespawnTime = respawnTime;
        }
    }
}
