using System.Linq;
using Turbo.Plugins.Default;
 
namespace Turbo.Plugins.User
{
    public class ArchonWizPlugin : BasePlugin, IInGameTopPainter, ICustomizer
    {
        public bool ShowWarnings { get; set; }
        public bool ShowInTown { get; set; }
        public bool ShowZeiCircle { get; set; }
        public bool ShowRashaElements { get; set; }
        public bool ShowArchonCD { get; set; }
        public bool ShowArchonRemain { get; set; }
        public bool AlwaysShowElements { get; set; }
        public bool AlwaysShowZei { get; set; }
        public float WarningYPos { get; set; }
        public float WarningYPosIncr { get; set; }
        public float ArchonCDandRemainYPos { get; set; }
        public float RashaIndicatorsYpos { get; set; }
 
        public GroundCircleDecorator ZeiRanceIndicator { get; set; }
        public TopLabelDecorator ArchonCDLabel { get; set; }
        public TopLabelDecorator ArchonCooldownLabel { get; set; }
        public TopLabelDecorator ArchonRemainingLabel { get; set; }
 
        public IFont WarningFont { get; set; }
        public IFont ArchonCDFont { get; set; }
        public IFont ArchonRemainFont { get; set; }
        public IFont ArchonRemainSoonFont { get; set; }
 
        public IBrush RashaBackgroundBrush { get; set; }
        public IBrush FireBrush { get; set; }
        public IBrush ArcaneBrush { get; set; }
        public IBrush LightningBrush { get; set; }
        public IBrush ColdBrush { get; set; }
        public IBrush GreyBrush { get; set; }
 
        private IPlayerSkill _archonSkill;
        private IPlayerSkill _magicWeaponSkill;
        private IPlayerSkill _energyArmorSkill;
/*AXYD added*/
        private IPlayerSkill _stormArmorSkill;
        private IPlayerSkill _iceArmorSkill;
        private IPlayerSkill _familiarSkill;
        public bool ShowStormArmor = false;
        public bool ShowIceArmor = false;
        public bool ShowFamiliar = false;          
/*add end*/
        private float HudWidth{get {return Hud.Window.Size.Width; }}
        private float HudHeight{get { return Hud.Window.Size.Height; }}
 
        private float _lWidth, _lHeight, _lRashaSize, _lRashaSizeMod, _arcCDRemain, _tick;
        private bool _timerRunning = false;
 
        public ArchonWizPlugin()
        {
            Enabled = true;
        }
 
        public void Customize()
        {
            Hud.RunOnPlugin<PlayerBottomBuffListPlugin>(plugin =>
            {
                plugin.BuffPainter.ShowTimeLeftNumbers = true;
                plugin.RuleCalculator.Rules.Add(new BuffRule(134872) { IconIndex = 2, MinimumIconCount = 1, ShowTimeLeft = false, ShowStacks = true, IconSizeMultiplier = 1.3f }); // Archon
                plugin.RuleCalculator.Rules.Add(new BuffRule(429855) { IconIndex = 5, MinimumIconCount = 1, ShowTimeLeft = true, ShowStacks = false, IconSizeMultiplier = 1.3f }); // Tal Rasha
            });
        }
 
