--[[============================================================================
  This file is a part of ChampionBar

  Copyright (C) 2015 Scott Yeskie (Sasky)

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  ============================================================================]]

---
--- Overwrite the VPBarType to show CP in the main bar
---
ChampionBar = ChampionBar or {}
local VPMaxBarType = ZO_Object:Subclass()
ChampionBar.VPMaxBarType = VPMaxBarType

function VPMaxBarType:New(barTypeClass, barTypeId, ...)
    local obj = ZO_Object.New(self)
    obj.barTypeClass = barTypeClass
    obj.barTypeId = barTypeId
    obj.params = {...}
    obj:Initialize()
    obj:InitializeLastValues()
    return obj
end

function VPMaxBarType:Initialize()
    self.barGradient = ZO_CP_BAR_GRADIENT_COLORS[self:GetShownAttribute()]
    self.barGlowColor = ZO_CP_BAR_GRADIENT_COLORS[self:GetShownAttribute()][2]
    self.levelTypeText = GetString(SI_EXPERIENCE_CHAMPION_RANK_LABEL)
    self.tooltipCurrentMaxFormat = SI_CHAMPION_POINTS_CURRENT_MAX
    self.icon = self:GetIcon()
end

function VPMaxBarType:InitializeLastValues()
    self.lastLevel = self:GetLevel()
    self.lastCurrent = self:GetCurrent()
end

function VPMaxBarType:Equals(barTypeClass, ...)
    if(self.barTypeClass == barTypeClass) then
        for i = 1, select("#", ...) do
            local param = select(i, ...)
            if(param ~= self.params[i]) then
                return false
            end
        end
        return true
    end
    return false
end

function VPMaxBarType:GetEnlightenedPool()
    return 0
end

function VPMaxBarType:GetEnlightenedTooltip()
    return nil
end

function VPMaxBarType:GetSecondaryBarType()
    return PPB_EP
end

function VPMaxBarType:GetLevelSize(rank)
    return GetChampionXPInRank(rank)
end

function VPMaxBarType:GetLevel()
    return GetPlayerChampionPointsEarned()
end

function VPMaxBarType:GetCurrent()
    return GetPlayerChampionXP()
end

function VPMaxBarType:GetBarGradient()
    return ZO_CP_BAR_GRADIENT_COLORS[self:GetShownAttribute()]
end

function VPMaxBarType:GetShownAttribute()
    local level = self:GetLevel()
    if self:GetLevelSize(level) ~= nil then
        level = level + 1
    end
    return GetChampionPointAttributeForRank(level)
end

function VPMaxBarType:GetLevelTypeText()
    if not IsInGamepadPreferredMode() then
        return self.levelTypeText
    end
end

local CHAMPION_ATTRIBUTE_HUD_ICONS =
{
    [ATTRIBUTE_HEALTH] = "EsoUI/Art/Champion/champion_points_health_icon-HUD-32.dds",
    [ATTRIBUTE_MAGICKA] = "EsoUI/Art/Champion/champion_points_magicka_icon-HUD-32.dds",
    [ATTRIBUTE_STAMINA] = "EsoUI/Art/Champion/champion_points_stamina_icon-HUD-32.dds",
}

function VPMaxBarType:GetIcon()
    return CHAMPION_ATTRIBUTE_HUD_ICONS[self:GetShownAttribute()]
end