using SharpDX;
using SharpDX.Direct2D1;
using System;
using System.Linq;
using System.Globalization;
using System.Collections.Generic;
using Turbo.Plugins.Default;
 
namespace Turbo.Plugins.User
{
    public class DAV_SaderValorRGK : BasePlugin, IInGameWorldPainter {
        public float xPos { get; set; }
        public float yPos { get; set; }
        public IFont Font_Norm { get; set; }
        public IFont Font_Good { get; set; }
        public IFont Font_Warm { get; set; }
 
        public float barH { get; set; }
        public float barW { get; set; }
        public float Energy_Bad { get; set; }
        public string Energy_Message { get; set; }
        public IBrush Brush_BG { get; set; }
        public IBrush Brush_EnergyBad { get; set; }
        public IBrush Brush_EnergyGood { get; set; }
 
        public int FistofHeaven_Min { get; set; }
        public float iconSize { get; set; }
        public ITexture Icon_FistHeaven { get; set; }
        public ITexture Icon_HeavenFury { get; set; }
        public ITexture Icon_COE { get; set; }
        public WorldDecoratorCollection FistofHeaven_Decorator { get; set; }
 
        public DAV_SaderValorRGK() {
            Enabled = true;
        }
 
        public override void Load(IController hud) {
            base.Load(hud);
 
            Energy_Bad = 0.4f;
            Energy_Message = "Fury : ";
            FistofHeaven_Min = 4;
 
            xPos = Hud.Window.Size.Width * 890 / 1920;
            yPos = Hud.Window.Size.Height * 265 / 1080;
            barH = Hud.Window.Size.Height * 14 / 1080;
            barW = Hud.Window.Size.Width * 120 / 1920;
            iconSize = Hud.Window.Size.Height * 40 / 1080;
 
            Font_Norm = Hud.Render.CreateFont("arial", 7, 255, 255, 255, 255, false, false, true);
            Font_Good = Hud.Render.CreateFont("arial", 9, 255, 51, 255, 51, true, false, 255, 0, 0, 0, true);
            Font_Warm = Hud.Render.CreateFont("arial", 9, 255, 255, 51, 51, true, false, 255, 0, 0, 0, true);
 
            Brush_EnergyGood = Hud.Render.CreateBrush(240, 51, 255, 51, 0);
            Brush_EnergyBad = Hud.Render.CreateBrush(240, 255, 51, 51, 0);
            Brush_BG = Hud.Render.CreateBrush(240, 204, 204, 204, 0);
 
            Icon_FistHeaven = Hud.Texture.GetTexture(3476188190);
            Icon_HeavenFury = Hud.Texture.GetTexture(2206308930);
            Icon_COE = Hud.Texture.GetTexture(2639019912);
            FistofHeaven_Decorator = new WorldDecoratorCollection(
                new GroundCircleDecorator(Hud) {
                    Brush = Hud.Render.CreateBrush(0, 204, 204, 51, 0),
                    Radius = 0,
                }
            );
        }
 
        public void PaintWorld(WorldLayer layer) {
            if (Hud.Game.SpecialArea != SpecialArea.GreaterRift) return;
            if (Hud.Game.RiftPercentage < 100) return;
 
            var players = Hud.Game.Players.Where(x => x.HeroClassDefinition.HeroClass == HeroClass.Crusader && x.Powers.BuffIsActive(483639, 0));
            if (players.Count() != 1) return;
 
            var FistofHeaven = Hud.Game.Actors.Where(x => x.SnoActor.Sno == ActorSnoEnum._x1_crusader_fistofheavens_teslacoil);
            foreach (var actor in FistofHeaven)
                FistofHeaven_Decorator.Paint(layer, null, actor.FloorCoordinate, null);
 
            var xref = xPos;
            var yref = yPos;
            foreach (var player in players) {
                // Number of Fist of Heaven Actor
                Icon_FistHeaven?.Draw(xref, yref, iconSize, iconSize);
                var stack = FistofHeaven.Count();
                var usedFont = stack < FistofHeaven_Min ? Font_Warm : Font_Good;
                var layout = usedFont.GetTextLayout(stack.ToString());
                usedFont.DrawText(layout, xref + (iconSize - layout.Metrics.Width) / 2f, yref + (iconSize - layout.Metrics.Height) / 2f);
                xref += iconSize;
 
                // Number of Valor Set 2 Stack
                var furyBuff = player.Powers.GetBuff(483643);
                Icon_HeavenFury?.Draw(xref, yref, iconSize, iconSize);
                stack = furyBuff.IconCounts[1];
                usedFont = stack < 3 ? Font_Warm : Font_Good;
                usedFont.DrawText(stack.ToString(), xref + 2, yref + 2);
 
                var timeLeft = furyBuff.TimeLeftSeconds[1];
                if (timeLeft > 0) {
                    layout = Font_Warm.GetTextLayout(timeLeft.ToString(timeLeft < 1 ? "F1" : "F0"));
                    Font_Warm.DrawText(layout, xref + iconSize - layout.Metrics.Width - 2, yref + iconSize - layout.Metrics.Height - 2);
                }
                xref += iconSize;
 
                // COE
                if (player.Powers.BuffIsActive(430674, 0)) {
                    Icon_COE.Draw(xref, yref, iconSize, iconSize);
                    var coeLeft = player.Powers.GetBuff(430674).TimeLeftSeconds[8];
                    if (coeLeft >= 8) {
                        usedFont = coeLeft > 12 ? Font_Warm : Font_Good;
                        layout = usedFont.GetTextLayout((coeLeft - 8).ToString("F1"));
                        usedFont.DrawText(layout, xref + (iconSize - layout.Metrics.Width) / 2f, yref + (iconSize - layout.Metrics.Height) / 2f);
                    }
                }
                yref += iconSize;
 
                // Resource Status
                var EnergyPct = player.Stats.ResourcePctPri / 100f;
                var usedBrush = EnergyPct < Energy_Bad ? Brush_EnergyBad : Brush_EnergyGood;
                Brush_BG.DrawRectangle(xPos, yref, barW, barH);
                usedBrush.DrawRectangle(xPos, yref, barW * EnergyPct, barH);
 
                layout = Font_Norm.GetTextLayout(Energy_Message + EnergyPct.ToString("0%"));
                Font_Norm.DrawText(layout, xPos + 2, yref + (barH - layout.Metrics.Height) / 2f);
            }
        }
    }
}