local ChatBtnComp = class("ChatBtnComp", LuaComponent)

function ChatBtnComp:init()
	local x, y = self.baseObject:fastGetSizeDelta()
	self.width = x
	self.height = y
	self.baseObject:addDragListener(function(eventType, x, y)
        self:onGrag(eventType, x, y)
    end)
    self:adsorbent()

	local uiMap = self.baseObject:genAllChildren()
	self.redPoint = uiMap["chat_btn.red_point"]
	self.redPointTx = uiMap["chat_btn.red_point.text"]
	self.isBindData = false
	self.initPosition = false
end

function ChatBtnComp:bindData()
	self.isBindData = true
	self:bind(DataManager.ChatData:getChatGuild(), "chatCount", function()
		self:refreshRedPoint()
	end)

	self:bind(DataManager.ChatData:getChatGuild(), "readMsgIndex", function()
		self:refreshRedPoint()
	end)

	self:bind(DataManager.ChatData:getChatWorld(), "chatCount", function()
		self:refreshRedPoint()
	end)

	self:bind(DataManager.ChatData:getChatWorld(), "readMsgIndex", function()
		self:refreshRedPoint()
	end)

	self:bind(DataManager.ChatData, "isBlockWorldRedPoint", function()
		self:refreshRedPoint()
	end)

	self:bind(DataManager.ChatData, "isBlockGuildRedPoint", function()
		self:refreshRedPoint()
	end)
end

function ChatBtnComp:show(isFirstEnter)
	if DataManager.TutorialData:getIsInTutorial() then -- 有新手引导的时候隐藏
		self.baseObject:getComponent(GConst.TYPEOF_UNITY_CLASS.CANVAS).enabled = false
		return
	end
	if isFirstEnter or not self.initPosition then
		self.initPosition = true
		local width = GConst.UI_SCREEN_WIDTH
		local height = GConst.UI_SCREEN_HEIGHT
		self.baseObject:setAnchoredPosition(-width/2 + 60, -height/2 + 220)
		self:bindData()
	end
	if not self.isBindData then
		self:bindData()
	end
	self:refreshRedPoint()
	self.baseObject:getComponent(GConst.TYPEOF_UNITY_CLASS.CANVAS).enabled = true
end

function ChatBtnComp:hide()
	if not self.baseObject:getComponent(GConst.TYPEOF_UNITY_CLASS.CANVAS).enabled then
		return
	end
	self.baseObject:getComponent(GConst.TYPEOF_UNITY_CLASS.CANVAS).enabled = false
	self:unBindAll()
	self.isBindData = false
end

function ChatBtnComp:adsorbent()
	local notchScreenHeight = SafeAreaManager:getNotchScreenHeight()
	if notchScreenHeight < 1 then -- 没有刘海
		notchScreenHeight = 0
	end
	local posX, posY = self.baseObject:fastGetAnchoredPosition()
    local width = GConst.UI_SCREEN_WIDTH
	local height = GConst.UI_SCREEN_HEIGHT
    if posY < -height/2 + self.height/2 then
		posY = -height/2 + self.height/2
	elseif posY > height/2 - self.height/2 - notchScreenHeight then
		posY = height/2 - self.height/2 - notchScreenHeight
	end
    local distanceLeft = posX + width/2
    local distanceRight = width - distanceLeft
    local min = math.min(distanceLeft, distanceRight)
    local x, y = width/2 - self.width/2 - 20, posY-- 默认向右吸附
    if distanceLeft == min then    -- 离左边最近
        x, y = -width/2 + self.width/2 + 20, posY
    end
	self.baseObject:setAnchoredPosition(x, y)
end

function ChatBtnComp:onGrag(eventType, x, y)
	if eventType == GConst.TOUCH_EVENT.DOWN then
    elseif eventType == GConst.TOUCH_EVENT.DRAG then
		local uiCamera = UIManager:getUICameraComponent()
    	local worldPosition = uiCamera:ScreenToWorldPoint(BF.Vector2(x, y))
    	local convertPosition = UIManager:getMainCanvas():getTransform():InverseTransformPoint(worldPosition)
		local x = convertPosition.x
		local y = convertPosition.y
		if x > GConst.UI_SCREEN_WIDTH/2 then
			x = GConst.UI_SCREEN_WIDTH/2
		elseif x < -GConst.UI_SCREEN_WIDTH/2 then
			x = -GConst.UI_SCREEN_WIDTH/2
		end
		if y > GConst.UI_SCREEN_HEIGHT/2 then
			y = GConst.UI_SCREEN_HEIGHT/2
		elseif y < -GConst.UI_SCREEN_HEIGHT/2 then
			y = -GConst.UI_SCREEN_HEIGHT/2
		end
        self.baseObject:setAnchoredPosition(x, y)
    elseif eventType == GConst.TOUCH_EVENT.UP_INSIDE then
        ModuleManager.ChatManager:showChatUI()
	elseif eventType == GConst.TOUCH_EVENT.DRAG_CANCEL_UP then
		self:adsorbent()
    end
end

function ChatBtnComp:refreshRedPoint()
	local count = 0
	if not DataManager.ChatData:getIsBlockWorldRedPoint() then
		local wordChat = DataManager.ChatData:getChatWorld()
		local worldReadIndex = wordChat:getReadMsgIndex()
		local chatWorldCount = wordChat:getChatCount()
		if worldReadIndex < chatWorldCount then
			count = count + chatWorldCount - worldReadIndex
		end
	end
	if not DataManager.ChatData:getIsBlockGuildRedPoint() then
		if DataManager.GuildData:checkInGuild() then
			local chatGuild = DataManager.ChatData:getChatGuild()
			local guildReadIndex = chatGuild:getReadMsgIndex()
			local chatGuildCount = chatGuild:getChatCount()
			if guildReadIndex < chatGuildCount then
				count = count + chatGuildCount - guildReadIndex
			end
		end
	end
	if count > 99 then
		self.redPoint:setLocalScale(1, 1, 1)
		self.redPointTx:setText("99")
	elseif count > 0 then
		self.redPoint:setLocalScale(1, 1, 1)
		self.redPointTx:setText(tostring(count))
	else
		self.redPoint:setLocalScale(0, 0, 0)
	end
end

return ChatBtnComp