--Parent: Narci_EquipmentFlyoutFrame (Narcissus.xml)

local DefaultHeight_Comparison = 160
local DefaultHeight_StatsComparisonTemplate = 12;
local Format_Digit = "%.2f";
local BreakUpLargeNumbers = BreakUpLargeNumbers;

local LocalizedSlotName = {                                                                         --{Localized Name, InventorySlotName}
    [1]  = {HEADSLOT, "HeadSlot"},         [11] = {FINGER0SLOT_UNIQUE, "Finger0Slot"},
    [2]  = {NECKSLOT, "NeckSlot"},         [12] = {FINGER1SLOT_UNIQUE, "Finger1Slot"},
    [3]  = {SHOULDERSLOT, "ShoulderSlot"}, [13] = {TRINKET0SLOT_UNIQUE, "Trinket0Slot"},
    [4]  = {SHIRTSLOT, "ShirtSlot"},       [14] = {TRINKET1SLOT_UNIQUE, "Trinket1Slot"},
    [5]  = {CHESTSLOT, "ChestSlot"},       [15] = {BACKSLOT, "BackSlot"},
    [6]  = {WAISTSLOT, "WaistSlot"},       [16] = {MAINHANDSLOT, "MainHandSlot"},
    [7]  = {LEGSSLOT, "LegsSlot"},         [17] = {SECONDARYHANDSLOT, "SecondaryHandSlot"},
    [8]  = {FEETSLOT, "FeetSlot"},         [18] = {RANGEDSLOT, "AmmoSlot"},                             --Old
    [9]  = {WRISTSLOT, "WristSlot"},       [19] = {TABARDSLOT, "TabardSlot"},
    [10] = {HANDSSLOT, "HandsSlot"},
}

local CR_ConvertRatio = {      --Combat Rating number/percent
    stamina = 20,              -- 1 stamina = 20 HP
    }

local function SetCombatRatingRatio()
    local crit = math.max(GetCombatRating(CR_CRIT_MELEE), GetCombatRating(CR_CRIT_RANGED), GetCombatRating(CR_CRIT_SPELL));
    local critBonus = math.max(GetCombatRatingBonus(CR_CRIT_MELEE), GetCombatRatingBonus(CR_CRIT_RANGED), GetCombatRatingBonus(CR_CRIT_SPELL));
    CR_ConvertRatio.crit = critBonus / crit;

    local _, bonusCoeff = GetMasteryEffect();
    local masteryBonus = GetCombatRatingBonus(CR_MASTERY) * bonusCoeff;
    
    CR_ConvertRatio.haste = GetCombatRatingBonus(CR_HASTE_MELEE) / GetCombatRating(CR_HASTE_MELEE);
    CR_ConvertRatio.mastery = masteryBonus / GetCombatRating(CR_MASTERY);
    CR_ConvertRatio.versa = GetCombatRatingBonus(CR_VERSATILITY_DAMAGE_DONE) / GetCombatRating(CR_VERSATILITY_DAMAGE_DONE);

    --[[
    print(STAT_CRITICAL_STRIKE.." "..CR_ConvertRatio.crit)
    print(STAT_HASTE.." "..CR_ConvertRatio.haste)
    print(STAT_MASTERY.." "..CR_ConvertRatio.mastery)
    print(STAT_VERSATILITY.." "..CR_ConvertRatio.versa)
    --]]
end

local ColorTable = {
    Green = {r = 124 ,g = 197 ,b = 118},
    Red = {r = 255 ,g = 80 ,b = 80},
}

local function TextColor(Fontstring, color)
    local r, g, b = color.r/255, color.g/255, color.b/255
    Fontstring:SetTextColor(r, g, b);
end

