local MessageBox = {}

-- params参数:
-- content:正文
-- okFunc:ok按钮的回调
-- okText:ok按钮的文字
-- cancelText:取消按钮的文字
-- cancelFunc:取消按钮的回调
-- boxType:messagebox的类型
-- top:是否处于所有ui的最高层级,谨慎使用,目前只允许给掉线必须回登陆界面的情况下使用
-- blankAreaClose:是否点击空白区域关闭,默认点击不关闭
-- titleTx:标题文字
-- costId:确认按钮上的消耗道具id
-- costNum:确认按钮上的消耗道具数量
function MessageBox:showMessageBox(params)
	params = params or {}
	local content = params.content or ""
	local okFunc = params.okFunc
	local okText = params.okText
	local cancelText = params.cancelText
	local cancelFunc = params.cancelFunc
	local boxType = params.boxType
	local top = params.top
	local showToday = params.showToday
	local tag = params.tag
	local blankAreaClose = params.blankAreaClose
	local titleTx = params.titleTx or I18N:getGlobalText(I18N.GlobalConst.MESSAGE_BOX_TITLE)
	local costId = params.costId
	local costNum = params.costNum

	if showToday then -- 今天内不在提示
		local time = LocalData:getMessageBoxShowTodayTime(showToday)
		local todayTime = Time:getBeginningOfServerToday()
		if time > 0 and time == todayTime then
			if okFunc then
				return okFunc()
			end
		end
	end

	UIManager:getMessageBox(top, function(prefabObject)
		prefabObject._closeByAndroidBackspace = false
		prefabObject:setTag(tag)

		local selected = false
		local uiMap = prefabObject:genAllChildren()
		uiMap["message_box.mask"]:addClickListener(function()
			if blankAreaClose then
				prefabObject:getComponent(GConst.TYPEOF_UNITY_CLASS.CANVAS).enabled = false
			end
		end)

		uiMap["message_box.title_bg_img.btn.ok_btn.text"]:setSizeDeltaX(196)
		if costId then
			local itemInfo = ConfigManager:getConfig("item")[costId]
			if itemInfo then
				uiMap["message_box.title_bg_img.btn.ok_btn.icon"]:setVisible(true)
				uiMap["message_box.title_bg_img.btn.ok_btn.icon"]:setSprite(GConst.ATLAS_PATH.ICON_ITEM, itemInfo.icon)
				uiMap["message_box.title_bg_img.btn.ok_btn.text"]:setText(tostring(costNum))
				GFunc.centerImgAndTx(uiMap["message_box.title_bg_img.btn.ok_btn.icon"], uiMap["message_box.title_bg_img.btn.ok_btn.text"])
			else
				uiMap["message_box.title_bg_img.btn.ok_btn.icon"]:setVisible(false)
				uiMap["message_box.title_bg_img.btn.ok_btn.text"]:setAnchoredPositionX(0)
				uiMap["message_box.title_bg_img.btn.ok_btn.text"]:setText(okText or I18N:getGlobalText(I18N.GlobalConst.BTN_TEXT_OK))
			end
		else
			uiMap["message_box.title_bg_img.btn.ok_btn.icon"]:setVisible(false)
			uiMap["message_box.title_bg_img.btn.ok_btn.text"]:setAnchoredPositionX(0)
			uiMap["message_box.title_bg_img.btn.ok_btn.text"]:setText(okText or I18N:getGlobalText(I18N.GlobalConst.BTN_TEXT_OK))
		end

		uiMap["message_box.title_bg_img.title_text"]:setText(titleTx)
		uiMap["message_box.title_bg_img.bg.content_tx"]:setText(content)
		uiMap["message_box.title_bg_img.btn.cancel_btn.text"]:setText(cancelText or I18N:getGlobalText(I18N.GlobalConst.BTN_TEXT_CANCEL))

		if boxType == GConst.MESSAGE_BOX_TYPE.MB_OK then
			uiMap["message_box.title_bg_img.btn.cancel_btn"]:setVisible(false)
			uiMap["message_box.title_bg_img.btn.ok_btn"]:setVisible(true)
			uiMap["message_box.title_bg_img.btn.ok_btn"]:setAnchoredPositionX(0)
		elseif boxType == GConst.MESSAGE_BOX_TYPE.MB_OK_CANCEL then
			if cancelFunc == nil then
				prefabObject._closeByAndroidBackspace = true
			end
			uiMap["message_box.title_bg_img.btn.ok_btn"]:setVisible(true)
			uiMap["message_box.title_bg_img.btn.ok_btn"]:setAnchoredPositionX(-124)
			uiMap["message_box.title_bg_img.btn.cancel_btn"]:setVisible(true)
			uiMap["message_box.title_bg_img.btn.cancel_btn"]:addClickListener(function()
				self:closeAndClear(prefabObject, uiMap)
				if cancelFunc then
					cancelFunc()
				end
			end)
		end

		uiMap["message_box.title_bg_img.btn.ok_btn"]:addClickListener(function()
			if showToday and selected then
				local todayTime = Time:getBeginningOfServerToday()
				LocalData:setMessageBoxShowTodayTime(showToday, todayTime)
			end
			self:closeAndClear(prefabObject, uiMap)
			if okFunc then
				okFunc()
			end
		end)

		if showToday then -- 今天内不在提示
			uiMap["message_box.title_bg_img.btn"]:setAnchoredPositionY(-136)
			uiMap["message_box.title_bg_img.today"]:setVisible(true)
			uiMap["message_box.title_bg_img.today.check.select"]:setVisible(false)

			local checkUI = uiMap["message_box.title_bg_img.today.check"]
			checkUI:addClickListener(function()
				selected = not selected
				Logger.logHighlight("%s", selected)
				uiMap["message_box.title_bg_img.today.check.select"]:setVisible(selected)
			end)

			local textUI = uiMap["message_box.title_bg_img.today.text"]
			textUI:setText(I18N:getGlobalText(I18N.GlobalConst.CONFIRM_IGNORE))

			GFunc.centerImgAndTx(checkUI, textUI, 5)
		else
			uiMap["message_box.title_bg_img.btn"]:setAnchoredPositionY(-120)
			uiMap["message_box.title_bg_img.today"]:setVisible(false)
		end
	end)
end

function MessageBox:refreshText(isTop)
	local gameObject = UIManager:getMessageBoxGameObject(isTop)
	if gameObject == nil then
		return
	end
	if gameObject:getComponent(GConst.TYPEOF_UNITY_CLASS.CANVAS).enabled then
		return
	end
	local uiMap = gameObject:genAllChildren()
	uiMap["message_box.title_bg_img.title_text"]:setText("")
	uiMap["message_box.title_bg_img.bg.content_tx"]:setText("")
	uiMap["message_box.title_bg_img.btn.ok_btn.text"]:setText("")
	uiMap["message_box.title_bg_img.btn.cancel_btn.text"]:setText("")
end

function MessageBox:closeAndClear(prefabObject, uiMap)
	uiMap["message_box.title_bg_img.btn.ok_btn"]:removeClickListener()
	uiMap["message_box.title_bg_img.btn.cancel_btn"]:removeClickListener()
	uiMap["message_box.title_bg_img.today.check"]:removeClickListener()
	prefabObject:getComponent(GConst.TYPEOF_UNITY_CLASS.CANVAS).enabled = false
end

return MessageBox