// SkillBarPainter by GLQ

using SharpDX;
using SharpDX.Direct2D1;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Turbo.Plugins.Default;

namespace Turbo.Plugins.Extended.Addons
{

    public class CooldownPainter : ITransparentCollection
    {
        public bool Enabled { get; set; }
        public IController Hud { get; set; }

        public IFont CooldownFont { get; set; }
        public IFont GreenFont { get; set; }
        public IFont RedFont { get; set; }

        public float TextureOpacity { get; set; }

        private readonly IWatch _lastTownEliteSimulation;

        public CooldownPainter(IController hud, bool setDefaultStyle)
        {
            Enabled = true;
            Hud = hud;

            _lastTownEliteSimulation = Hud.Time.CreateWatch();

            if (setDefaultStyle)
            {
                TextureOpacity = 0.2f;
                CooldownFont = Hud.Render.CreateFont("tahoma", 6f, 255, 255, 255, 255, true, false, 220, 0, 0, 0, true);
            }

            GreenFont = Hud.Render.CreateFont("tahoma", 6f, 255, 128, 255, 0, true, false, 220, 0, 0, 0, true);
            RedFont = Hud.Render.CreateFont("tahoma", 6f, 255, 255, 30, 50, true, false, 220, 0, 0, 0, true);
        }

        public void Paint(IPlayerSkill skill, RectangleF rect)
        {
            if (skill == null) return;
	    if (!skill.IsOnCooldown) return;

            if (TextureOpacity > 0)
            {
                var texture = Hud.Texture.GetTexture(skill.SnoPower.NormalIconTextureId);
                if (texture != null)
                {
                    texture.Draw(rect.X, rect.Y, rect.Width, rect.Height, TextureOpacity);
                    Hud.Texture.BuffFrameTexture.Draw(rect.X, rect.Y, rect.Width, rect.Height, TextureOpacity);
                }
            }

            if (skill.IsOnCooldown && (skill.CooldownFinishTick > Hud.Game.CurrentGameTick))
            {
                var remaining = (skill.CooldownFinishTick - Hud.Game.CurrentGameTick) / 60.0d;
                var text = remaining > 1.0f ? remaining.ToString("F0", CultureInfo.InvariantCulture) : remaining.ToString("F0", CultureInfo.InvariantCulture);
                var textLayout = CooldownFont.GetTextLayout(text);
                CooldownFont.DrawText(textLayout, rect.X + (rect.Width - (float)Math.Ceiling(textLayout.Metrics.Width)) / 2.0f, rect.Y + (rect.Height - textLayout.Metrics.Height) / 2);
	    }

            var rune = skill.Rune;
            if (rune == byte.MaxValue) rune = 0; else rune += 1;
        }

        public IEnumerable<ITransparent> GetTransparents()
        {
            yield return CooldownFont;
        }

    }

}