local BaseTipsComp = class("BaseTipsComp", LuaComponent)

------------------------------------ override ------------------------------------
---- 刷新时调用
function BaseTipsComp:refresh(params)

end

---- 界面关闭时会主动调此方法
function BaseTipsComp:onUIClose()

end

------------------------------------ end override ------------------------------------

---- pop ui相同的动画,需要主动调用
function BaseTipsComp:showPopAni(callback)
    GFunc.showPopAni(self:getBaseObject(), callback)
end

function BaseTipsComp:getOffsetX()
    return 10
end

function BaseTipsComp:getOffsetY()
    return 10
end

function BaseTipsComp:getPosPercentX()
    return 0.25
end

function BaseTipsComp:getPosPercentY()
    return 0.15
end

function BaseTipsComp:getFinalX()
    return 0
end

function BaseTipsComp:getFinalY()
    return 0
end

function BaseTipsComp:locate(location, originSizeDelta, node, tarCornerScreenPos)
    local uiCamera = UIManager:getUICameraComponent()
    local tarCornerLp = CS.BF.Utils.RectTransformScreenPointToLocalPointInRectangle(UIManager:getMainCanvasTransform(), tarCornerScreenPos.x, tarCornerScreenPos.y, uiCamera)
    if location == ModuleManager.TipsManager.ALIGN_TYPE.TOP_CENTER then
        node:getTransform().pivot = BF.Vector2(0.5, 0)
        node:addLocalPosition(tarCornerLp.x, tarCornerLp.y + self:getOffsetY(), 0)
        self:adaptWhenOverScreen(node, 0.5, 0)
    elseif location == ModuleManager.TipsManager.ALIGN_TYPE.TOP_LEFT then
        node:getTransform().pivot = BF.Vector2(0, 0)
        node:addLocalPosition(tarCornerLp.x - node:getTransform().sizeDelta.x*self:getPosPercentX(), tarCornerLp.y + self:getOffsetY(), 0)
        self:adaptWhenOverScreen(node, 0, 0)
    elseif location == ModuleManager.TipsManager.ALIGN_TYPE.TOP_RIGHT then
        node:getTransform().pivot = BF.Vector2(1, 0)
        node:addLocalPosition(tarCornerLp.x + node:getTransform().sizeDelta.x*self:getPosPercentX(), tarCornerLp.y + self:getOffsetY(), 0)
        self:adaptWhenOverScreen(node, 1, 0)
    elseif location == ModuleManager.TipsManager.ALIGN_TYPE.BOTTOM_CENTER then
        node:getTransform().pivot = BF.Vector2(0.5, 1)
        node:addLocalPosition(tarCornerLp.x, tarCornerLp.y - self:getOffsetY(), 0)
        self:adaptWhenOverScreen(node, 0.5, 1)
    elseif location == ModuleManager.TipsManager.ALIGN_TYPE.BOTTOM_LEFT then
        node:getTransform().pivot = BF.Vector2(0, 1)
        node:addLocalPosition(tarCornerLp.x - node:getTransform().sizeDelta.x*self:getPosPercentX(), tarCornerLp.y - self:getOffsetY(), 0)
        self:adaptWhenOverScreen(node, 0, 1)
    elseif location == ModuleManager.TipsManager.ALIGN_TYPE.BOTTOM_RIGHT then
        node:getTransform().pivot = BF.Vector2(1, 1)
        node:addLocalPosition(tarCornerLp.x + node:getTransform().sizeDelta.x*self:getPosPercentX(), tarCornerLp.y - self:getOffsetY(), 0)
        self:adaptWhenOverScreen(node, 1, 1)
    elseif location == ModuleManager.TipsManager.ALIGN_TYPE.LEFT_TOP then
        node:getTransform().pivot = BF.Vector2(1, 0.5)
        local posy = node:getTransform().sizeDelta.y/2 - originSizeDelta.y*(0.5 - self:getPosPercentY())
        node:addLocalPosition(tarCornerLp.x - self:getOffsetX(), tarCornerLp.y - posy, 0)
        self:adaptWhenOverScreen(node, 1, 0.5)
    elseif location == ModuleManager.TipsManager.ALIGN_TYPE.LEFT_CENTER then
        node:getTransform().pivot = BF.Vector2(1, 0.5)
        node:addLocalPosition(tarCornerLp.x - self:getOffsetX(), tarCornerLp.y, 0)
        self:adaptWhenOverScreen(node, 1, 0.5)
    elseif location == ModuleManager.TipsManager.ALIGN_TYPE.LEFT_BOTTOM then
        node:getTransform().pivot = BF.Vector2(1, 0.5)
        local posy = node:getTransform().sizeDelta.y/2 - originSizeDelta.y*(0.5 - self:getPosPercentY())
        node:addLocalPosition(tarCornerLp.x - self:getOffsetX(), tarCornerLp.y + posy, 0)
        self:adaptWhenOverScreen(node, 1, 0.5)
    elseif location == ModuleManager.TipsManager.ALIGN_TYPE.RIGHT_TOP then
        node:getTransform().pivot = BF.Vector2(0, 0.5)
        local posy = node:getTransform().sizeDelta.y/2 - originSizeDelta.y*(0.5 - self:getPosPercentY())
        node:addLocalPosition(tarCornerLp.x + self:getOffsetX(), tarCornerLp.y - posy, 0)
        self:adaptWhenOverScreen(node, 0, 0.5)
    elseif location == ModuleManager.TipsManager.ALIGN_TYPE.RIGHT_CENTER then
        node:getTransform().pivot = BF.Vector2(0, 0.5)
        node:addLocalPosition(tarCornerLp.x + self:getOffsetX(), tarCornerLp.y, 0)
        self:adaptWhenOverScreen(node, 0, 0.5)
    elseif location == ModuleManager.TipsManager.ALIGN_TYPE.RIGHT_BOTTOM then
        node:getTransform().pivot = BF.Vector2(0, 0.5)
        local posy = node:getTransform().sizeDelta.y/2 - originSizeDelta.y*(0.5 - self:getPosPercentY())
        node:addLocalPosition(tarCornerLp.x + self:getOffsetX(), tarCornerLp.y + posy, 0)
        self:adaptWhenOverScreen(node, 0, 0.5)
    end

    self:finalSetPos(node)
