// Modified. Original (by glq): https://www.ownedcore.com/forums/diablo-3/turbohud/turbohud-discussions/618926-damage-count-down-circle-of-spirit-barrage-post3728819.html#post3728819
using System.Linq;
using Turbo.Plugins.Default;
 
namespace Turbo.Plugins.User
{
    public class SpiritBarragePhantasmPlugin : BasePlugin, IInGameWorldPainter, ICustomizer
    {
        private WorldDecoratorCollection SpiritBarragePhantasmDecorator { get; set; }
        private GroundCircleDecorator SpiritBarragePhantasmExplosion { get; set; }
        private TopLabelWithTitleDecorator PhantomCountDecorator { get; set; }
       
        public bool ShowOthers {get; set;}
        public bool ShowCounter {get; set;}
        public bool ShowExplosionCircle {get; set;}
        public int CircleSeconds {get; set;}
       
        public SpiritBarragePhantasmPlugin()
        {
            Enabled = true;
        }
 
        public override void Load(IController hud)
        {
            base.Load(hud);
            Order = 30001;
           
            ShowOthers = true;          // Also show for other players.
            ShowCounter = true;         // Counter
            ShowExplosionCircle = true; // Show  additional circle after "CircleSeconds"
            CircleSeconds = 8;          // 0 .. 10 ,  Draw additional circle after this seconds ( 8 -> the last 2 seconds)
   
            SpiritBarragePhantasmDecorator = new WorldDecoratorCollection(
                new GroundCircleDecorator(Hud)
                {
                    Brush = Hud.Render.CreateBrush(255, 0, 128, 255, 2),
                    Radius = 10,
                },
                new GroundLabelDecorator(Hud)
                {
                    CountDownFrom = 10,
                    TextFont = Hud.Render.CreateFont("tahoma", 9, 255, 100, 255, 150, true, false, 128, 0, 0, 0, true),
                },
                new GroundTimerDecorator(Hud)
                {
                    CountDownFrom = 10,
                    BackgroundBrushEmpty = Hud.Render.CreateBrush(100, 0, 0, 0, 0),
                    BackgroundBrushFill = Hud.Render.CreateBrush(200, 0, 128, 255, 0),
                    Radius = 25,
                }
            );
           
            SpiritBarragePhantasmExplosion = new GroundCircleDecorator(hud)
            {
                Brush = hud.Render.CreateBrush(200, 255, 255, 0, 3, SharpDX.Direct2D1.DashStyle.Dash),
                Radius = 15f,
            };
           
            PhantomCountDecorator = new TopLabelWithTitleDecorator(Hud)
            {
                BackgroundBrush = Hud.Render.CreateBrush(80, 134, 238, 240, 0),
                BorderBrush = Hud.Render.CreateBrush(255, 0, 0, 0, -1),
                TextFont = Hud.Render.CreateFont("tahoma", 8, 255, 255, 0, 0, true, false, true),
            };
                   
        }
 
        public void Customize()
        {
            if ((CircleSeconds < 0) || (CircleSeconds > 10))    {   CircleSeconds = 8 ; }              
        }
               
        public void PaintWorld(WorldLayer layer)
        {
            if (!Hud.Game.IsInGame) return;        
            if (layer != WorldLayer.Ground) return;
           
            var actors = Hud.Game.Actors.Where(a => a.SnoActor.Sno == ActorSnoEnum._wd_spiritbarragerune_aoe_ghostmodel);                                      
            if (actors.Any())
            {
                var total = 0;
                foreach(var player in Hud.Game.Players)
                {                  
                    if (!ShowOthers && !player.IsMe) continue;
                    var actorsPlayer = actors.Where(a => a.SummonerAcdDynamicId == player.SummonerId).OrderByDescending(a => a.CreatedAtInGameTick);
                    var c = 0;
                    foreach (var actor in actorsPlayer)
                    {
                        if (c++ == 3) break;
                        total++;       
                        var t = Hud.Game.CurrentGameTick - actor.CreatedAtInGameTick;
                        if (t < 606)
                        {
                            SpiritBarragePhantasmDecorator.Paint(layer, actor, actor.FloorCoordinate, null);
                            if  (ShowExplosionCircle && (t > 60 * CircleSeconds))
                            {
                                SpiritBarragePhantasmExplosion.Paint(actor, actor.FloorCoordinate, null);
                            }
                        }
                    }                                  
                }
                if (ShowCounter)
                {
                    var uiRect = Hud.Render.GetUiElement("Root.NormalLayer.game_dialog_backgroundScreenPC.game_progressBar_manaBall").Rectangle;                           
                    PhantomCountDecorator.Paint(uiRect.Left + uiRect.Width * 0.20f, uiRect.Top - uiRect.Height * 0.20f, uiRect.Width * 0.60f, uiRect.Height * 0.15f, "Phantom:" + total);
                }
            }          
        }
    }
}