using System;
using System.Collections.Generic;
using Turbo.Plugins.Default;
 
namespace Turbo.Plugins.User
{
    public class DAV_UsefulCubeItems : BasePlugin, IInGameTopPainter {
        public bool BiLanguage { get; set; } = true; // also add NameEnglish for item Sno if TRUE
        public IFont TextFont { get; set; }
        public IBrush DefaultBrush { get; set; }
        public Dictionary<string, IBrush> HighLightBrushs { get; set; } = new Dictionary<string, IBrush>();
 
        private IUiElement cubeSlot { get; set; }
        private List<IUiElement> CubeSlots { get; set; }
        private Dictionary<string, string> [] HighLightItems{ get; set; } = new Dictionary<string, string> [7];
 
        public DAV_UsefulCubeItems() {
            Enabled = true;
            for (var i = 0; i < 7; i++)
                HighLightItems[i] = new Dictionary<string, string> ();
        }
 
        public void PaintTopInGame(ClipState clipState) {
            if (clipState != ClipState.Inventory) return;
            if (!cubeSlot.Visible) return;
 
            var myList = HighLightItems[(int) Hud.Game.Me.HeroClassDefinition.HeroClass];
            foreach (var Slot in CubeSlots) {
                if (Slot == null || !Slot.Visible) continue;
 
                var itemName = Slot.ReadText(System.Text.Encoding.UTF8, true);
                if (string.IsNullOrEmpty(itemName)) continue;
 
                if (myList.TryGetValue(itemName, out var profileN)) {
                    var usedBrush = HighLightBrushs.ContainsKey(profileN) ? HighLightBrushs[profileN] : DefaultBrush;
                    usedBrush.DrawRectangle(Slot.Rectangle);
                    TextFont.DrawText(profileN, Slot.Rectangle.X + 4, Slot.Rectangle.Y + 2);
                }
            }
        }
 
