using System.IO;
using Decimation.Content.Items.Misc.Souls;
using Decimation.Lib.NPCs;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace Decimation.Content.NPCs.DuneWyrm.AncientTombCrawler
{
    public class AncientTombCrawlerHead : AncientTombCrawler
    {
        public override void SetDefaults()
        {
            npc.lifeMax = 2500;
            npc.damage = 50;
            npc.defense = 5;
            npc.width = 38;
            npc.height = 38;
            npc.value = Item.buyPrice(gold: 3);
            npc.npcSlots = 1f;
            npc.aiStyle = -1;
            npc.boss = true;
            music = mod.GetSoundSlot(SoundType.Music, "Sounds/Music/The_Deserts_Call");
        }

        public override void Init()
        {
            base.Init();
            head = true;
        }

        public override void OnHitNPC(NPC target, int damage, float knockback, bool crit)
        {
            if (Main.rand.NextBool(20)) target.AddBuff(BuffID.Darkness, 600);
        }

        public override void OnHitPlayer(Player target, int damage, bool crit)
        {
            if (Main.rand.NextBool(20)) target.AddBuff(BuffID.Darkness, 600);
        }

        public override void NPCLoot()
        {
            Item.NewItem(npc.Center, ModContent.ItemType<SoulofTime>(), Main.rand.Next(5, 11));
        }

        public override void BossLoot(ref string name, ref int potionType)
        {
            name = "An Ancient Tomb Crawler";
            potionType = ItemID.HealingPotion;

            base.BossLoot(ref name, ref potionType);
        }

        public override bool? DrawHealthBar(byte hbPosition, ref float scale, ref Vector2 position)
        {
            scale = 1.3f; //this make the NPC Health Bar biger
            return base.DrawHealthBar(hbPosition, ref scale, ref position);
        }

        public override bool PreDraw(SpriteBatch spriteBatch, Color drawColor)
        {
            Draw("Content/NPCs/DuneWyrm/AncientTombCrawler/AncientTombCrawlerHead", spriteBatch, drawColor);

            return false;
        }
    }

    public class AncientTombCrawlerBody : AncientTombCrawler
    {
        public override void SetDefaults()
        {
            npc.width = 24;
            npc.height = 24;
            npc.damage = 35;
            npc.defense = 2;
            npc.lifeMax = 1;
            npc.dontCountMe = true;
        }

        public override bool PreDraw(SpriteBatch spriteBatch, Color drawColor)
        {
            Draw("Content/NPCs/DuneWyrm/AncientTombCrawler/AncientTombCrawlerBody", spriteBatch, drawColor);

            return false;
        }
    }

    public class AncientTombCrawlerTail : AncientTombCrawler
    {
        public override void SetDefaults()
        {
            npc.width = 20;
            npc.height = 20;
            npc.damage = 35;
            npc.defense = 2;
            npc.lifeMax = 1;
            npc.dontCountMe = true;
        }

        public override void Init()
        {
            base.Init();
            tail = true;
        }

        public override bool PreDraw(SpriteBatch spriteBatch, Color drawColor)
        {
            Draw("Content/NPCs/DuneWyrm/AncientTombCrawler/AncientTombCrawlerTail", spriteBatch, drawColor);

            return false;
        }
    }

    public abstract class AncientTombCrawler : Worm
    {
        protected const float BaseSpeed = 10f;

        public override void SetStaticDefaults()
        {
            DisplayName.SetDefault("Ancient Tomb Crawler");
        }

        public override void Init()
        {
            minLength = 8;
            maxLength = 8;
            tailType = ModContent.NPCType<AncientTombCrawlerTail>();
            bodyType = ModContent.NPCType<AncientTombCrawlerBody>();
            headType = ModContent.NPCType<AncientTombCrawlerHead>();
            speed = BaseSpeed;
            turnSpeed = 0.045f;
            npc.scale = 1.5f;
            npc.lavaImmune = true;
            npc.noGravity = true;
            npc.noTileCollide = true;
            npc.behindTiles = true;
            npc.knockBackResist = 0f;
            npc.netAlways = true;
            npc.DeathSound = SoundID.NPCDeath18;
            npc.HitSound = SoundID.NPCHit1;
            undergroundSound = mod.GetLegacySoundSlot(SoundType.Custom, "Sounds/Custom/Earthquake");
        }

        protected override void SendSoundPacket()
        {
            GetPacket(AncientTombCrawlerMessageType.UndergroundSound).Send();
        }

        private ModPacket GetPacket(AncientTombCrawlerMessageType type)
        {
            ModPacket packet = mod.GetPacket();
            packet.Write((byte) DecimationModMessageType.AncientTombCrawler);
            packet.Write(npc.whoAmI);
            packet.Write((byte) type);
            return packet;
        }

        public void HandlePacket(BinaryReader reader)
        {
            AncientTombCrawlerMessageType type = (AncientTombCrawlerMessageType) reader.ReadByte();
            switch (type)
            {
                case AncientTombCrawlerMessageType.UndergroundSound:
                    Main.PlaySound(undergroundSound, npc.Center);
                    break;
            }
        }
    }

    public enum AncientTombCrawlerMessageType
    {
        UndergroundSound
    }
}