--[[
Author: Jarth
Filename: CBs_Button.lua
]] --
-------------------------------------------------------------------------------------------------
-- VARIABLES --
-------------------------------------------------------------------------------------------------
local base = CollectionBars
local texts = base.Texts

-------------------------------------------------------------------------------------------------
-- FUNCTIONS --
-------------------------------------------------------------------------------------------------
CBs_Button = ZO_Object:Subclass()

function CBs_Button:New(_type, frame, cId, saved)
    local newB = ZO_Object.New(self)

    if newB then
        local lowdashButton = texts.Helpers.Lowdash .. texts.Components.Button
        local ctrl = CreateControlFromVirtual(_type.Name .. lowdashButton, frame, base.Addon.Abbreviation .. lowdashButton, cId)
        newB.cId = cId
        newB.type = _type
        newB.saved = saved
        newB.ctrl = ctrl
        newB.ctrl.cId = cId
        newB.button = ctrl:GetNamedChild(texts.Components.Button)
        newB.button:SetId(cId)
        newB.button.tooltip = _type.Category[cId].Name
        newB.button.CBs = true
        newB.icon = ctrl:GetNamedChild(texts.Components.Icon)
        newB.cooldownIcon = ctrl:GetNamedChild(texts.Components.Cooldown .. texts.Components.Icon)
        newB.cooldownTime = ctrl:GetNamedChild(texts.Components.Cooldown .. texts.Components.Time)
        newB.buttonText = ctrl:GetNamedChild(texts.Components.Button .. texts.Components.Text)
        newB.cooldown = ctrl:GetNamedChild(texts.Components.Cooldown)
        newB.cooldownCompleteAnim = ctrl:GetNamedChild(texts.Components.Cooldown .. texts.Components.Complete .. texts.Components.Animation)
        newB.status = ctrl:GetNamedChild(texts.Components.Status)
        newB.inCooldown = false
        newB.showingCooldown = false
        newB.playSounds = false
    end

    return newB
end

function CBs_Button:UpdateAnchor(frame, left, top)
    self.ctrl:ClearAnchors()
    self.ctrl:SetAnchor(TOPLEFT, frame, TOPLEFT, left, top)
end

function CBs_Button:Setup()
    self.ctrl:SetHidden(false)

    local texture = self.type.Category[self.cId].EnabledTexture
    self.icon:SetTexture(texture)
    self.icon:SetHidden(false)

    if self.cooldownIcon ~= nil then
        self.cooldownIcon:SetTexture(texture)
        self.cooldownIcon:SetDesaturation(1)
    end

    self.button:SetHandler(texts.Action.OnClicked, function() base.Activate(self) end)
    self.button:SetHandler(texts.Action.OnMouseEnter, function()
        if self.playSounds then PlaySound(SOUNDS.QUICKSLOT_MOUSEOVER) end
        if self.type.Saved.Tooltip.Show then
            local text = self.type.Category[self.cId].Name
            ZO_Tooltips_ShowTextTooltip(self.button, self.type.Saved.Tooltip.Position, text)
        end
    end)
    self.button:SetHandler(texts.Action.OnMouseExit, function() ZO_Tooltips_HideTextTooltip() end)
end

function CBs_Button:SetShowBindingText(visible) self.buttonText:SetHidden(not visible) end

function CBs_Button:SetSize(size)
    self.ctrl:SetHeight(size)
    self.ctrl:SetWidth(size)
    self.icon:SetHeight(size - 6)
    self.icon:SetWidth(size - 6)
end

function CBs_Button:SetBindingText(show, cId)
    local keyId = base.Global.ReverseBindings[cId]
    if self.buttonText ~= nil then
        ZO_Keybindings_UnregisterLabelForBindingUpdate(self.buttonText)
        self.buttonText:ClearAnchors()
        self.buttonText:SetText(texts.EmptyString)
        if keyId ~= nil and show then
            ZO_Keybindings_UnregisterLabelForBindingUpdate(self.buttonText)
            self.buttonText:SetHeight(self.ctrl:GetHeight())
            self.buttonText:SetWidth(self.ctrl:GetWidth())
            self.buttonText:SetAnchor(BOTTOM, self.ctrl, BOTTOM, 0, 0)
            ZO_Keybindings_RegisterLabelForBindingUpdate(self.buttonText, base.Addon.Abbreviation .. texts.Helpers.Lowdash .. keyId, false)
        end
    end
end

function CBs_Button:OnClicked() if self.usable then UseCollectible(self.cId) end end

function CBs_Button:UpdateState(forceId, isAttempting)
    local show = false
    if self.saved.IsActiveActivationEnabled then show = self.cId == forceId and isAttempting or not forceId and IsCollectibleActive(self.cId) end
    self.status:SetHidden(show == false)
end

function CBs_Button:UpdateUsable()
    local isShowingCooldown = self.showingCooldown
    local usable = false
    if not isShowingCooldown then usable = true end

    if usable ~= self.usable then self.usable = usable end
end

function CBs_Button:RefreshCooldown(remaining, duration, cooldown)
    local percentComplete = (1 - remaining / duration)

    self.icon.percentComplete = percentComplete
    self.cooldownTime:SetText(cooldown)
end

function CBs_Button:UpdatePlaySounds(playSounds) self.playSounds = playSounds end

function CBs_Button:UpdateCooldown(remaining, duration, cooldown)
    local showCooldown = duration > 0

    self.cooldown:SetHidden(not showCooldown)
    self.cooldownTime:SetHidden(not showCooldown)

    if showCooldown then
        self.cooldown:StartCooldown(remaining, duration, CD_TYPE_RADIAL, nil, NO_LEADING_EDGE)
        if self.cooldownCompleteAnim.animation then self.cooldownCompleteAnim.animation:GetTimeline():PlayInstantlyToStart() end
        self.cooldown:SetHidden(false)
        self.ctrl:SetHandler(texts.Action.OnUpdate, function() self:RefreshCooldown(remaining, duration, cooldown) end)
    else
        if self.showingCooldown then
            if self.playSounds then PlaySound(SOUNDS.ABILITY_READY) end

            self.cooldownCompleteAnim.animation = self.cooldownCompleteAnim.animation or CreateSimpleAnimation(ANIMATION_TEXTURE, self.cooldownCompleteAnim)
            self.cooldownCompleteAnim:SetHidden(false)
            self.cooldown:SetHidden(false)

            self.cooldownCompleteAnim.animation:SetImageData(16, 1)
            self.cooldownCompleteAnim.animation:SetFramerate(30)
            self.cooldownCompleteAnim.animation:GetTimeline():PlayFromStart()
        end

        self.icon.percentComplete = 1
        self.ctrl:SetHandler(texts.Action.OnUpdate, nil)
        self.cooldown:ResetCooldown()
    end

    if showCooldown ~= self.showingCooldown then
        self.showingCooldown = showCooldown

        if self.showingCooldown then
            ZO_ContextualActionBar_AddReference()
        else
            ZO_ContextualActionBar_RemoveReference()
        end
    end

    local textColor = showCooldown and INTERFACE_TEXT_COLOR_FAILED or INTERFACE_TEXT_COLOR_SELECTED
    self.buttonText:SetColor(GetInterfaceColor(INTERFACE_COLOR_TYPE_TEXT_COLORS, textColor))

    self:UpdateUsable()
end

function CBs_Button:SetHidden(value) self.ctrl:SetHidden(value) end