end

function BaseTipsComp:adaptWhenOverScreen(node, pivotX, pivotY)
    local halfWidth = GConst.DESIGN_RESOLUTION_WIDTH / 2
    local halfHeight = GConst.DESIGN_RESOLUTION_HEIGHT / 2
    local anchoredPos = node:getAnchoredPosition()
    local sizeDelta = node:getSizeDelta()
    local leftX = anchoredPos.x - sizeDelta.x * (pivotX - 0)
    local rightX = anchoredPos.x + sizeDelta.x * (1 - pivotX)
    local topY = anchoredPos.y + sizeDelta.y * (1 - pivotY)
    local bottomY = anchoredPos.y - sizeDelta.y * (pivotY - 0)
    -- 左边超出屏幕
    if math.abs(leftX) > halfWidth then
        node:addAnchoredPosition(math.abs(leftX) - halfWidth, 0)
    end
    -- 右边超出屏幕
    if math.abs(rightX) > halfWidth then
        node:addAnchoredPosition(halfWidth - math.abs(rightX), 0)
    end
    -- 上边超出屏幕
    if math.abs(topY) > halfHeight then
        node:addAnchoredPosition(0, halfHeight - math.abs(topY))
    end
    -- 下边超出屏幕
    if math.abs(bottomY) > halfHeight then
        node:addAnchoredPosition(0, math.abs(bottomY) - halfHeight)
    end
end

function BaseTipsComp:finalSetPos(node)
    node:addLocalPosition(self:getFinalX(), self:getFinalY(), 0)
end

return BaseTipsComp