local LightSettingUI = class("LightSettingUi", BaseUI)

function LightSettingUI:ctor()
end

function LightSettingUI:getPrefabPath()
	return "assets/prefabs/ui/gm/gm_light_setting.prefab"
end

function LightSettingUI:isFullScreen()
	return false
end

function LightSettingUI:onClose()
	self.dropList.onValueChanged:RemoveAllListeners()
	self.colorInputField.onEndEdit:RemoveAllListeners()
	self.intensityInputField.onEndEdit:RemoveAllListeners()
	self.shadowStrengthSlider.onValueChanged:RemoveAllListeners()
	self.shadowStrengthInputField.onEndEdit:RemoveAllListeners()
	self.shadowResolutionDropList.onValueChanged:RemoveAllListeners()
	self.shadowBiasSlider.onValueChanged:RemoveAllListeners()
	self.shadowBiasInputField.onEndEdit:RemoveAllListeners()
	self.shadowNormalBiasSlider.onValueChanged:RemoveAllListeners()
	self.shadowNormalBiasInputField.onEndEdit:RemoveAllListeners()
	self.shadowNearPlaneSlider.onValueChanged:RemoveAllListeners()
	self.shadowNearPlaneInputField.onEndEdit:RemoveAllListeners()
	self.shadowProjectionDropList.onValueChanged:RemoveAllListeners()
	self.shadowDistanceInputField.onEndEdit:RemoveAllListeners()
	self.shadowNearPlaneOffsetInputField.onEndEdit:RemoveAllListeners()
	self.shadowCascadesDropList.onValueChanged:RemoveAllListeners()
	self.shadowmaskModeDropList.onValueChanged:RemoveAllListeners()
	self.shadowsDropList.onValueChanged:RemoveAllListeners()
end