        public override void Load(IController hud)
        {
            base.Load(hud);
 
            // Public vars
            ShowWarnings = false;
            ShowInTown = true;
            ShowZeiCircle = true;
            ShowRashaElements = true;
            ShowArchonCD = true;
            ShowArchonRemain = true;
            AlwaysShowElements = false;
            AlwaysShowZei = false;
            WarningYPos = 0.27f;
            WarningYPosIncr = 0.022f; // Distance between warnings
            ArchonCDandRemainYPos = 0.495f; // Just below tal rasha icons = 0.605f;
            RashaIndicatorsYpos = 0.585f;
 
            WarningFont = Hud.Render.CreateFont("tahoma", 14f, 255, 255, 0, 0, false, false, true);
            ArchonCDFont = Hud.Render.CreateFont("tahoma", 13f, 255, 0, 255, 0, false, false, true);
            ArchonRemainFont = Hud.Render.CreateFont("tahoma", 14f, 255, 0, 255, 0, false, false, true);
            ArchonRemainSoonFont = Hud.Render.CreateFont("tahoma", 14.5f, 255, 255, 0, 0, false, false, true);
 
            RashaBackgroundBrush = Hud.Render.CreateBrush(100, 30, 30, 30, 0);
            GreyBrush = Hud.Render.CreateBrush(255, 50, 50, 50, 0);
            FireBrush = Hud.Render.CreateBrush(255, 200, 130, 30, 0);
            ArcaneBrush = Hud.Render.CreateBrush(255, 180, 80, 180, 0);
            LightningBrush = Hud.Render.CreateBrush(255, 0, 65, 145, 0);
            ColdBrush = Hud.Render.CreateBrush(255, 80, 130, 180, 0);
            ZeiRanceIndicator = new GroundCircleDecorator(Hud)
            {
                Brush = Hud.Render.CreateBrush(50, 165, 223, 255, 4.5f),
                Radius = 50f
            };
            ArchonCooldownLabel = new TopLabelDecorator(Hud)
            {
                TextFont = Hud.Render.CreateFont("tahoma", 10f, 255, 140, 140, 180, false, false, 160, 0, 0, 0, true),
                TextFunc = ArchonCooldown,
            };
 
            // Private vars
            _lWidth = 80;
            _lHeight = 15;
            _lRashaSize = 24f;
            _lRashaSizeMod = 0.9f;
 
        }
 
        public void PaintTopInGame(ClipState clipState)
        {
            if (clipState != ClipState.BeforeClip) return;
 
            if (Hud.Game.IsInGame && Hud.Game.Me.HeroClassDefinition.HeroClass == HeroClass.Wizard && !(Hud.Game.Me.IsInTown && !ShowInTown))
            {
                var me = Hud.Game.Me;
                UpdateSkills(me);
 
                //If Disentegration Wave is channelled, draw zei circle
                if ((me.Powers.BuffIsActive(392891, 4) || AlwaysShowZei) && ShowZeiCircle)
                    ZeiRanceIndicator.Paint(me, me.FloorCoordinate, null);
             
                //Draw missing buff warnings
                if (ShowWarnings && !me.IsDead)
                    DrawWarnings(me);
 
                //Draw indicators for each tal rasha element
                if ((me.Powers.BuffIsActive(429855, 5) || AlwaysShowElements) && ShowRashaElements)
                    TalRashaElements(me);
 
                //Draw Archon cooldown
                if (_archonSkill != null && ShowArchonCD)
                    ArchonCooldownLabel.Paint(HudWidth * 0.5f - _lWidth / 2, HudHeight * (ArchonCDandRemainYPos+0.015f), _lWidth, _lHeight, HorizontalAlign.Center);
 
                //Draw Archon time remaining
                if (_archonSkill != null && ShowArchonRemain)
                    ArchonRemaining(me);
            }
        }
 
        public string ArchonCooldown()
        {
            string s = "";
            if (_archonSkill.CooldownFinishTick > Hud.Game.CurrentGameTick)
            {
                var c = (_archonSkill.CooldownFinishTick - Hud.Game.CurrentGameTick) / 60.0d;
                s = string.Format("☏ {0:N1} ☏", c);
            }
            return s;
        }
 
