local DevToolUI = class("DevToolUI", BaseUI)

local TOOL_INFO_LIST = {
	{
		title = "GM工具",
		func = function(self)
			self:closeUI()
			UIManager:showUI(UIManager.UI_PATH.GM_TOO_UI)
		end
	},
	{
		title = "FPS",
		func = function(self)
			local comp = UIManager.uiRoot:getComponent(typeof(CS.BF.ShowFPS))
			if comp == nil then
				comp = UIManager.uiRoot:addComponent(typeof(CS.BF.ShowFPS))
			else
				comp.enabled = not comp.enabled
			end
		end
	},
	{
		getTitle = function()
			return math.tointeger(CS.UnityEngine.Time.timeScale) .. "倍速"
		end,
		func = function(self)
			local timeScale = CS.UnityEngine.Time.timeScale
			if timeScale == 1 then
				timeScale = 2
			elseif timeScale == 2 then
				timeScale = 5
			elseif timeScale == 5 then
				timeScale = 10
			else
				timeScale = 1
			end
			CS.UnityEngine.Time.timeScale = timeScale
			self.scrollRect:refreshAll()
		end
	},
	{
		title = "调整灯光",
		func = function(self)
			self:closeUI()
			ModuleManager.DevToolManager:showLightSetting()
		end
	},
}

function DevToolUI:ctor()

end

function DevToolUI:isFullScreen()
	return false
end

function DevToolUI:getPrefabPath()
	return "assets/prefabs/ui/gm/dev_tool.prefab"
end

function DevToolUI:onLoadRootComplete()
	self._baseRootCanvas.sortingOrder = 30000
	self.uiMap = self.root:genAllChildren()
	self.uiMap["dev_tool.floating_btn"]:addClickListener(function()
		local showFloatingIcon = LocalData:getGMShowFloatingIcon()
		if showFloatingIcon then
			LocalData:setGMShowFloatingIcon(0)
		else
			LocalData:setGMShowFloatingIcon(1)
		end
		ModuleManager.DevToolManager:showOrHideFloatingIcon()
		self:refreshFloatingBtn()
	end)

	self:scheduleGlobal(function()
		self:updateTime()
	end, 0)
end

function DevToolUI:onRefresh()
	self:initScrollRect()
	self:initGameSetting()
	self:refreshFloatingBtn()
	self:updateTime()
end

function DevToolUI:initScrollRect()
	local scrollRect = self.uiMap['dev_tool.scroll_rect']
	self.scrollRect = scrollRect:getLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
	self.scrollRect:clearCells()
	self.scrollRect:setFadeArgs(0, 0.3)
	self.scrollRect:addInitCallback(function()
		return "app/ui/gm/cell/dev_tool_cell"
	end)
	self.scrollRect:addRefreshCallback(function(index, cell)
		self:initScrollRectCell(index, cell)
	end)
	self.scrollRect:refillCells(#TOOL_INFO_LIST)

	self.uiMap['dev_tool.close_btn']:addClickListener(function() self:closeUI() end)
end

function DevToolUI:initScrollRectCell(index, cell)
	local data = TOOL_INFO_LIST[index]
	cell:addClickListener(function ()
		if data.func then
			data.func(self)
		end
	end)
	cell:refresh(data)
end

function DevToolUI:initGameSetting()
	self.uiMap["dev_tool.toggle_skip_tutorial.check_tx"]:setText("跳过引导", self.uiMap["dev_tool.toggle_skip_tutorial.check_tx"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT))
	self.skipTutorial = LocalData:getSkipTutorial()
	if self.skipTutorial then
		self.uiMap["dev_tool.toggle_skip_tutorial.check_img"]:setLocalScale(1, 1, 1)
	else
		self.uiMap["dev_tool.toggle_skip_tutorial.check_img"]:setLocalScale(0, 0, 0)
	end
	self.uiMap["dev_tool.toggle_skip_tutorial"]:addClickListener(function()
		self.skipTutorial = not self.skipTutorial
		local scale = self.skipTutorial and 1 or 0
		self.uiMap["dev_tool.toggle_skip_tutorial.check_img"]:setLocalScale(scale, scale, scale)
		LocalData:setSkipTutorial(scale)
	end)

	self.uiMap["dev_tool.toggle_skip_stage.check_tx"]:setText("跳过关卡战斗", self.uiMap["dev_tool.toggle_skip_stage.check_tx"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT))
	self.skipStageBattle = LocalData:getSkipStageBattle()
	if self.skipStageBattle then
		self.uiMap["dev_tool.toggle_skip_stage.check_img"]:setLocalScale(1, 1, 1)
	else
		self.uiMap["dev_tool.toggle_skip_stage.check_img"]:setLocalScale(0, 0, 0)
	end
	self.uiMap["dev_tool.toggle_skip_stage"]:addClickListener(function()
		self.skipStageBattle = not self.skipStageBattle
		local scale = self.skipStageBattle and 1 or 0
		self.uiMap["dev_tool.toggle_skip_stage.check_img"]:setLocalScale(scale, scale, scale)
		LocalData:setSkipStageBattle(scale)
	end)
end

function DevToolUI:updateTime()
	local time = Time:getServerTime()
	if self.serverTime == time then
		return
	end
	self.serverTime = time
	local now = os.date('!*t', self.serverTime)
	local str = string.format("%s-%s-%s %02d:%02d:%02d", now.year, now.month, now.day, now.hour, now.min, now.sec)
	self.uiMap["dev_tool.time"]:setText("当前服务器时间：" .. str, self.uiMap["dev_tool.time"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT))
end

function DevToolUI:refreshFloatingBtn()
	local showFloatingIcon = LocalData:getGMShowFloatingIcon()
	if showFloatingIcon then
		self.uiMap["dev_tool.floating_btn.text"]:setText("隐藏悬浮窗", self.uiMap["dev_tool.floating_btn.text"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT))
	else
		self.uiMap["dev_tool.floating_btn.text"]:setText("显示悬浮窗", self.uiMap["dev_tool.floating_btn.text"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT))
	end
end

return DevToolUI