local function ItemStats(itemLocation)
    local statsTable = {};

    if not C_Item.DoesItemExist(itemLocation) then
        statsTable.prim = 0;
        statsTable.stamina = 0;
        statsTable.crit = 0;
        statsTable.haste = 0;
        statsTable.mastery = 0;
        statsTable.versa = 0;
        statsTable.GemIcon = "";
        statsTable.GemPos = "";
        statsTable.ilvl = 0;
        return statsTable;
    end

    local ItemLevel = C_Item.GetCurrentItemLevel(itemLocation)
    local itemLink = C_Item.GetItemLink(itemLocation)
    local stats = GetItemStats(itemLink);
    local prim = stats["ITEM_MOD_AGILITY_SHORT"] or stats["ITEM_MOD_STRENGTH_SHORT"] or stats["ITEM_MOD_INTELLECT_SHORT"] or 0;
    local stamina = stats["ITEM_MOD_STAMINA_SHORT"] or 0;
    local crit = stats["ITEM_MOD_CRIT_RATING_SHORT"] or 0;
    local haste = stats["ITEM_MOD_HASTE_RATING_SHORT"] or 0;
    local mastery = stats["ITEM_MOD_MASTERY_RATING_SHORT"] or 0;
    local versa = stats["ITEM_MOD_VERSATILITY"] or 0;

    statsTable.prim = prim;
    statsTable.stamina = stamina;
    statsTable.crit = crit;
    statsTable.haste = haste;
    statsTable.mastery = mastery;
    statsTable.versa = versa;
    statsTable.ilvl = ItemLevel;

    --Calculate bonus from Gems and Enchants--

    local GemName, GemLink = GetItemGem(itemLink, 1)
    if GemLink then
        local GemID = GetItemInfoInstant(GemLink)
        local _, _, _, _, _, _, _, _, _, GemIcon, _, _, itemSubClassID = GetItemInfo(GemLink)
        statsTable.GemIcon = GemIcon
        local GemInfo
        if not Narci_GemInfo[GemID] then
            return statsTable;
        else
            GemInfo = Narci_GemInfo[GemID]
        end
        
        if GemInfo[1] == "crit" then
            statsTable.crit = statsTable.crit + GemInfo[2];
            statsTable.GemPos = "NarciTooltipCritNum";
        elseif GemInfo[1] == "haste" then
            statsTable.haste = statsTable.haste + GemInfo[2];
            statsTable.GemPos = "NarciTooltipHasteNum";
        elseif GemInfo[1] == "mastery" then
            statsTable.mastery = statsTable.mastery + GemInfo[2];
            statsTable.GemPos = "NarciTooltipMasteryNum";
        elseif GemInfo[1] == "versa" then
            statsTable.versa = statsTable.versa + GemInfo[2];
            statsTable.GemPos = "NarciTooltipVersaNum";
        elseif GemInfo[1] == "AGI" or GemInfo[1] == "STR" or GemInfo[1] == "INT" then
            statsTable.prim = statsTable.prim + GemInfo[2];
            statsTable.GemPos = "NarciTooltipPrimNum";
        end
    end

    return statsTable;
end

local function DisplayGearBonus()

end

local function DisplayComparison(Textframe, name, number, baseNumber, ratio, CustomColor)
    if not number then            --Set Number to "-"
        Textframe.Arrow:Hide();
        Textframe.NumDiff:Hide();
        Textframe.Num:SetText("-");
        return;
    end

    local differentialNumber = tonumber(number) - tonumber(baseNumber)

    if differentialNumber > 0 then
        Textframe.Arrow:Show()
        Textframe.Arrow:SetTexCoord(0, 0.5, 0, 1)

        Textframe.NumDiff:Show();
        Textframe.PctDiff:Show();
        TextColor(Textframe.NumDiff, ColorTable.Green)
        TextColor(Textframe.PctDiff, ColorTable.Green)
    elseif differentialNumber < 0 then
        Textframe.Arrow:Show()
        Textframe.Arrow:SetTexCoord(0.5, 1, 0, 1)

        Textframe.NumDiff:Show();
        Textframe.PctDiff:Show();
        TextColor(Textframe.NumDiff, ColorTable.Red)
        TextColor(Textframe.PctDiff, ColorTable.Red)
    else
        Textframe.Arrow:Hide()
        Textframe.NumDiff:Hide();
        Textframe.PctDiff:Hide();
    end

    differentialNumber = math.abs(differentialNumber)

    Textframe.Label:SetText(name)
    if number ~= 0 then
        Textframe.Num:SetText(number);
        --Textframe:SetHeight(DefaultHeight_StatsComparisonTemplate);
        Textframe:Show();
    else
        Textframe.Num:SetText("-");
        --Textframe:SetHeight(0);
        --Textframe:Hide();
    end
    Textframe.NumDiff:SetText(differentialNumber);

    if CustomColor then
        Textframe.Label:SetTextColor(CustomColor[1], CustomColor[2], CustomColor[3])
    else
        Textframe.Label:SetTextColor(1, 0.96, 0.41)
    end

    if ratio then
        if name ~= ITEM_MOD_STAMINA_SHORT then
            Textframe.PctDiff:SetText(string.format(Format_Digit, ratio*differentialNumber).."%");
        else
            Textframe.PctDiff:SetText(BreakUpLargeNumbers(ratio*differentialNumber));
        end
    else
        Textframe.PctDiff:SetText("");
    end
end

