// Extended MonsterHealthBarPlugin
// Adds Iceblink & Piranhas Tracker in Top Debuff List.
// Adds an option to only see HP % on monsters (Disabled by default). 
// Adds Frailty Kill Treshold with Early Grave Support & Executioner Support on non-trash monsters.
// Adds a Bottom Buff List including Commanded Skeletons, Firebird, "Touched by Inna" (Lesser Bindings), Single Out Passive, Crus Debuffs...
 
using System;
using System.Globalization;
using System.Collections.Generic;
using System.Linq;
using Turbo.Plugins.Default;
 
namespace Turbo.Plugins.User
{
    public class ExtendedMonsterHealthBarPlugin : BasePlugin, ICustomizer, IInGameTopPainter
    {
        public IFont MonsterHitpointsFont { get; set; }
        public IFont MonsterEffectsFont { get; set; }
        public IBrush FrailtyLineBrush { get; set; }
        public IFont MonsterIllusionFont { get; set; }
 
        public bool EnableShortHP { get; set; }
        public bool EnableCCDebuff { get; set; }
        public bool EnableBottomBuffBar { get; set; }
        public bool EnableFrailty { get; set; }
 
        private readonly int[] _skillOrder = { 2, 3, 4, 5, 0, 1 };
 
        public ExtendedMonsterHealthBarPlugin()
        {
            Enabled = true;
 
        EnableShortHP = false;
        EnableCCDebuff = true;
        EnableBottomBuffBar = true;
        EnableFrailty = true;
        }
 
        public void Customize()
        {
        // Disable Default Plugin
            Hud.GetPlugin<TopMonsterHealthBarPlugin>().Enabled = false;
    }
 
        public override void Load(IController hud)
        {
            base.Load(hud);
 
            MonsterHitpointsFont = Hud.Render.CreateFont("tahoma", 6, 255, 255, 255, 255, true, false, true);
            MonsterIllusionFont = Hud.Render.CreateFont("tahoma", 6, 255, 100, 230, 152, true, false, true);
            MonsterEffectsFont = Hud.Render.CreateFont("tahoma", 6, 255, 40, 200, 40, true, false, 128, 0, 0, 0, true);
            FrailtyLineBrush = Hud.Render.CreateBrush(255, 255, 190, 70, 1.5f);
        }
 
