using Turbo.Plugins.Default;
using System.Linq;
using System;
using System.Collections.Generic;
 
namespace Turbo.Plugins.User
{
    public class ShieldOfFuryStacks : BasePlugin, IInGameTopPainter, ICustomizer, INewAreaHandler
    {          
        private IFont FontStacks  { get; set; }
        private Dictionary<uint, int[]> SoF { get; set; } = new Dictionary<uint,int[]>();  // [stateOld,stacks]
        private float SizeIconWidth  { get; set; }
        private float SizeIconHeight  { get; set; }
        private IBrush BorderGreenBrush { get; set; }
 
        public delegate bool JudgmentFunc(IMonster m);
        public JudgmentFunc JudgmentImmob { get; set; }
 
        public float SizeMultiplier  { get; set; }
        public float Opacity { get; set; }
        public float OfssetY { get; set; }
        public bool OnlyBoss { get; set; }
        public bool CheckBracer { get; set; }
 
        public ShieldOfFuryStacks()
        {
            Enabled = true;
        }
       
        public override void Load(IController hud)
        {
            Order = 30004;
            base.Load(hud);
 
            Opacity = 0.75f;            // 0f..1f  Opacity for icon texture
            SizeMultiplier = 0.65f;     // Size multiplier for icons
            OfssetY =  -11f;            // By default it will be shown in the monster's position and this value will be added
            CheckBracer = true;     // Check if the monster is blinded, stunned or immobilized to show a green frame if yes
            OnlyBoss = true;
            BorderGreenBrush = Hud.Render.CreateBrush(250, 0, 220, 0, -1);
            JudgmentImmob = (m) =>  (
                                        (m.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_0_Visual_Effect_E,267600) == 1) ||
                                        (m.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_0_Visual_Effect_D,267600) == 1) ||
                                        (m.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_0_Visual_Effect_C,267600) == 1) ||
                                        (m.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_0_Visual_Effect_B,267600) == 1) ||
                                        (m.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_0_Visual_Effect_A,267600) == 1) ||
                                        (m.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_0_Visual_Effect_None,267600) == 1)
                                    );
        }
 
        public void OnNewArea(bool newGame, ISnoArea area)
        {  
            if (newGame) SoF.Clear();
        }
               
        public void Customize()
        {
            SizeIconWidth = Hud.Texture.BuffFrameTexture.Width   * Hud.Window.Size.Height/1200.0f * SizeMultiplier;
            SizeIconHeight = Hud.Texture.BuffFrameTexture.Height * Hud.Window.Size.Height/1200.0f * SizeMultiplier;
            FontStacks = Hud.Render.CreateFont("Tahoma", 12f * SizeMultiplier ,  255, 255, 255, 255, true, false, 255, 0, 0, 0, true);
        }
 
        public void PaintTopInGame(ClipState clipState)
        {
            if (clipState != ClipState.BeforeClip) return;         
            if (!Hud.Game.IsInGame) return;
            if (Hud.Game.Me.InGreaterRiftRank == 0) return;
            if (OnlyBoss && Hud.Game.RiftPercentage != 100) return;
            var player = Hud.Game.Players.FirstOrDefault(p => (p.HeroClassDefinition.HeroClass == HeroClass.Crusader) && p.Powers.BuffIsActive(446142) );
            if ( player != null )
            {
                var monsters = Hud.Game.AliveMonsters.Where(m => (m.Rarity != ActorRarity.Normal && m.Rarity != ActorRarity.RareMinion && m.Rarity != ActorRarity.Hireling) && (m.SummonerAcdDynamicId == 0));
                foreach(var m in monsters)
                {
                    if (!SoF.TryGetValue(m.AnnId,out var a))
                    {
                        if (m.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_1_Visual_Effect_None,446142) == -1)   { a = new int[2]{0,0}; }
                        else { a = new int[2]{ (m.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_2_Visual_Effect_None,446142) == 1)? 1:0, 1}; }
                        //a = new int[2]{0,0};
                        SoF.Add(m.AnnId, a);                           
                    }
                    else
                    {
                        if (m.GetAttributeValue(Hud.Sno.Attributes.Power_Buff_2_Visual_Effect_None,446142) == 1)
                        {
                            if (a[0] == 0)  { a[0] = 1; a[1] += 1;  }                          
                        }
                        else
                        {
                            if (a[0] == 1) { a[0] = 0; }
                        }
                    }
                    if (a[1] > 0)
                    {
                        var x = m.FloorCoordinate.ToScreenCoordinate().X - SizeIconWidth/2 ;
                        var y = m.FloorCoordinate.ToScreenCoordinate().Y - SizeIconHeight + OfssetY;
                        Hud.Texture.InventoryLegendaryBackgroundLarge.Draw(x,y,SizeIconWidth,SizeIconHeight,Opacity);
                        Hud.Texture.GetItemTexture(Hud.Sno.SnoItems.P61_Unique_Shield_106_x1).Draw(x,y,SizeIconWidth,SizeIconHeight,Opacity);
                        if (CheckBracer && player.Powers.BuffIsActive(446162))  
                        {
                            if (m.Blind || m.Stunned || ( (player.Powers.UsedCrusaderPowers.Judgment != null) && JudgmentImmob(m)) )
                            {  
                                BorderGreenBrush.DrawRectangle(x, y, SizeIconWidth - 1, SizeIconHeight - 1); // border green
                            }
                                   
                            else
                            {  
                                Hud.Texture.DebuffFrameTexture.Draw(x,y,SizeIconWidth,SizeIconHeight,Opacity);
                            }                                  
                        }
                        var _layout = FontStacks.GetTextLayout( a[1].ToString());
                        FontStacks.DrawText(_layout, x + ((SizeIconWidth - (float)Math.Ceiling(_layout.Metrics.Width))/2.0f), y + ((SizeIconHeight - (float)Math.Ceiling(_layout.Metrics.Height))/2.0f) );
                    }
                }
            }
        }          
    }
}