using System.Collections.Generic;
using Turbo.Plugins.Default;
using System.Linq;
using System;
 
namespace Turbo.Plugins.User
{
 
    public class EliteMarkPlugin : BasePlugin, IInGameWorldPainter
    {
 
        public Dictionary<MonsterAffix, WorldDecoratorCollection> AffixDecorators { get; set; }
        public Dictionary<MonsterAffix, string> CustomAffixNames { get; set; }
        public WorldDecoratorCollection RareDecorator { get; set; }
        public WorldDecoratorCollection ChampionDecorator { get; set; }
        public WorldDecoratorCollection JuggernautDecorator { get; set; }
        public WorldDecoratorCollection GoblinDecorator { get; set; } // Goblin
        public WorldDecoratorCollection RareMinionDecorator { get; set; }   // Elite Minion
        public WorldDecoratorCollection UniqueDecorator { get; set; }   //Purple
        public WorldDecoratorCollection BossDecorator { get; set; }   //Boss
 
        public EliteMarkPlugin()
        {
            Enabled = true;
        }
 
        public override void Load(IController hud)
        {
            base.Load(hud);
            var shadowBrush = Hud.Render.CreateBrush(96, 0, 0, 0, 1);
           
            GoblinDecorator = new WorldDecoratorCollection(
                new GroundCircleDecorator(Hud) {
                    Brush = Hud.Render.CreateBrush(255, 57, 194, 29, 3),
                    Radius = 3 
                },
                new GroundCircleDecorator(Hud) {
                    Brush = Hud.Render.CreateBrush(255, 240, 213, 10, 5),
                    Radius = 2 
                },
                new GroundCircleDecorator(Hud) {
                    Brush = Hud.Render.CreateBrush(180, 255, 0, 0, 6),
                    Radius = 0.3f  
                }
            );
           
            RareDecorator = new WorldDecoratorCollection(
                new GroundCircleDecorator(Hud) {
                    Brush = Hud.Render.CreateBrush(200, 255, 148, 20, 0),
                    Radius = 2
                },
                new MapShapeDecorator(Hud)
                {
                    Brush = Hud.Render.CreateBrush(180, 255, 148, 20, 0),
                    ShadowBrush = shadowBrush,
                    Radius = 8,
                    ShapePainter = new CircleShapePainter(Hud),
                }
            );
            ChampionDecorator = new WorldDecoratorCollection(
                new GroundCircleDecorator(Hud) {
                    Brush = Hud.Render.CreateBrush(200, 64, 128, 255, 0),
                    Radius = 2
                },
                new MapShapeDecorator(Hud)
                {
                    Brush = Hud.Render.CreateBrush(180, 64, 128, 255, 0),
                    ShadowBrush = shadowBrush,
                    Radius = 8,
                    ShapePainter = new CircleShapePainter(Hud),
                }
            );
            JuggernautDecorator = new WorldDecoratorCollection(
                new MapShapeDecorator(Hud)
                {
                    Brush = Hud.Render.CreateBrush(255, 255, 0, 0, 0),
                    ShadowBrush = shadowBrush,
                    Radius = 8,
                    ShapePainter = new CircleShapePainter(Hud),
                },
                new GroundCircleDecorator(Hud)
                {
                    Brush = Hud.Render.CreateBrush(200, 255, 148, 20, 0),
                    Radius = 2,
                },
                new GroundCircleDecorator(Hud) {
                    Brush = Hud.Render.CreateBrush(180, 255, 0, 0, 0),
                    Radius = 0.3f  
                }
                );
            RareMinionDecorator = new WorldDecoratorCollection(
                new MapShapeDecorator(Hud)
                {
                    Brush = Hud.Render.CreateBrush(180, 192, 92, 20, 0),
                    ShadowBrush = shadowBrush,
                    Radius = 5,
                    ShapePainter = new CircleShapePainter(Hud),
                }
            );
           
            UniqueDecorator = new WorldDecoratorCollection(
                new GroundCircleDecorator(Hud) {
                    Brush = Hud.Render.CreateBrush(200, 120, 0, 255, 0),
                    Radius = 2
                },
                new MapShapeDecorator(Hud)
                {
                    Brush = Hud.Render.CreateBrush(180, 120, 0, 255, 0),
                    ShadowBrush = shadowBrush,
                    Radius = 8.0f,
                    ShapePainter = new CircleShapePainter(Hud),
                }
            );
 
            BossDecorator = new WorldDecoratorCollection(
                new GroundCircleDecorator(Hud) {
                    Brush = Hud.Render.CreateBrush(180, 120, 0, 255, 0),
                    Radius = 2
                },
                new MapShapeDecorator(Hud)
                {
                    Brush = Hud.Render.CreateBrush(180, 120, 0, 255, 0),
                    ShadowBrush = shadowBrush,
                    Radius = 9.0f,
                    ShapePainter = new CircleShapePainter(Hud),
                }
            );
               
 
 
 
            CustomAffixNames = new Dictionary<MonsterAffix, string>();
 
            AffixDecorators = new Dictionary<MonsterAffix, WorldDecoratorCollection>();
 
        }
 
