using System;
using System.Linq;
using System.Collections.Generic;
using Turbo.Plugins.Default;
using SharpDX;
using SharpDX.Direct2D1;
 
namespace Turbo.Plugins.User
{
    public class WormHolePlugin : BasePlugin, INewAreaHandler, IInGameTopPainter
    {      
        private Dictionary<uint,int> WormHole { get;set; } = new Dictionary<uint,int>();
        private IBrush BrushBorder { get; set; }
        private IBrush BrushFill { get; set; }
        private IBrush BrushCounter { get; set; }      
        private IBrush TimeLeftClockBrush { get; set; }
        private IFont FontText { get; set; }
        private const int duration = 6;
        private const int radius = 8;
        private const int radius_countdown = 25;
       
        public WormHolePlugin()
        {
            Enabled = true;
        }
 
        private void DrawTimeLeftClock(RectangleF rect, double elapsed, double timeLeft)   // plugins\Default\BuffLists\Painter\BuffPainter.cs
        {
            if ((timeLeft > 0) && (elapsed >= 0) && (TimeLeftClockBrush != null))
            {
                var endAngle = Convert.ToInt32(360.0d / (timeLeft + elapsed) * elapsed);
                var startAngle = 0;
                TimeLeftClockBrush.Opacity = 1 - (float)(0.5f / (timeLeft + elapsed) * elapsed);
                var rad = rect.Width; // * 0.45f;
                using (var pg = Hud.Render.CreateGeometry())
                {
                    using (var gs = pg.Open())
                    {
                        gs.BeginFigure(rect.Center, FigureBegin.Filled);
                        for (var angle = startAngle; angle <= endAngle; angle++)
                        {
                            var mx = rad * (float)Math.Cos((angle - 90) * Math.PI / 180.0f);
                            var my = rad * (float)Math.Sin((angle - 90) * Math.PI / 180.0f);
                            var vec = new Vector2(rect.Center.X + mx, rect.Center.Y + my);
                            gs.AddLine(vec);
                        }
                        gs.EndFigure(FigureEnd.Closed);
                        gs.Close();
                    }
                    TimeLeftClockBrush.DrawGeometry(pg);
                }
            }
        }
       
        public override void Load(IController hud)
        {
            base.Load(hud);
            Order = 30949;
 
            BrushBorder = hud.Render.CreateBrush(250, 255, 0, 255, 2);
            BrushFill = hud.Render.CreateBrush(30, 255, 0, 255, 0);        
            BrushCounter = hud.Render.CreateBrush(230, 255, 150, 255, 0);
            TimeLeftClockBrush = Hud.Render.CreateBrush(220, 0, 0, 0, 0);
           
            FontText = Hud.Render.CreateFont("tahoma", 9f, 255, 100, 255, 150, true, false, 128, 0, 0, 0, true);
        }
 
        public void OnNewArea(bool newGame, ISnoArea area)
        {  
            if (newGame) WormHole.Clear();     
        }
               
        public void PaintTopInGame(ClipState clipState)
        {
            if (Hud.Game.IsInTown) return;
            if (clipState != ClipState.BeforeClip) return;
            var actors = Hud.Game.Actors.Where(a => a.SnoActor.Sno == ActorSnoEnum._x1_monsteraffix_teleportmines); // 337109          
            foreach (var actor in actors)  
            {
                if (!WormHole.ContainsKey(actor.AnnId)) { WormHole[actor.AnnId] = actor.CreatedAtInGameTick; }
                BrushBorder.DrawWorldEllipse(radius, -1, actor.FloorCoordinate);   
                BrushFill.DrawWorldEllipse(radius, -1, actor.FloorCoordinate);
                Hud.Render.GetMinimapCoordinates(actor.FloorCoordinate.X, actor.FloorCoordinate.Y, out var mapX, out var mapY);
                var r = radius * Hud.Render.MinimapScale;
                BrushBorder.DrawEllipse(mapX,mapY,r ,r );
 
                if (actor.IsOnScreen)
                {
                    var elapsed = (Hud.Game.CurrentGameTick - WormHole[actor.AnnId])/60d;   var timeleft = duration - elapsed;
                    var x = actor.FloorCoordinate.ToScreenCoordinate().X; var y = actor.FloorCoordinate.ToScreenCoordinate().Y;
                    var radiusc = radius_countdown / 1200.0f * Hud.Window.Size.Height;
                   
                    BrushCounter.DrawEllipse(x , y , radiusc , radiusc);               
                    DrawTimeLeftClock(new RectangleF(x - radiusc/2, y - radiusc/2 , radiusc, radiusc), elapsed , timeleft );                           
                    var layout = FontText.GetTextLayout( (timeleft < 0)?"0.0":timeleft.ToString((timeleft > 1)?"F0":"F1") );
                    FontText.DrawText(layout, x - layout.Metrics.Width/2 , y - layout.Metrics.Height/2 - 1);   
                }                                                          
            }
        }
    }
}