function LightSettingUI:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()
	uiMap["gm_light_setting.close_btn"]:addClickListener(function()
		self:closeUI()
	end)

	self.dropList = uiMap["gm_light_setting.light_list"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_DROP_DOWN)
	self.dropList.onValueChanged:AddListener(function(index)
		if self.lights == nil then
			return
		end
		self.currLight = self.lights[index]
		self:refreshLight()
	end)
	self.dropList:ClearOptions()

	self.mask = uiMap["gm_light_setting.mask"]

	-- 颜色
	self.colorInputField = uiMap["gm_light_setting.input_field_1"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_INPUT_FIELD)
	self.colorImage = uiMap["gm_light_setting.image_1"]
	self.colorAlphaSlider = uiMap["gm_light_setting.image_1.alpha_slider"]
	self.colorInputField.onEndEdit:AddListener(function (value)
		if self.currLight == nil then
			return
		end
		if string.byte(value) ~= 35 then
			value = "#" .. value
		end
		local color = CS.BF.Utils.ParseHtmlString(value)
		if color == nil then
			return
		end
		self.currLight.color = color
		self:refreshLight()
	end)

	-- 强度
	self.intensityInputField = uiMap["gm_light_setting.input_field_2"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_INPUT_FIELD)
	self.intensityInputField.onEndEdit:AddListener(function (value)
		if self.currLight == nil then
			return
		end
		local intensity = tonumber(value)
		if intensity == nil then
			return
		end
		self.currLight.intensity = intensity
		self:refreshLight()
	end)

	-- 阴影
	self.shadowStrengthSlider = uiMap["gm_light_setting.slider_5"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_SLIDER)
	self.shadowStrengthSlider.onValueChanged:AddListener(function (value)
		if self.currLight == nil then
			return
		end
        self.currLight.shadowStrength = value
		self:refreshLight()
    end)
	self.shadowStrengthInputField = uiMap["gm_light_setting.input_field_5"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_INPUT_FIELD)
	self.shadowStrengthInputField.onEndEdit:AddListener(function (value)
		if self.currLight == nil then
			return
		end
		local strength = tonumber(value)
		if strength == nil then
			return
		end
		self.currLight.shadowStrength = strength
		self:refreshLight()
	end)

	self.shadowResolutionDropList = uiMap["gm_light_setting.resolution_list_6"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_DROP_DOWN)
	self.shadowResolutionDropList:ClearOptions()
	local optionList = {}
	table.insert(optionList, CS.UnityEngine.UI.Dropdown.OptionData("Use Quality Settings"))
	table.insert(optionList, CS.UnityEngine.UI.Dropdown.OptionData("Low Resolution"))
	table.insert(optionList, CS.UnityEngine.UI.Dropdown.OptionData("Medium Resolution"))
	table.insert(optionList, CS.UnityEngine.UI.Dropdown.OptionData("High Resolution"))
	table.insert(optionList, CS.UnityEngine.UI.Dropdown.OptionData("Very High Resolution"))
	self.shadowResolutionDropList:AddOptions(optionList)
	self.shadowResolutionDropList.onValueChanged:AddListener(function(index)
		if self.currLight == nil then
			return
		end
		if index == 0 then
			self.currLight.shadowResolution = CS.UnityEngine.Rendering.LightShadowResolution.FromQualitySettings
		elseif index == 1 then
			self.currLight.shadowResolution = CS.UnityEngine.Rendering.LightShadowResolution.Low
		elseif index == 2 then
			self.currLight.shadowResolution = CS.UnityEngine.Rendering.LightShadowResolution.Medium
		elseif index == 3 then
			self.currLight.shadowResolution = CS.UnityEngine.Rendering.LightShadowResolution.High
		elseif index == 4 then
			self.currLight.shadowResolution = CS.UnityEngine.Rendering.LightShadowResolution.VeryHigh
		else
			self.currLight.shadowResolution = CS.UnityEngine.Rendering.LightShadowResolution.FromQualitySettings
		end
		self:refreshLight()
	end)
	self.shadowResolutionDropList:SetValueWithoutNotify(0)

	self.shadowBiasSlider = uiMap["gm_light_setting.slider_7"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_SLIDER)
	self.shadowBiasSlider.onValueChanged:AddListener(function (value)
		if self.currLight == nil then
			return
		end
        self.currLight.shadowBias = value
		self:refreshLight()
    end)
	self.shadowBiasInputField = uiMap["gm_light_setting.input_field_7"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_INPUT_FIELD)
	self.shadowBiasInputField.onEndEdit:AddListener(function (value)
		if self.currLight == nil then
			return
		end
		local shadowBias = tonumber(value)
		if shadowBias == nil then
			return
		end
		self.currLight.shadowBias = shadowBias
		self:refreshLight()
	end)

	self.shadowNormalBiasSlider = uiMap["gm_light_setting.slider_8"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_SLIDER)
	self.shadowNormalBiasSlider.onValueChanged:AddListener(function (value)
		if self.currLight == nil then
			return
		end
        self.currLight.shadowNormalBias = value
		self:refreshLight()
    end)
	self.shadowNormalBiasInputField = uiMap["gm_light_setting.input_field_8"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_INPUT_FIELD)
	self.shadowNormalBiasInputField.onEndEdit:AddListener(function (value)
		if self.currLight == nil then
			return
		end
		local shadowNormalBias = tonumber(value)
		if shadowNormalBias == nil then
			return
		end
		self.currLight.shadowNormalBias = shadowNormalBias
		self:refreshLight()
	end)

	self.shadowNearPlaneSlider = uiMap["gm_light_setting.slider_9"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_SLIDER)
	self.shadowNearPlaneSlider.onValueChanged:AddListener(function (value)
		if self.currLight == nil then
			return
		end
        self.currLight.shadowNearPlane = value
		self:refreshLight()
    end)
	self.shadowNearPlaneInputField = uiMap["gm_light_setting.input_field_9"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_INPUT_FIELD)
	self.shadowNearPlaneInputField.onEndEdit:AddListener(function (value)
		if self.currLight == nil then
			return
		end
		local shadowNearPlane = tonumber(value)
		if shadowNearPlane == nil then
			return
		end
		self.currLight.shadowNearPlane = shadowNearPlane
		self:refreshLight()
	end)

	self.shadowProjectionDropList = uiMap["gm_light_setting.projection_list_11"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_DROP_DOWN)
	self.shadowProjectionDropList:ClearOptions()
	local optionList = {}
	table.insert(optionList, CS.UnityEngine.UI.Dropdown.OptionData("Close Fit"))
	table.insert(optionList, CS.UnityEngine.UI.Dropdown.OptionData("Stable Fit"))
	self.shadowProjectionDropList:AddOptions(optionList)
	self.shadowProjectionDropList.onValueChanged:AddListener(function(index)
		if self.currLight == nil then
			return
		end
		if index == 0 then
			CS.UnityEngine.QualitySettings.shadowProjection = CS.UnityEngine.ShadowProjection.CloseFit
		else
			CS.UnityEngine.QualitySettings.shadowProjection = CS.UnityEngine.ShadowProjection.StableFit
		end
		self:refreshLight()
	end)
	self.shadowProjectionDropList:SetValueWithoutNotify(0)

	self.shadowDistanceInputField = uiMap["gm_light_setting.input_field_12"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_INPUT_FIELD)
	self.shadowDistanceInputField.onEndEdit:AddListener(function (value)
		if self.currLight == nil then
			return
		end
		local shadowDistance = tonumber(value)
		if shadowDistance == nil then
			return
		end
		CS.UnityEngine.QualitySettings.shadowDistance = shadowDistance
		self:refreshLight()
	end)

	self.shadowNearPlaneOffsetInputField = uiMap["gm_light_setting.input_field_13"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_INPUT_FIELD)
	self.shadowNearPlaneOffsetInputField.onEndEdit:AddListener(function (value)
		if self.currLight == nil then
			return
		end
		local shadowNearPlaneOffset = tonumber(value)
		if shadowNearPlaneOffset == nil then
			return
		end
		CS.UnityEngine.QualitySettings.shadowNearPlaneOffset = shadowNearPlaneOffset
		self:refreshLight()
	end)

	self.shadowCascadesDropList = uiMap["gm_light_setting.cascades_list_14"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_DROP_DOWN)
	self.shadowCascadesDropList:ClearOptions()
	local optionList = {}
	table.insert(optionList, CS.UnityEngine.UI.Dropdown.OptionData("No Cascades"))
	table.insert(optionList, CS.UnityEngine.UI.Dropdown.OptionData("Two Cascades"))
	table.insert(optionList, CS.UnityEngine.UI.Dropdown.OptionData("Four Cascades"))
	self.shadowCascadesDropList:AddOptions(optionList)
	self.shadowCascadesDropList.onValueChanged:AddListener(function(index)
		if self.currLight == nil then
			return
		end
		if index == 0 then
			CS.UnityEngine.QualitySettings.shadowCascades = 1
		elseif index == 1 then
			CS.UnityEngine.QualitySettings.shadowCascades = 2
		elseif index == 2 then
			CS.UnityEngine.QualitySettings.shadowCascades = 4
		else
			CS.UnityEngine.QualitySettings.shadowCascades = 1
		end
		self:refreshLight()
	end)
	self.shadowCascadesDropList:SetValueWithoutNotify(0)

	self.shadowmaskModeDropList = uiMap["gm_light_setting.mode_list_15"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_DROP_DOWN)
	self.shadowmaskModeDropList:ClearOptions()
	local optionList = {}
	table.insert(optionList, CS.UnityEngine.UI.Dropdown.OptionData("Shadowmask"))
	table.insert(optionList, CS.UnityEngine.UI.Dropdown.OptionData("Distance Shadowmask"))
	self.shadowmaskModeDropList:AddOptions(optionList)
	self.shadowmaskModeDropList.onValueChanged:AddListener(function(index)
		if self.currLight == nil then
			return
		end
		if index == 1 then
			CS.UnityEngine.QualitySettings.shadowmaskMode = CS.UnityEngine.ShadowmaskMode.DistanceShadowmask
		else
			CS.UnityEngine.QualitySettings.shadowmaskMode = CS.UnityEngine.ShadowmaskMode.Shadowmask
		end
		self:refreshLight()
	end)
	self.shadowmaskModeDropList:SetValueWithoutNotify(0)

	self.shadowsDropList = uiMap["gm_light_setting.shadows_list_16"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_DROP_DOWN)
	self.shadowsDropList:ClearOptions()
	local optionList = {}
	table.insert(optionList, CS.UnityEngine.UI.Dropdown.OptionData("Disable Shadows"))
	table.insert(optionList, CS.UnityEngine.UI.Dropdown.OptionData("Hard Shadows Only"))
	table.insert(optionList, CS.UnityEngine.UI.Dropdown.OptionData("Hard And Soft Shadows"))
	self.shadowsDropList:AddOptions(optionList)
	self.shadowsDropList.onValueChanged:AddListener(function(index)
		if self.currLight == nil then
			return
		end
		if index == 1 then
			CS.UnityEngine.QualitySettings.shadows = CS.UnityEngine.ShadowQuality.HardOnly
		elseif index == 2 then
			CS.UnityEngine.QualitySettings.shadows = CS.UnityEngine.ShadowQuality.All
		else
			CS.UnityEngine.QualitySettings.shadows = CS.UnityEngine.ShadowQuality.Disable
		end
		self:refreshLight()
	end)
	self.shadowsDropList:SetValueWithoutNotify(0)

	self.lights = CS.UnityEngine.GameObject.FindObjectsOfType(GConst.TYPEOF_UNITY_CLASS.LIGHT)
	if self.lights == nil or self.lights.Length <= 0 then
		self.lights = nil
		self.currLight = nil
		self.mask:setLocalScale(1, 1, 1)
	else
		self.mask:setLocalScale(0, 0, 0)
	end