        public override void Load(IController hud) {
            base.Load(hud);
 
            TextFont = Hud.Render.CreateFont("Arial", 8, 255, 225, 255, 102, true, false, true);
            DefaultBrush = Hud.Render.CreateBrush(255, 255, 255, 51, 4f);
            HighLightBrushs.Add("Uliana", Hud.Render.CreateBrush(255, 255, 128, 51, 4f));
            HighLightBrushs.Add("ZDPS", Hud.Render.CreateBrush(255, 51, 255, 51, 4f, SharpDX.Direct2D1.DashStyle.DashDot));
 
        // Common Gears
            AddUsefulCubeItem("ZDPS", 3107424000, HeroClass.Barbarian); // 核眼之戒 Oculus Ring
            AddUsefulCubeItem("ZDPS", 3107424000, HeroClass.Monk); // 核眼之戒 Oculus Ring
            AddUsefulCubeItem("ZDPS", 923327011, HeroClass.Monk); // 強橫護腕 Strongarm Bracers
            AddUsefulCubeItem("ZDPS", 595616090, HeroClass.None); // 劊子手 The Executioner
            AddUsefulCubeItem("ZDPS", 595616121, HeroClass.None); // 梅希斯密特之劫掠者 Messerschmidt's Reaver
            AddUsefulCubeItem("General", 962426467, HeroClass.None); // 死敵護腕 Nemesis Bracers
            AddUsefulCubeItem("General", 3145373733, HeroClass.None); // 皇家華戒 Ring of Royal Grandeur
            AddUsefulCubeItem("General", 1979309080, HeroClass.None); // 虛幻長靴 Illusory Boots
            AddUsefulCubeItem("Speed", 1771898421, HeroClass.None); // 黃金裹腰 Goldwrap
            AddUsefulCubeItem("Speed", 3145409670, HeroClass.None); // 貪婪指環 Avarice Band
 
        // Monk
            AddUsefulCubeItem("General", 446651865, HeroClass.Monk); // 翔龍 Flying Dragon
            AddUsefulCubeItem("Uliana", 4061835217, HeroClass.Monk); // 猛獅之爪 Lion's Claw
            AddUsefulCubeItem("Uliana", 400756501, HeroClass.Monk); // 失蹤者的羈絆 Binding of the Lost
            AddUsefulCubeItem("Uliana", 1935108657, HeroClass.Monk); // 狂石 Madstone
            AddUsefulCubeItem("Uliana", 1861699992, HeroClass.Monk); // 弓術護腕 Gungdo Gear
            AddUsefulCubeItem("Uliana", 1484349569, HeroClass.Monk); // 永恆之悟 The Flow of Eternity
            AddUsefulCubeItem("Uliana", 3074252290, HeroClass.Monk); // 厄圖拉斯之拳 The Fist of Az'Turrasq
 
            cubeSlot = Hud.Render.GetUiElement("Root.NormalLayer.Kanais_Collection");
            CubeSlots = new List<IUiElement> {
                Hud.Render.RegisterUiElement("Root.NormalLayer.Kanais_Collection.LayoutRoot.Kanais_Power_Select.Kanais_Collection_Powers.CollectionList._content._stackpanel._tilerow0._item0.ItemPowerTitle", cubeSlot, null),
                Hud.Render.RegisterUiElement("Root.NormalLayer.Kanais_Collection.LayoutRoot.Kanais_Power_Select.Kanais_Collection_Powers.CollectionList._content._stackpanel._tilerow0._item1.ItemPowerTitle", cubeSlot, null),
                Hud.Render.RegisterUiElement("Root.NormalLayer.Kanais_Collection.LayoutRoot.Kanais_Power_Select.Kanais_Collection_Powers.CollectionList._content._stackpanel._tilerow0._item2.ItemPowerTitle", cubeSlot, null),
                Hud.Render.RegisterUiElement("Root.NormalLayer.Kanais_Collection.LayoutRoot.Kanais_Power_Select.Kanais_Collection_Powers.CollectionList._content._stackpanel._tilerow1._item4.ItemPowerTitle", cubeSlot, null),
                Hud.Render.RegisterUiElement("Root.NormalLayer.Kanais_Collection.LayoutRoot.Kanais_Power_Select.Kanais_Collection_Powers.CollectionList._content._stackpanel._tilerow1._item5.ItemPowerTitle", cubeSlot, null),
                Hud.Render.RegisterUiElement("Root.NormalLayer.Kanais_Collection.LayoutRoot.Kanais_Power_Select.Kanais_Collection_Powers.CollectionList._content._stackpanel._tilerow1._item6.ItemPowerTitle", cubeSlot, null),
                Hud.Render.RegisterUiElement("Root.NormalLayer.Kanais_Collection.LayoutRoot.Kanais_Power_Select.Kanais_Collection_Powers.CollectionList._content._stackpanel._tilerow2._item8.ItemPowerTitle", cubeSlot, null),
                Hud.Render.RegisterUiElement("Root.NormalLayer.Kanais_Collection.LayoutRoot.Kanais_Power_Select.Kanais_Collection_Powers.CollectionList._content._stackpanel._tilerow2._item9.ItemPowerTitle", cubeSlot, null),
                Hud.Render.RegisterUiElement("Root.NormalLayer.Kanais_Collection.LayoutRoot.Kanais_Power_Select.Kanais_Collection_Powers.CollectionList._content._stackpanel._tilerow2._item10.ItemPowerTitle", cubeSlot, null),
                Hud.Render.RegisterUiElement("Root.NormalLayer.Kanais_Collection.LayoutRoot.Kanais_Power_Select.Kanais_Collection_Powers.CollectionList._content._stackpanel._tilerow3._item12.ItemPowerTitle", cubeSlot, null),
                Hud.Render.RegisterUiElement("Root.NormalLayer.Kanais_Collection.LayoutRoot.Kanais_Power_Select.Kanais_Collection_Powers.CollectionList._content._stackpanel._tilerow3._item13.ItemPowerTitle", cubeSlot, null),
                Hud.Render.RegisterUiElement("Root.NormalLayer.Kanais_Collection.LayoutRoot.Kanais_Power_Select.Kanais_Collection_Powers.CollectionList._content._stackpanel._tilerow3._item14.ItemPowerTitle", cubeSlot, null),
                Hud.Render.RegisterUiElement("Root.NormalLayer.Kanais_Collection.LayoutRoot.Kanais_Power_Select.Kanais_Collection_Powers.CollectionList._content._stackpanel._tilerow4._item16.ItemPowerTitle", cubeSlot, null),
                Hud.Render.RegisterUiElement("Root.NormalLayer.Kanais_Collection.LayoutRoot.Kanais_Power_Select.Kanais_Collection_Powers.CollectionList._content._stackpanel._tilerow4._item17.ItemPowerTitle", cubeSlot, null),
                Hud.Render.RegisterUiElement("Root.NormalLayer.Kanais_Collection.LayoutRoot.Kanais_Power_Select.Kanais_Collection_Powers.CollectionList._content._stackpanel._tilerow4._item18.ItemPowerTitle", cubeSlot, null),
                Hud.Render.RegisterUiElement("Root.NormalLayer.Kanais_Collection.LayoutRoot.Kanais_Power_Select.Kanais_Collection_Powers.CollectionList._content._stackpanel._tilerow5._item20.ItemPowerTitle", cubeSlot, null),
                Hud.Render.RegisterUiElement("Root.NormalLayer.Kanais_Collection.LayoutRoot.Kanais_Power_Select.Kanais_Collection_Powers.CollectionList._content._stackpanel._tilerow5._item21.ItemPowerTitle", cubeSlot, null),
                Hud.Render.RegisterUiElement("Root.NormalLayer.Kanais_Collection.LayoutRoot.Kanais_Power_Select.Kanais_Collection_Powers.CollectionList._content._stackpanel._tilerow5._item22.ItemPowerTitle", cubeSlot, null),
            };
        }
 
        public void AddUsefulCubeItem(string buildName, string itemName, HeroClass myHero = HeroClass.None) {
            if (myHero == HeroClass.None) {
                for (var i = 0; i < 7; i++)
                    HighLightItems[i].Add(itemName, buildName);
            }
            else HighLightItems[(int) myHero].Add(itemName, buildName);
        }
 
        public void AddUsefulCubeItem(string buildName, uint itemSno, HeroClass myHero = HeroClass.None) {
            var usefulItem = Hud.Inventory.GetSnoItem(itemSno);
            if (usefulItem != null) {
                AddUsefulCubeItem(buildName, usefulItem.NameLocalized, myHero);
                if (BiLanguage)
                    AddUsefulCubeItem(buildName, usefulItem.NameEnglish, myHero);
            }
        }
    }
}