        public void PaintWorld(WorldLayer layer)
        {
            var monsters = Hud.Game.AliveMonsters;
            var goblins = Hud.Game.AliveMonsters.Where(x => x.SnoMonster.Priority == MonsterPriority.goblin);
            foreach (var monster in goblins)
            {
                GoblinDecorator.Paint(layer, monster, monster.FloorCoordinate, null);
            }
 
            var eliteMaxHealth = 0.0d;
            List<IMonster> monstersElite = new List<IMonster>();
            foreach (var monster in monsters)
            {
                if (monster.Rarity == ActorRarity.Champion || monster.Rarity == ActorRarity.Rare)
                {
                    if (eliteMaxHealth < monster.MaxHealth) eliteMaxHealth = monster.MaxHealth;
                    monstersElite.Add(monster);
                }
               
                if (monster.Rarity == ActorRarity.RareMinion) {
                    RareMinionDecorator.Paint(layer, monster, monster.FloorCoordinate, monster.SnoMonster.NameLocalized);
                }
 
                if (monster.Rarity == ActorRarity.Unique) {
                    UniqueDecorator.Paint(layer, monster, monster.FloorCoordinate, monster.SnoMonster.NameLocalized);
                }
 
                if (monster.Rarity == ActorRarity.Boss) {
                    BossDecorator.Paint(layer, monster, monster.FloorCoordinate, monster.SnoMonster.NameLocalized);
                }
            }
            foreach (var monster in monstersElite)
            {
                if (monster.MaxHealth > eliteMaxHealth / 4)
                {
                    bool flag = true;
                    foreach (var snoMonsterAffix in monster.AffixSnoList)
                    {
                        string affixName = null;
                        if (CustomAffixNames.ContainsKey(snoMonsterAffix.Affix))
                        {
                            affixName = CustomAffixNames[snoMonsterAffix.Affix];
                        }
                        else affixName = snoMonsterAffix.NameLocalized;
                        if (snoMonsterAffix.Affix == MonsterAffix.Juggernaut) flag = false;
 
                        WorldDecoratorCollection decorator;
                        if (!AffixDecorators.TryGetValue(snoMonsterAffix.Affix, out decorator)) continue;
                        decorator.Paint(layer, monster, monster.FloorCoordinate, affixName);
                    }
                    if (monster.Rarity == ActorRarity.Rare)
                    {
                        if (flag) RareDecorator.Paint(layer, monster, monster.FloorCoordinate, monster.SnoMonster.NameLocalized);
                        else JuggernautDecorator.Paint(layer, monster, monster.FloorCoordinate, monster.SnoMonster.NameLocalized);
                    }
                    if (monster.Rarity == ActorRarity.Champion) ChampionDecorator.Paint(layer, monster, monster.FloorCoordinate, monster.SnoMonster.NameLocalized);
                   
                }
           
            }
           
            monstersElite.Clear();
        }
    }
}