local SideBarComp = class("SideBarComp", LuaComponent)
local UIPrefabObject = require "app/bf/unity/uiprefab_object"

-- 侧边栏整体边距
local SIDE_BAR_NODE_INTERVAL_X = 0

-- 侧边栏每项间距
local SIDE_BAR_INTERVAL_X = 30
local SIDE_BAR_INTERVAL_Y = 70

function SideBarComp:onReshow()
	self:checkSideBarOpenStatus()
end

function SideBarComp:updateTime()
	self:updateSideBarStatus()
end

function SideBarComp:init()
    local uiMap = self:getUIMap()
    self.leftNode = uiMap["side_bar_comp.left_node"]
    self.leftSideBar = uiMap["side_bar_comp.left_node.side_bar"]
    self.leftArrow = uiMap["side_bar_comp.left_node.arrow"]
    self.leftArrowRp = uiMap["side_bar_comp.left_node.arrow.rp"]
    self.rightNode = uiMap["side_bar_comp.right_node"]
    self.rightSideBar = uiMap["side_bar_comp.right_node.side_bar"]
    self.rightArrow = uiMap["side_bar_comp.right_node.arrow"]
    self.rightArrowRp = uiMap["side_bar_comp.right_node.arrow.rp"]
    self.sideBarCacheRoot = uiMap["side_bar_comp.cache"]
    self.sideBarCellObject = uiMap["side_bar_comp.cache.side_bar_base_cell"]

    self.sideBarWidth, self.sideBarHeight = self.sideBarCellObject:fastGetSizeDelta()
	self.sideBarHeight = self.sideBarHeight * 0.8
    if GConst.UI_SCREEN_HEIGHT / GConst.UI_SCREEN_WIDTH >= 2 then
		self.sideBarMaxCount = 5
	else
		self.sideBarMaxCount = 4
	end

	if self.showLeftSideBar == nil then
		self.showLeftSideBar = true
	end
	if self.showRightSideBar == nil then
		self.showRightSideBar = true
	end
	if self.leftBarList == nil then
        self.leftBarList = {}
    end
    if self.rightBarList == nil then
        self.rightBarList = {}
    end
    if self.sideBarCellCacheMap == nil then
        self.sideBarCellCacheMap = {}
    end
    if self.sideBarCellAllMap == nil then
        self.sideBarCellAllMap = {}
    end

	self.leftArrow:addClickListener(function()
		self.showLeftSideBar = not self.showLeftSideBar
		self:refreshLeftSideBars()
	end)
	self.rightArrow:addClickListener(function()
		self.showRightSideBar = not self.showRightSideBar
		self:refreshRightSideBars()
	end)

    -- self:bind(DataManager.PaymentData, "isDirty", function()
    --     self:onReshow() 
    --     self:updateTime() 
    -- end)
    -- self:bind(DataManager.SignWeekData, "isDirty", function()
    --     self:updateTime()
    -- end)    
    -- self:bind(DataManager.SignMonthData, "isDirty", function()
    --     self:updateTime()
    -- end)
end

function SideBarComp:refresh()
    self:refreshLeftSideBars()
	self:refreshRightSideBars()
    self:updateSideBarStatus()
end

function SideBarComp:addSideBarCellComp(cellClassPath, parent)
    local prefab = CS.UnityEngine.Object.Instantiate(self.sideBarCellObject:getGameObject())
    local prefabObject = UIPrefabObject:create()
    prefabObject:initWithPrefab(GConst.EMPTY_STRING, prefab)
    prefabObject:initPrefabHelper()
    prefabObject:genAllChildren()
    prefabObject:setName(cellClassPath)
    return prefabObject:addLuaComponent(cellClassPath)
end

function SideBarComp:clearSideBarList(sideBarList)
    local count = #sideBarList
    for i = 1, count do
        local cell = table.remove(sideBarList)
        cell:setActive(false)
        cell:setVisible(false)
        self.sideBarCellCacheMap[cell:getCellPath()] = cell
    end
end