        public void PaintTopInGame(ClipState clipState)
        {
            if (clipState != ClipState.BeforeClip) return;
 
            var uiBar = Hud.Render.MonsterHpBarUiElement;
            var monster = Hud.Game.SelectedMonster2 ?? Hud.Game.SelectedMonster1;
        
            if ((monster == null) || (uiBar == null)) return;
 
            var hpText = ValueToString(monster.CurHealth, ValueFormat.LongNumber) + " / " + ValueToString(monster.MaxHealth, ValueFormat.LongNumber);
            hpText += " - " + ValueToString(monster.CurHealth / (monster.MaxHealth / 100.0f), ValueFormat.LongNumber) + "%";
 
            var hpTextShort = ValueToString(monster.CurHealth / (monster.MaxHealth / 100.0f), ValueFormat.LongNumber) + "%";
 
        var MonsterHPFont = MonsterHitpointsFont;
        if (monster.SummonerAcdDynamicId != 0 && monster.IsElite) MonsterHPFont = MonsterIllusionFont;
 
            var textLayout = MonsterHPFont.GetTextLayout(hpText);
            if (EnableShortHP) textLayout = MonsterHPFont.GetTextLayout(hpTextShort);
            MonsterHPFont.DrawText(textLayout, uiBar.Rectangle.Left + (uiBar.Rectangle.Width - textLayout.Metrics.Width) / 2, uiBar.Rectangle.Top + (uiBar.Rectangle.Height - textLayout.Metrics.Height) / 2);
 
 
    if (EnableCCDebuff) {
            string textCC = null;
            if (monster.Frozen) textCC += (textCC == null ? "" : ", ") + "frozen";
            if (monster.Chilled) textCC += (textCC == null ? "" : ", ") + "chill";
            if (monster.Slow) textCC += (textCC == null ? "" : ", ") + "slow";
            if (monster.Stunned) textCC += (textCC == null ? "" : ", ") + "stun";
            if (monster.Blind) textCC += (textCC == null ? "" : ", ") + "blind";
            if (monster.Invulnerable) textCC += (textCC == null ? "" : ", ") + "invulnerable";
        // if (monster.GetAttributeValue(Hud.Sno.Attributes.Uninterruptible, uint.MaxValue) == 1) textCC += (textCC == null ? "" : ", ") + "uninterruptible";
 
            string textDebuff = null;
            if (monster.Palmed) textDebuff += (textDebuff == null ? "" : ", ") + "palm";
            if (monster.Locust) textDebuff += (textDebuff == null ? "" : ", ") + "locust";
            if (monster.Haunted) textDebuff += (textDebuff == null ? "" : ", ") + "haunt";
        if ((monster.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_1_Visual_Effect_None, 347265) == 1) || (monster.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_2_Visual_Effect_A, 347265) == 1) || (monster.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_3_Visual_Effect_B, 347265) == 1) || (monster.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_4_Visual_Effect_C, 347265) == 1) || (monster.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_5_Visual_Effect_D, 347265) == 1) || (monster.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_6_Visual_Effect_E, 347265) == 1)) textDebuff += (textDebuff == null ? "" : ", ") + "piranhas";
            if (monster.MarkedForDeath) textDebuff += (textDebuff == null ? "" : ", ") + "mark";
        if (monster.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_1_Visual_Effect_None, 428354) == 1) textDebuff += (textDebuff == null ? "" : ", ") + "iceblink";
            if (monster.Strongarmed) textDebuff += (textDebuff == null ? "" : ", ") + "strongarm";
            // if (monster.Phoenixed) textDebuff += (textDebuff == null ? "" : ", ") + "firebird";
 
            var text = textCC + (textCC != null && textDebuff != null ? " | " : "") + textDebuff;
 
            if (monster.DotDpsApplied > 0) text += (string.IsNullOrEmpty(text) ? "" : " | ") + "DoT: " + ValueToString(monster.DotDpsApplied, ValueFormat.LongNumber);
 
            if (text != null)
            {
                textLayout = MonsterEffectsFont.GetTextLayout(text);
                MonsterEffectsFont.DrawText(textLayout, uiBar.Rectangle.Left + (uiBar.Rectangle.Width - textLayout.Metrics.Width) / 2, uiBar.Rectangle.Top - (uiBar.Rectangle.Height * 0.38f) - textLayout.Metrics.Height);
            }
    }
 
    // Frailty Kill Treshold (With Early Grave & Executioner Support)
    if (EnableFrailty) {
 
    var earlygrave = false;
    var necroIG = Hud.Game.Players.FirstOrDefault(p => (p.HasValidActor && p.HeroClassDefinition.HeroClass == HeroClass.Necromancer));
 
    if (necroIG != null) {
        foreach (var i in _skillOrder)
        {
        var skill = necroIG.Powers.SkillSlots[i];
        if (skill == null) continue;
        if (skill.SnoPower.Sno == 460870 && skill.Rune == 0) { // Early Grave
 
    if (monster.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_2_Visual_Effect_None, 471845) == 1 && monster.Rarity != ActorRarity.Normal) 
        { FrailtyLineBrush.DrawLine(uiBar.Rectangle.Left + (uiBar.Rectangle.Width * 0.18f), uiBar.Rectangle.Top, uiBar.Rectangle.Left + (uiBar.Rectangle.Width * 0.18f), uiBar.Rectangle.Bottom, 0);
          earlygrave = true; continue;  }
            }
        }
        } 
 
    if (Hud.Game.Players.FirstOrDefault(p => (p.HasValidActor && p.Powers.BuffIsActive(483516, 0))) != null) // The Executioner
                {
        if (monster.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_2_Visual_Effect_None, 471845) != 1 && monster.Rarity != ActorRarity.Normal && !earlygrave) 
        { FrailtyLineBrush.DrawLine(uiBar.Rectangle.Left + (uiBar.Rectangle.Width * 0.10f), uiBar.Rectangle.Top, uiBar.Rectangle.Left + (uiBar.Rectangle.Width * 0.10f), uiBar.Rectangle.Bottom, 0); }
        }
 
    if (monster.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_2_Visual_Effect_None, 471845) == 1 && monster.Rarity != ActorRarity.Normal && !earlygrave) 
        { FrailtyLineBrush.DrawLine(uiBar.Rectangle.Left + (uiBar.Rectangle.Width * 0.15f), uiBar.Rectangle.Top, uiBar.Rectangle.Left + (uiBar.Rectangle.Width * 0.15f), uiBar.Rectangle.Bottom, 0); }
 
    } // End Frailty Treshold
 
    // Bottom BuffBar
    if (EnableBottomBuffBar) {
            string otherBuff = null;
 
        // Touched by Inna (Lesser Bindings Bracers)
        if (monster.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_1_Visual_Effect_None, 449222) == 1) 
        otherBuff += (otherBuff == null ? "" : " - ") + "Touch of Inna";
 
        // Judgment + Divine Verdict
        // Debilitate Judgment Debuff - Power_Buff_2_Visual_Effect_E, 267600
        if (monster.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_0_Visual_Effect_None, 267600) == 1 || monster.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_0_Visual_Effect_A, 267600) == 1 || monster.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_0_Visual_Effect_B, 267600) == 1 || monster.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_0_Visual_Effect_C, 267600) == 1 || monster.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_0_Visual_Effect_D, 267600) == 1 || monster.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_0_Visual_Effect_E, 267600) == 1) 
        otherBuff += (otherBuff == null ? "" : " - ") + "Judgment";
        if (monster.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_0_Visual_Effect_A, 268530) == 1) 
        otherBuff += (otherBuff == null ? "" : " - ") + "Divine Verdict";
 
        // Firebird Debuff
        if (monster.Phoenixed)
        otherBuff += (otherBuff == null ? "" : " - ") + "Firebird";
 
        // Single Out Passive
        if (monster.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_1_Visual_Effect_None, 338859) == 1) 
        otherBuff += (otherBuff == null ? "" : " - ") + "Single Out";
 
        // Commanded Skeletons
        if (monster.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_4_Visual_Effect_D, 453801) == 1 || monster.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_4_Visual_Effect_A, 453801) == 1 || monster.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_4_Visual_Effect_B, 453801) == 1  || monster.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_4_Visual_Effect_E, 453801) == 1 || monster.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_6_Visual_Effect_C, 453801) == 1 || monster.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_4_Visual_Effect_C, 453801) == 1 || monster.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_4_Visual_Effect_None, 453801) == 1) 
        otherBuff += (otherBuff == null ? "" : " - ") + "Command Skeletons";
 
            if (otherBuff != null)
            {
                var textotherBuff = MonsterEffectsFont.GetTextLayout(otherBuff);
        if (monster.Rarity == ActorRarity.Boss) {
                MonsterEffectsFont.DrawText(textotherBuff, uiBar.Rectangle.Left + (uiBar.Rectangle.Width - textotherBuff.Metrics.Width) / 2, uiBar.Rectangle.Bottom + uiBar.Rectangle.Height / 1.9f);
        } else {
                MonsterEffectsFont.DrawText(textotherBuff, uiBar.Rectangle.Left + (uiBar.Rectangle.Width - textotherBuff.Metrics.Width) / 2, uiBar.Rectangle.Bottom + uiBar.Rectangle.Height / 3.5f);
            }
        }
        }
 
// End
 
        }
    }
}