local function EmptyComparison()
    DisplayComparison(NarciTooltipPrim, SPEC_FRAME_PRIMARY_STAT);
    DisplayComparison(NarciTooltipStamina, ITEM_MOD_STAMINA_SHORT);
    DisplayComparison(NarciTooltipCrit, STAT_CRITICAL_STRIKE);
    DisplayComparison(NarciTooltipHaste, STAT_HASTE);
    DisplayComparison(NarciTooltipMastery, STAT_MASTERY);
    DisplayComparison(NarciTooltipVersa, STAT_VERSATILITY);
end

function NarciTooltip_SetComparison(itemLocation)
    local frame = NarciTooltip;
    if not C_Item.DoesItemExist(itemLocation) then
        frame.ItemName:SetText(EMPTY);
        frame.ItemName:SetTextColor(0.6, 0.6, 0.6);
        frame.EquipLoc:SetText(LocalizedSlotName[Narci_EquipmentFlyoutFrame.slotID][1])
        
        local _, textureName = GetInventorySlotInfo(LocalizedSlotName[Narci_EquipmentFlyoutFrame.slotID][2])
        frame.Icon:SetTexture(textureName);
        return;
    end
    --print("location"..C_Item.GetItemInventoryType(itemLocation))
    local itemIcon = C_Item.GetItemIcon(itemLocation);
    local name = C_Item.GetItemName(itemLocation);
    local quality = C_Item.GetItemQuality(itemLocation);
    local r, g, b = GetItemQualityColor(quality);

    local stats = ItemStats(itemLocation);
    local primName = Narci_GetPrimaryStatusName();

    local baseStats = ItemStats(Narci_EquipmentFlyoutFrame.BaseItem);

    frame.ItemName:SetText(name);
    frame.ItemName:SetTextColor(r, g, b);
    frame.Icon:SetTexture(itemIcon);
    frame.EquipLoc:SetText(LocalizedSlotName[Narci_EquipmentFlyoutFrame.slotID][1])

    DisplayComparison(NarciTooltipIlvl, STAT_AVERAGE_ITEM_LEVEL, stats.ilvl, baseStats.ilvl, nil, {1, 0.82, 0});
    DisplayComparison(NarciTooltipPrim, primName, stats.prim, baseStats.prim, nil, {1, 1, 1});
    DisplayComparison(NarciTooltipStamina, ITEM_MOD_STAMINA_SHORT, stats.stamina, baseStats.stamina, CR_ConvertRatio.stamina, {1, 1, 1});
    DisplayComparison(NarciTooltipCrit, STAT_CRITICAL_STRIKE, stats.crit, baseStats.crit, CR_ConvertRatio.crit);
    DisplayComparison(NarciTooltipHaste, STAT_HASTE, stats.haste, baseStats.haste, CR_ConvertRatio.haste);
    DisplayComparison(NarciTooltipMastery, STAT_MASTERY, stats.mastery, baseStats.mastery, CR_ConvertRatio.mastery);
    DisplayComparison(NarciTooltipVersa, STAT_VERSATILITY, stats.versa, baseStats.versa, CR_ConvertRatio.versa);

    local itemLink = C_Item.GetItemLink(itemLocation)
    RefVirtualTooltip:SetHyperlink(itemLink)

    if stats.GemIcon and stats.GemPos then
        frame.BonusIcon1:SetTexture(stats.GemIcon);
        frame.BonusIcon1:ClearAllPoints();
        frame.BonusIcon1:SetPoint("LEFT", stats.GemPos, "RIGHT", 4, 0);
        frame.BonusIcon1:Show();
    else
        frame.BonusIcon1:Hide();
    end

    frame:SetFrameStrata("TOOLTIP")
end

local NT = CreateFrame("Frame");
NT:RegisterEvent("VARIABLES_LOADED");
NT:RegisterEvent("PLAYER_LEVEL_UP");
NT:RegisterEvent("ACTIVE_TALENT_GROUP_CHANGED");
NT:SetScript("OnEvent",function(self,event,...)
    C_Timer.After(4, function()
        SetCombatRatingRatio()
    end)
end)

function PrintStats()
    --GetItemStats(C_Item.GetItemLink(ItemLocation:CreateFromEquipmentSlot(11)))
    local _, GemLink = GetItemGem(C_Item.GetItemLink(ItemLocation:CreateFromEquipmentSlot(11)), 1)
    print(GemLink)
    GemStats = GetItemStats(GemLink);
end


function NarciTooltip_Resize()
    local frame = NarciTooltip;
    local extraHeight = math.floor(frame.PawnText:GetHeight() + frame.ItemName:GetHeight() + 0.5)
    frame:SetHeight(DefaultHeight_Comparison + extraHeight)
    frame.Icon:SetWidth(frame.Background:GetHeight());
end


