local SideBarBaseCell = class("SideBarBaseCell", BaseCell)

local DEFAULT_RED_POINT_POS_X = 30
local DEFAULT_RED_POINT_POS_Y = 30
local DEFAULT_RED_POINT_SCALE = 1

-- 需要继承重写的部分 ***********************************************************

-- 模块key值，对应ModuleManager.MODULE_KEY
function SideBarBaseCell:getModuleKey()
    return nil
end

function SideBarBaseCell:getIsOpen()
    return false
end

-- 是否有内购
function SideBarBaseCell:getHasPurchase()
    return true
end

function SideBarBaseCell:onClick()
end

function SideBarBaseCell:updateTime()
end

function SideBarBaseCell:onRefresh()
end

-- icon图标资源路径,为空时不显示
function SideBarBaseCell:getIconRes()
    return nil
end

function SideBarBaseCell:getIconSize()
    return nil
end

-- 图标动画资源路径,为空时不显示
function SideBarBaseCell:getSpineName()
    return nil
end

-- 图标动画资源默认名称
function SideBarBaseCell:getSpineAnimationName()
    return "idle"
end

-- 图标动画默认位置
function SideBarBaseCell:getSpinePositionY()
    return 0
end

-- 是否有shake效果
function SideBarBaseCell:getHasShake()
    return false
end

function SideBarBaseCell:getIsShowRedPoint()
    return false
end

-- 红点偏移位置
function SideBarBaseCell:getRedPointPosition()
    return DEFAULT_RED_POINT_POS_X, DEFAULT_RED_POINT_POS_Y
end

-- 红点大小
function SideBarBaseCell:getRedPointScale()
    return DEFAULT_RED_POINT_SCALE
end

-- 广告红点偏移位置
function SideBarBaseCell:getAdPointPosition()
    return DEFAULT_RED_POINT_POS_X, DEFAULT_RED_POINT_POS_Y
end

-- 广告红点大小
function SideBarBaseCell:getAdPointScale()
    return DEFAULT_RED_POINT_SCALE
end

function SideBarBaseCell:getCellName()
end

-- 常规逻辑 ***********************************************************

function SideBarBaseCell:init()
    local uiMap = self.baseObject:genAllChildren()
    self.imgIcon = uiMap["side_bar_base_cell.img_icon"]
    self.spineIcon = uiMap["side_bar_base_cell.spine_icon"]
    self.timeBg = uiMap["side_bar_base_cell.time"]
    self.txTime = uiMap["side_bar_base_cell.time.tx_time"]
    self.txName = uiMap["side_bar_base_cell.tx_name"]
    -- -- 金猪
    -- self.nodePig = uiMap["side_bar_base_cell.node_pig"]
    -- self.nodePigIcon = uiMap["side_bar_base_cell.node_pig.icon"]
    -- self.nodePigDesc = uiMap["side_bar_base_cell.node_pig.desc"]
    -- -- 功能预览
    -- self.nodeFuncForeshow = uiMap["side_bar_base_cell.node_func_foreshow"]
    -- self.nodeFuncForeshowLock = uiMap["side_bar_base_cell.node_func_foreshow.img_lock"]
    -- self.nodeFuncForeshowName = uiMap["side_bar_base_cell.node_func_foreshow.tx_name"]
    -- self.nodeFuncForeshowDesc = uiMap["side_bar_base_cell.node_func_foreshow.tx_desc"]

    self.timeBg:setActive(false)
    self.baseObject:addClickListener(function()
        self:onClick()
    end)
    self:bindData()
end

function SideBarBaseCell:bindData()
end

function SideBarBaseCell:refresh()
    self:refreshIcon()
    self:refreshName()
    self:refreshSpine()
    self:refreshShake()
    self:onRefresh()

    -- 额外
    -- self:refreshNodePig()
    -- self:refreshNodeFuncForeshow()
end

-- function SideBarBaseCell:refreshNodePig()
--     self.nodePig:setActive(false)
-- end

-- function SideBarBaseCell:refreshNodeFuncForeshow()
--     self.nodeFuncForeshow:setActive(false)
-- end

function SideBarBaseCell:refreshIcon()
    if not self:getIconRes() then
        self.imgIcon:setActive(false)
        return
    end
    self.imgIcon:setActive(true)
    self.imgIcon:setSprite(GConst.ATLAS_PATH.UI_MAIN, self:getIconRes())
    local width = self:getIconSize()
    if width then
        self.imgIcon:setSizeDelta(width, width)
    end
end

function SideBarBaseCell:refreshName()
    if self:getCellName() then
        self.txName:setText(self:getCellName())
    else
        self.txName:setText(GConst.EMPTY_STRING)
    end
end