        private void ArchonRemaining(IPlayer me)
        {
            if (me.Powers.BuffIsActive(134872, 2))
            {
                if (!_timerRunning)
                    _tick = Hud.Game.CurrentGameTick;
                _timerRunning = true;
 
                var r = 20f - ((Hud.Game.CurrentGameTick - _tick) / 60.0d);
                if (r > 3f)
                {
                    var layout = ArchonRemainFont.GetTextLayout(string.Format("{0:N1}", r));
                    ArchonRemainFont.DrawText(layout, HudWidth * 0.5f - (layout.Metrics.Width * 0.5f), HudHeight * (ArchonCDandRemainYPos + 0.015f));
                }
                else
                {
                    string str = string.Format("{0:N1}", r);
                    if (r <= 3 && r > 2) str = string.Format("✪ {0:N1} ✪", r);
                    if (r <= 2 && r > 1) str = string.Format("☞ {0:N1} ☜", r);
                    if (r <= 1) str = string.Format("ᐒ {0:N1} ᐘ", r);
                    var layout = ArchonRemainSoonFont.GetTextLayout(str);
                    ArchonRemainSoonFont.DrawText(layout, HudWidth * 0.5f - (layout.Metrics.Width * 0.5f), HudHeight * (ArchonCDandRemainYPos + 0.005f));
                }
            }
            else _timerRunning = false;
        }
 
        private void TalRashaElements(IPlayer me)
        {
            RashaBackgroundBrush.DrawRectangle((HudWidth * 0.5f - _lRashaSize * .5f) - _lRashaSize * 1.6f, HudHeight * RashaIndicatorsYpos - _lRashaSize * 0.1f, _lRashaSize * 4.1f, _lRashaSize * 1.1f);
 
            if (me.Powers.BuffIsActive(429855, 1)) ArcaneBrush.DrawRectangle((HudWidth * 0.5f - _lRashaSize * .5f) - _lRashaSize * 1.5f, HudHeight * RashaIndicatorsYpos, _lRashaSize * _lRashaSizeMod, _lRashaSize * _lRashaSizeMod);
            else DrawGreyBrush(-_lRashaSize * 1.5f);
 
            if (me.Powers.BuffIsActive(429855, 2)) ColdBrush.DrawRectangle((HudWidth * 0.5f - _lRashaSize * .5f) - _lRashaSize / 2, HudHeight * RashaIndicatorsYpos, _lRashaSize * _lRashaSizeMod, _lRashaSize * _lRashaSizeMod);
            else DrawGreyBrush(-_lRashaSize / 2);
 
            if (me.Powers.BuffIsActive(429855, 3)) FireBrush.DrawRectangle((HudWidth * 0.5f - _lRashaSize * .5f) + _lRashaSize / 2, HudHeight * RashaIndicatorsYpos, _lRashaSize * _lRashaSizeMod, _lRashaSize * _lRashaSizeMod);
            else DrawGreyBrush(_lRashaSize / 2);
 
            if (me.Powers.BuffIsActive(429855, 4)) LightningBrush.DrawRectangle((HudWidth * 0.5f - _lRashaSize * .5f) + _lRashaSize * 1.5f, HudHeight * RashaIndicatorsYpos, _lRashaSize * _lRashaSizeMod, _lRashaSize * _lRashaSizeMod);
            else DrawGreyBrush(_lRashaSize * 1.5f);
        }
 
        private void DrawGreyBrush(float xPos)
        {
            GreyBrush.DrawRectangle((HudWidth * 0.5f - _lRashaSize * .5f) + xPos, HudHeight * RashaIndicatorsYpos, _lRashaSize * _lRashaSizeMod, _lRashaSize * _lRashaSizeMod);
        }
 
