--[[
Author: Jarth
Filename: CBs_Button.lua
]] --
-------------------------------------------------------------------------------------------------
-- FUNCTIONS --
-------------------------------------------------------------------------------------------------
CBs_Button = ZO_Object:Subclass()

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

    if newB then
        local ctrl = CreateControlFromVirtual(_type.Name .. "_Button", frame, "CBs_Button", cId)
        newB.cId = cId
        newB.base = base
        newB.type = _type
        newB.saved = saved
        newB.ctrl = ctrl
        newB.ctrl.cId = cId
        newB.button = ctrl:GetNamedChild("Button")
        newB.button:SetId(cId)
        newB.button.tooltip = _type.Collection[cId].Name
        newB.button.CBs = true
        newB.icon = ctrl:GetNamedChild("Icon")
        newB.cooldownIcon = ctrl:GetNamedChild("CooldownIcon")
        newB.cooldownTime = ctrl:GetNamedChild("CooldownTime")
        newB.buttonText = ctrl:GetNamedChild("ButtonText")
        newB.cooldown = ctrl:GetNamedChild("Cooldown")
        newB.cooldownCompleteAnim = ctrl:GetNamedChild("CooldownCompleteAnimation")
        newB.status = ctrl:GetNamedChild("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(base)
    self.ctrl:SetHidden(false)

    local texture = self.type.Collection[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("OnClicked", function()
        base.Activate(self)
    end)
    self.button:SetHandler("OnMouseEnter", function()
        if self.playSounds then
            PlaySound(SOUNDS.QUICKSLOT_MOUSEOVER)
        end
        if self.type.Saved.Tooltip.Show then
            local text = self.type.Collection[self.cId].Name
            ZO_Tooltips_ShowTextTooltip(self.button, self.type.Saved.Tooltip.Position, text)
        end
    end)
    self.button:SetHandler("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 = self.base.Global.ReverseBindings[cId]
    if self.buttonText ~= nil then
        ZO_Keybindings_UnregisterLabelForBindingUpdate(self.buttonText)
        self.buttonText:ClearAnchors()
        self.buttonText:SetText("")
        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, string.format("%s_%s", self.base.Addon.Abbreviation, 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("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("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