function SideBarBaseCell:refreshSpine()
    if not self:getSpineName() then
        self.spineIcon:getSkeletonGraphic().enabled = false
        return
    end
    self.spineIcon:setAnchoredPositionY(self:getSpinePositionY())
    if not self.isInitSpine then
        self.isInitSpine = true
        self.spineIcon:loadAssetAsync(self:getSpineName(), function()
            self.spineIcon:playAnim(self:getSpineAnimationName(), true, true, true)
            if self.isActive then
                self.spineIcon:setActive(true)
            else
                self.spineIcon:setActive(false)
            end
            if self.isVisible then
                self.spineIcon:getSkeletonGraphic().enabled = true
            else
                self.spineIcon:getSkeletonGraphic().enabled = false
            end
        end, false)
    else
        if not self.spineIcon:getIsLoadingAsset() then
            self.spineIcon:getSkeletonGraphic().enabled = true
        end
    end
end

function SideBarBaseCell:refreshShake()
    if self:getHasShake() then
        self:doShake()
    else
        self:stopShake()
    end
end

function SideBarBaseCell:doShake()
    if self.shakeSeq then
        self.shakeSeq:Restart()
        return
    end
    self.shakeSeq = self.baseObject:createBindTweenSequence()
	self.shakeSeq:AppendInterval(2)
    local transform = self.baseObject:getTransform()
	self.shakeSeq:Append(transform:DOScale(1.12, 0.1))
	self.shakeSeq:Append(transform:DOScale(0.9, 0.11))
	self.shakeSeq:Append(transform:DOScale(1.1, 0.15))
	self.shakeSeq:Append(transform:DOScale(0.9, 0.2))
	self.shakeSeq:Append(transform:DOScale(1.05, 0.25))
	self.shakeSeq:Append(transform:DOScale(0.9, 0.3))
	self.shakeSeq:Append(transform:DOScale(1, 0.3))
    self.shakeSeq:AppendInterval(0.3)
	self.shakeSeq:SetLoops(-1)
end

function SideBarBaseCell:stopShake()
    if self.shakeSeq then
        self.shakeSeq:Pause()
    end
end

function SideBarBaseCell:refreshRedPoint()
    if self:getIsShowRedPoint() then
        self:showRedPoint()
    else
        self:hideRedPoint()
    end
end

function SideBarBaseCell:showRedPoint()
    if self.redPoint then
        return
    end
    self.redPoint = true
    local x, y = self:getRedPointPosition()
    self.baseObject:addRedPoint(x, y, self:getRedPointScale())
end

function SideBarBaseCell:hideRedPoint()
    if self.redPoint == false then
        return
    end
    self.redPoint = false
    self.baseObject:removeRedPoint()
end

function SideBarBaseCell:checkIsOpen()
    -- 审核模式下大多数的带内购的侧边栏入口都不显示
    if self:getHasPurchase() and GFunc.isShenhe() then
        return false
    end
    return self:getIsOpen()
end

function SideBarBaseCell:getEntrancePos()
    if self:getIconRes() then
        return self.imgIcon:getPosition()
    elseif self:getSpineName() then
        return self.spineIcon:getPosition()
    end
    return nil
end

function SideBarBaseCell:getCellPath()
    return self.cellPath
end

-- 额外设置的部分 ***********************************************************

function SideBarBaseCell:setAnchoredPositionY(y)
    self.baseObject:setAnchoredPositionY(y)
end

function SideBarBaseCell:setAnchoredPositionX(x)
    self.baseObject:setAnchoredPositionX(x)
end

function SideBarBaseCell:getIsVisible()
    return self.isVisible
end

function SideBarBaseCell:setVisible(visible)
    self.isVisible = visible
    self.baseObject:setActive(visible)
    if not visible then
        self:stopShake()
    end
    if self.isInitSpine and self:getSpineName() and not self.spineIcon:getIsLoadingAsset() then
        self.spineIcon:getSkeletonGraphic().enabled = visible
    end
end

function SideBarBaseCell:getIsActive()
    return self.isActive
end

function SideBarBaseCell:setActive(isActive)
    if self.isActive == isActive then
        return
    end
    self.isActive = isActive
    if self.isInitSpine and self:getSpineName() and not self.spineIcon:getIsLoadingAsset() then
        self.spineIcon:setActive(isActive)
    end
end

function SideBarBaseCell:initWithParentUI(parentUI, cellPath, isLeftSide)
    self.parentUI = parentUI
    self.cellPath = cellPath
    self.isLeftSide = isLeftSide
end

function SideBarBaseCell:closeBtn()
    if self.parentUI then
        self.parentUI:closeSideBarBtn(self.isLeftSide)
    end
end

function SideBarBaseCell:onDestroy()
    if self.shakeSeq then
        self.shakeSeq:Kill()
        self.shakeSeq = nil
    end
end

return SideBarBaseCell