        private void DrawWarnings(IPlayer me)
        {
            //IN ARCHON
            if (me.Powers.BuffIsActive(134872, 2)) //Archon
            {
                if (!me.Powers.BuffIsActive(135663, 0)) //Slow Time
                {
                    var layout = WarningFont.GetTextLayout("✽ " + /*me.Powers.GetBuff(135663).SnoPower.NameLocalized*/"Bubble Up" + " ✽"); // UsedWizardPowers returns "Archon" instead of "Slow Time" so i used GetBuff() instead
                    WarningFont.DrawText(layout, HudWidth * 0.5f - (layout.Metrics.Width * 0.5f), HudHeight * WarningYPos);
                }
            }
            else
            {//NOT IN ARCHON
                if (_magicWeaponSkill != null)
                {
                    var layout = WarningFont.GetTextLayout("❊ " + Hud.Sno.SnoPowers.Wizard_MagicWeapon.NameLocalized + " ❊");
                    if (!me.Powers.BuffIsActive(76108, 0))
                        WarningFont.DrawText(layout, HudWidth * 0.5f - (layout.Metrics.Width * 0.5f), HudHeight * WarningYPos);
                }
 
                if (_energyArmorSkill != null)
                {
                    var layout = WarningFont.GetTextLayout("❊ " + Hud.Sno.SnoPowers.Wizard_EnergyArmor.NameLocalized + " ❊");
                    if (!me.Powers.BuffIsActive(86991, 0) && !me.Powers.BuffIsActive(86991, 1) && !me.Powers.BuffIsActive(86991, 2) && !me.Powers.BuffIsActive(86991, 3) && !me.Powers.BuffIsActive(86991, 4) && !me.Powers.BuffIsActive(86991, 5))
                        WarningFont.DrawText(layout, HudWidth * 0.5f - (layout.Metrics.Width * 0.5f), HudHeight * (WarningYPos+WarningYPosIncr));
                }
/*Axyd added*/             
                if (_stormArmorSkill != null && ShowStormArmor == true)
                {
                    var layout = WarningFont.GetTextLayout("❊ " + Hud.Sno.SnoPowers.Wizard_StormArmor.NameLocalized + " ❊");
                    if (!me.Powers.BuffIsActive(74499, 0) && !me.Powers.BuffIsActive(74499, 1) && !me.Powers.BuffIsActive(74499, 2) && !me.Powers.BuffIsActive(74499, 3) && !me.Powers.BuffIsActive(74499, 4) && !me.Powers.BuffIsActive(74499, 5))
                        WarningFont.DrawText(layout, HudWidth * 0.5f - (layout.Metrics.Width * 0.5f), HudHeight * (WarningYPos+WarningYPosIncr));
                }                
               
                if (_iceArmorSkill != null && ShowIceArmor == true)
                {
                    var layout = WarningFont.GetTextLayout("❊ " + Hud.Sno.SnoPowers.Wizard_IceArmor.NameLocalized + " ❊");
                    if (!me.Powers.BuffIsActive(73223, 0) && !me.Powers.BuffIsActive(73223, 1) && !me.Powers.BuffIsActive(73223, 2) && !me.Powers.BuffIsActive(73223, 3) && !me.Powers.BuffIsActive(73223, 4) && !me.Powers.BuffIsActive(73223, 5))
                        WarningFont.DrawText(layout, HudWidth * 0.5f - (layout.Metrics.Width * 0.5f), HudHeight * (WarningYPos+WarningYPosIncr));
                }
               
                if (_familiarSkill != null && ShowFamiliar == true)
                {
                    var layout = WarningFont.GetTextLayout("❊ " + Hud.Sno.SnoPowers.Wizard_Familiar.NameLocalized + " ❊");
                    if (!me.Powers.BuffIsActive(99120, 0))
                        WarningFont.DrawText(layout, HudWidth * 0.5f - (layout.Metrics.Width * 0.5f), HudHeight * WarningYPos);
                }
/*add end*/
            }
        }
 
        private void UpdateSkills(IPlayer me)
        {
            _archonSkill = null;
            _magicWeaponSkill = null;
            _energyArmorSkill = null;
/*Axyd added*/
            _stormArmorSkill = null;
            _iceArmorSkill = null;
            _familiarSkill = null;
/*add end*/        
           
            me.Powers.UsedSkills.ForEach(skill =>
            {          
                if (skill.SnoPower.Sno == 134872) _archonSkill = skill;
                if (skill.SnoPower.Sno == 76108) _magicWeaponSkill = skill;
                if (skill.SnoPower.Sno == 86991) _energyArmorSkill = skill;
/*Axyd added*/
                if (skill.SnoPower.Sno == 74499) _stormArmorSkill = skill;
                if (skill.SnoPower.Sno == 73223) _iceArmorSkill = skill;
                if (skill.SnoPower.Sno == 99120) _familiarSkill = skill;
/*add end*/
            });
        }
    }
}