function SideBarComp:refreshLeftSideBars()
    self:clearSideBarList(self.leftBarList)
	self.leftArrow:setActive(false)
	self.leftNode:setAnchoredPositionX(GConst.NOT_VISIBLE_POS)
    local list = GConst.MainCityConst.LEFT_SIDE_BARS
    if #list <= 0 then
        return
    end

	local showCellCount = 0
	local showArrowRedPoint = false
    for index, path in ipairs(list) do
        local cellClass = self.sideBarCellAllMap[path]
        if cellClass == nil then
            cellClass = require(path)
            self.sideBarCellAllMap[path] = cellClass
        end

        if cellClass:checkIsOpen() then
			showCellCount = showCellCount + 1
            local cell = self.sideBarCellCacheMap[path]
            if cell == nil then
                cell = self:addSideBarCellComp(path)
                cell:getBaseObject():setParent(self.leftSideBar, false)
                cell:getBaseObject():setAnchorMin(0, 1)
                cell:getBaseObject():setAnchorMax(0, 1)
            end
            cell:initWithParentUI(self, path, true)
            table.insert(self.leftBarList, cell)

			-- 侧边栏红点
			if showCellCount > GConst.MainCityConst.SIDE_BAR_MIN_COUNT and cellClass:getIsShowRedPoint() then
				showArrowRedPoint = true
			end
        end
    end
	local listCount = #self.leftBarList
    if listCount <= 0 then
        return
    end

    -- 刷新位置
    self.leftNode:setAnchoredPositionX(SIDE_BAR_NODE_INTERVAL_X)
    local initX = self.sideBarWidth/2
    local addX = SIDE_BAR_INTERVAL_X + self.sideBarWidth
    local initY = self.sideBarHeight/2
    local addY = SIDE_BAR_INTERVAL_Y + self.sideBarHeight

	local x = 110
    for index, cell in ipairs(self.leftBarList) do
		if self.showLeftSideBar or index <= GConst.MainCityConst.SIDE_BAR_MIN_COUNT then
			local addMulX = math.floor((index - 1) / self.sideBarMaxCount)
			local addMulY = (index - 1) % self.sideBarMaxCount
			cell:setAnchoredPositionX(initX + addMulX * addX)
			cell:setAnchoredPositionY(-(initY + addMulY * addY))
			cell:refresh()
			cell:setActive(true)
			cell:setVisible(true)

			if addMulX >= 1 and x <= 100 then
				x = (addMulX + 1) * self.sideBarWidth + SIDE_BAR_INTERVAL_X + 14
			end
		else
			cell:setActive(false)
			cell:setVisible(false)
		end
    end

	local y = 120
	if listCount > GConst.MainCityConst.SIDE_BAR_MIN_COUNT then
		self.leftArrow:setActive(true)
		self.leftArrowRp:setVisible(false)
		if self.showLeftSideBar then
			local addMulY = ((listCount - 1) % self.sideBarMaxCount) + 1
			if listCount > self.sideBarMaxCount then
				addMulY = self.sideBarMaxCount
			end
			self.leftArrow:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_GRAPHIC_FLIP).horizontal = true
			self.leftArrow:setAnchoredPositionY(-(initY + addMulY * addY) - 5)

			y = initY + addMulY * addY - 10 + 27 + 14
		else
			self.leftArrow:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_GRAPHIC_FLIP).horizontal = false
			self.leftArrow:setAnchoredPositionY(-(initY + GConst.MainCityConst.SIDE_BAR_MIN_COUNT * addY) + 10)
			self.leftArrowRp:setVisible(showArrowRedPoint)

			y = initY + GConst.MainCityConst.SIDE_BAR_MIN_COUNT * addY -10 + 27 + 14
		end
	end

	self.leftNode:setSizeDelta(x, y)
end