end

function LightSettingUI:onRefresh()
	self:refreshLightList()
end

function LightSettingUI:refreshLightList()
	self.dropList:ClearOptions()
	if self.lights == nil then
		return
	end
	if self.lights.Length <= 0 then
		return
	end
	local optionList = {}
	for i = 0, self.lights.Length - 1 do
		local data = CS.UnityEngine.UI.Dropdown.OptionData(self.lights[i].transform.name)
		table.insert(optionList, data)
	end
	self.dropList:AddOptions(optionList)
	self.dropList:SetValueWithoutNotify(0)
	self.currLight = self.lights[0]
	self:refreshLight()
end

function LightSettingUI:refreshLight()
	local lightColor = self.currLight.color
	self.colorImage:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_IMAGE).color = BF.Color(lightColor.r, lightColor.g, lightColor.b, 1)
	self.colorAlphaSlider:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER).value = 1 - lightColor.a
	self.colorInputField.text = CS.UnityEngine.ColorUtility.ToHtmlStringRGBA(lightColor)
	self.intensityInputField.text = tostring(self.currLight.intensity)

	self.shadowStrengthInputField.text = tostring(self.currLight.shadowStrength)
	self.shadowStrengthSlider.value = self.currLight.shadowStrength

	local shadowResolution = self.currLight.shadowResolution
	if shadowResolution == CS.UnityEngine.Rendering.LightShadowResolution.FromQualitySettings then
		self.shadowResolutionDropList:SetValueWithoutNotify(0)
	elseif shadowResolution == CS.UnityEngine.Rendering.LightShadowResolution.Low then
		self.shadowResolutionDropList:SetValueWithoutNotify(1)
	elseif shadowResolution == CS.UnityEngine.Rendering.LightShadowResolution.Medium then
		self.shadowResolutionDropList:SetValueWithoutNotify(2)
	elseif shadowResolution == CS.UnityEngine.Rendering.LightShadowResolution.High then
		self.shadowResolutionDropList:SetValueWithoutNotify(3)
	elseif shadowResolution == CS.UnityEngine.Rendering.LightShadowResolution.VeryHigh then
		self.shadowResolutionDropList:SetValueWithoutNotify(4)
	else
		self.shadowResolutionDropList:SetValueWithoutNotify(0)
	end

	self.shadowBiasSlider.value = self.currLight.shadowBias
	self.shadowBiasInputField.text = tostring(self.currLight.shadowBias)

	self.shadowNormalBiasSlider.value = self.currLight.shadowNormalBias
	self.shadowNormalBiasInputField.text = tostring(self.currLight.shadowNormalBias)

	self.shadowNearPlaneSlider.value = self.currLight.shadowNearPlane
	self.shadowNearPlaneInputField.text = tostring(self.currLight.shadowNearPlane)

	if CS.UnityEngine.QualitySettings.shadowProjection == CS.UnityEngine.ShadowProjection.StableFit then
		self.shadowProjectionDropList:SetValueWithoutNotify(1)
	else
		self.shadowProjectionDropList:SetValueWithoutNotify(0)
	end

	self.shadowDistanceInputField.text = tostring(CS.UnityEngine.QualitySettings.shadowDistance)

	self.shadowNearPlaneOffsetInputField.text = tostring(CS.UnityEngine.QualitySettings.shadowNearPlaneOffset)

	if CS.UnityEngine.QualitySettings.shadowCascades == 2 then
		self.shadowCascadesDropList:SetValueWithoutNotify(1)
	elseif CS.UnityEngine.QualitySettings.shadowCascades == 4 then
		self.shadowCascadesDropList:SetValueWithoutNotify(2)
	else
		self.shadowCascadesDropList:SetValueWithoutNotify(0)
	end

	if CS.UnityEngine.QualitySettings.shadowmaskMode == CS.UnityEngine.ShadowmaskMode.DistanceShadowmask then
		self.shadowmaskModeDropList:SetValueWithoutNotify(1)
	else
		self.shadowmaskModeDropList:SetValueWithoutNotify(0)
	end

	if CS.UnityEngine.QualitySettings.shadows == CS.UnityEngine.ShadowQuality.HardOnly then
		self.shadowsDropList:SetValueWithoutNotify(1)
	elseif CS.UnityEngine.QualitySettings.shadows == CS.UnityEngine.ShadowQuality.All then
		self.shadowsDropList:SetValueWithoutNotify(2)
	else
		self.shadowsDropList:SetValueWithoutNotify(0)
	end
end

return LightSettingUI