function SideBarComp:refreshRightSideBars()
    self:clearSideBarList(self.rightBarList)
	self.rightArrow:setActive(false)
	self.rightNode:setAnchoredPositionX(GConst.NOT_VISIBLE_POS)
    local list = GConst.MainCityConst.RIGHT_SIDE_BARS
    if #list <= 0 then
        return
    end

	local showCellCount = 0
	local showArrowRedPoint = false
    for index, path in ipairs(list) do
        local cellClass = self.sideBarCellAllMap[path]
        if cellClass == nil then
            cellClass = require(path)
            self.sideBarCellAllMap[path] = cellClass
        end

        if cellClass:checkIsOpen() then
			showCellCount = showCellCount + 1
            local cell = self.sideBarCellCacheMap[path]
            if cell == nil then
                cell = self:addSideBarCellComp(path)
                cell:getBaseObject():setParent(self.rightSideBar, false)
                cell:getBaseObject():setAnchorMin(1, 1)
                cell:getBaseObject():setAnchorMax(1, 1)
            end
            cell:initWithParentUI(self, path, false)
            table.insert(self.rightBarList, cell)

			-- 侧边栏红点
			if showCellCount > GConst.MainCityConst.SIDE_BAR_MIN_COUNT and cellClass:getIsShowRedPoint() then
				showArrowRedPoint = true
			end
        end
    end
	local listCount = #self.rightBarList
    if listCount <= 0 then
        return
    end

    -- 刷新位置
    self.rightNode:setAnchoredPositionX(-SIDE_BAR_NODE_INTERVAL_X)
    local initX = self.sideBarWidth/2
    local addX = SIDE_BAR_INTERVAL_X + self.sideBarWidth
    local initY = self.sideBarHeight/2
    local addY = SIDE_BAR_INTERVAL_Y + self.sideBarHeight
	local x = 110
    for index, cell in ipairs(self.rightBarList) do
		if self.showRightSideBar or index <= GConst.MainCityConst.SIDE_BAR_MIN_COUNT then
			local addMulX = math.floor((index - 1) / self.sideBarMaxCount)
			local addMulY = (index - 1) % self.sideBarMaxCount
			cell:setAnchoredPositionX(-(initX + addMulX * addX))
			cell:setAnchoredPositionY(-(initY + addMulY * addY))
			cell:refresh()
			cell:setActive(true)
			cell:setVisible(true)

			if addMulX >= 1 and x <= 100 then
				x = (addMulX + 1) * self.sideBarWidth + SIDE_BAR_INTERVAL_X + 14
			end
		else
			cell:setActive(false)
			cell:setVisible(false)
		end
    end

	local y = 120
	if listCount > GConst.MainCityConst.SIDE_BAR_MIN_COUNT then
		self.rightArrow:setActive(true)
		self.rightArrowRp:setVisible(false)
		if self.showRightSideBar then
			local addMulY = ((listCount - 1) % self.sideBarMaxCount) + 1
			if listCount > self.sideBarMaxCount then
				addMulY = self.sideBarMaxCount
			end
			self.rightArrow:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_GRAPHIC_FLIP).horizontal = true
			self.rightArrow:setAnchoredPositionY(-(initY + addMulY * addY) - 5)

			y = initY + addMulY * addY - 10 + 27 + 14
		else
			self.rightArrow:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_GRAPHIC_FLIP).horizontal = false
			self.rightArrow:setAnchoredPositionY(-(initY + GConst.MainCityConst.SIDE_BAR_MIN_COUNT * addY) + 10)
			self.rightArrowRp:setVisible(showArrowRedPoint)

			y = initY + GConst.MainCityConst.SIDE_BAR_MIN_COUNT * addY -10 + 27 + 14
		end
	end

	self.rightNode:setSizeDelta(x, y)
end

function SideBarComp:updateSideBarStatus()
    if self.leftBarList then
		local refreshCount = self.showLeftSideBar and #self.leftBarList or GConst.MainCityConst.SIDE_BAR_MIN_COUNT
        for i = 1, refreshCount do
            local sideBarBtn = self.leftBarList[i]
            if sideBarBtn then
                if sideBarBtn:getIsShowRedPoint() then
                    sideBarBtn:showRedPoint()
                else
                    sideBarBtn:hideRedPoint()
                end
				sideBarBtn:updateTime()
            end
        end
    end
    if self.rightBarList then
		local refreshCount = self.showRightSideBar and #self.rightBarList or GConst.MainCityConst.SIDE_BAR_MIN_COUNT
        for i = 1, refreshCount do
            local sideBarBtn = self.rightBarList[i]
            if sideBarBtn then
                if sideBarBtn:getIsShowRedPoint() then
                    sideBarBtn:showRedPoint()
                else
                    sideBarBtn:hideRedPoint()
                end
				sideBarBtn:updateTime()
            end
        end
    end
end

-- 检查侧边栏功能开启
function SideBarComp:checkSideBarOpenStatus()
    for k, CellClass in pairs(self.sideBarCellAllMap) do
        local cell = self.sideBarCellCacheMap[k]
        if cell and cell:getIsActive() then
            if not CellClass:checkIsOpen() then
                self:refreshLeftSideBars()
				self:refreshRightSideBars()
            end
        else
            if CellClass:checkIsOpen() then
                self:refreshLeftSideBars()
				self:refreshRightSideBars()
            end
        end
    end
end

-- 获取侧边栏活动入口坐标
function SideBarComp:getSideBarActIconPos(moduleKey)
    for name, cell in pairs(self.leftBarList) do
        if moduleKey == cell:getModuleKey() then
            return cell:getBaseObject():getPosition()
        end
    end
    for name, cell in pairs(self.rightBarList) do
        if moduleKey == cell:getModuleKey() then
            return cell:getBaseObject():getPosition()
        end
    end
    return nil
end

function SideBarComp:closeSideBarBtn(isLeft)
    if isLeft then
		self:refreshLeftSideBars()
	else
		self:refreshRightSideBars()
	end
end

return SideBarComp