dofile('Data/Scripts/Common/Screens/PlayingModeScreenBase.lua')

class 'PlayingModeScreen' (PlayingModeScreenBase)

local x_offset  = 9

function PlayingModeScreen:__init(name, tag)
    PlayingModeScreenBase.__init(self, name, tag)

   self:initModeData(tag)
end
-------------------------------------------------------------------------------

function PlayingModeScreen:initModeData(tag)
	-- self.tag		= tag or getDefaultGameStateTag()
	-- self.mode		= HasGameState(PLAY_MODE, nil, self.tag) and GetGameState(PLAY_MODE, self.tag):get(0)
	-- 														  or EPlayMode.CASUAL
	if self.mode == EPlayMode.CUSTOM then
		if HasGameState(PLAY_MODE_PARAMETERS, nil, self.tag) then
			self.modeParameters = Helpers.RestoreObjectFromGameState(PLAY_MODE_PARAMETERS, self.tag)
		else
			self.modeParameters = table.copy(EPlayModeDefaultParameters[EPlayMode.CASUAL])
		end
	else
		self.modeParameters = table.copy(EPlayModeDefaultParameters[self.mode])
	end

	self:setupModeButtons()
	self:initModeSliders() -- before self:initModeCheckboxes()
	self:initModeCheckboxes()
	
end
-------------------------------------------------------------------------------

function PlayingModeScreen:reInitModeData()
	self.modeParameters = table.copy(EPlayModeDefaultParameters[self.mode])
   	self:setupModeCheckboxes()
   	self:setupModeSliders()
end
-------------------------------------------------------------------------------

function PlayingModeScreen:initModeCheckboxes()
	self.ModeCheckboxes = {}
	self.ModeCheckboxes.tutorialEnabled			= self:createModeCheckbox(self:getEntityNamed('tutorial_checkbox'), 
													'tutorialEnabled')
	self.ModeCheckboxes.sparklesEnabled			= self:createModeCheckbox(self:getEntityNamed('sparkles_checkbox'), 
													'sparklesEnabled')
	self.ModeCheckboxes.sparklesHOEnabled		= self:createModeCheckbox(self:getEntityNamed('sparkles_ho_checkbox'),
													'sparklesHOEnabled')
	self.ModeCheckboxes.blackbarEnabled			= self:createModeCheckbox(self:getEntityNamed('blackbar_checkbox'),
													'blackbarEnabled')
	self.ModeCheckboxes.cursorPunishmentEnabled	= self:createModeCheckbox(self:getEntityNamed('cursor_checkbox'),
													'cursorPunishmentEnabled')
	self.ModeCheckboxes.hintEnabled				= self:createModeCheckbox(self:getEntityNamed('hint_checkbox'),
													'hintEnabled')
	self.ModeCheckboxes.skipEnabled				= self:createModeCheckbox(self:getEntityNamed('skip_checkbox'),
													'skipEnabled')

	self:setupModeCheckboxes()
end
-------------------------------------------------------------------------------

function PlayingModeScreen:setupModeCheckboxes()
	for parameter_name, checkbox in pairs(self.ModeCheckboxes) do
		checkbox:setChecked(self.modeParameters[parameter_name])
		if self.ModeSliders and self.ModeSliders[parameter_name] then
			self:setAvailableModeSlider(self.ModeSliders[parameter_name].entity, self.modeParameters[parameter_name])
		end
	end
end
-------------------------------------------------------------------------------

function PlayingModeScreen:createModeCheckbox(anchor, parameter_name)
	local spriteCheck = CreateSpriteEntity('Data/Textures/Screens/Options/CheckboxTick.png')

	local checkBox = createCheckBoxEntity(nil, spriteCheck)
	checkBox:attachTo(anchor)
	checkBox:setXY(-x_offset, 0)
	checkBox:setActiveRectSize(30, 26)
	checkBox.onCheck:add( bind(self.onModeCheckboxClick, self, parameter_name) )
	
	return checkBox
end
-------------------------------------------------------------------------------

function PlayingModeScreen:onModeCheckboxClick(parameter_name)
	if not (self.mode == EPlayMode.CUSTOM) then
		self:onModeButtonClick(EPlayMode.CUSTOM)
		getCommonSoundEntity('ButtonClick'):play()
	end
	local checkbox_value = self.ModeCheckboxes[parameter_name]:isChecked()
	self.modeParameters[parameter_name]	= checkbox_value
	if self.ModeSliders and self.ModeSliders[parameter_name] then
		self:setAvailableModeSlider(self.ModeSliders[parameter_name].entity, checkbox_value)
	end
end
-------------------------------------------------------------------------------

function PlayingModeScreen:setupCheckboxes()
	-- not used
end
-------------------------------------------------------------------------------

function PlayingModeScreen:initModeSliders()
	self.ModeSliders = {}
	self.ModeSliders.hintEnabled = { entity = self:createModeSlider(self:getEntityNamed('hint_slider_anchor'), 'hintEnabled'),
									parameter_name = 'hintRechargeTime', text_field_name = 'hint_time_text', text_const = 'PLAY_MODE_HINT_TIME'}
	self.ModeSliders.skipEnabled = { entity = self:createModeSlider(self:getEntityNamed('skip_slider_anchor'), 'skipEnabled'),
									parameter_name = 'skipRechargeTime', text_field_name = 'skip_time_text', text_const = 'PLAY_MODE_SKIP_TIME'}
	self:setupModeSliders()
end
-------------------------------------------------------------------------------

function PlayingModeScreen:setupModeSliders()
	self.bSetupModeSliders = true
	for checkbox_name, obj in pairs(self.ModeSliders) do
		local parameter_value = self.modeParameters[obj.parameter_name]
		local slider_value = (parameter_value - EPlayModeDefaultParameters['min_'..obj.parameter_name])/
				(EPlayModeDefaultParameters['max_'..obj.parameter_name] - EPlayModeDefaultParameters['min_'..obj.parameter_name])
		obj.entity:setValue(slider_value)
		self:printModeSlider(obj, parameter_value)
	end
	self.bSetupModeSliders = nil
end
-------------------------------------------------------------------------------

function PlayingModeScreen:createModeSlider(anchor, checkbox_name)
	local sliderBar       = CreateSpriteEntity('Data/Textures/Screens/Options/SliderBar.png')
	local sliderCursor    = CreateSpriteEntityInstant('Data/Textures/Screens/Options/SliderCursor.png')
	
	local slider = createSliderEntity(sliderBar, sliderCursor)
	slider:attachTo(anchor)
	slider:setActiveRectSize(300, sliderCursor:getHeight())
	slider.onValueChange:add(bind(self.onModeSliderChange, self, checkbox_name))

	return slider
end
-------------------------------------------------------------------------------

function PlayingModeScreen:onModeSliderChange(checkbox_name, entity, value)
	if not self.bSetupModeSliders then
		local obj = self.ModeSliders[checkbox_name]
		if not (self.mode == EPlayMode.CUSTOM) then
			self:onModeButtonClick(EPlayMode.CUSTOM)
			getCommonSoundEntity('ButtonClick'):play()
		end
		local parameter_value = math.floor(value*(EPlayModeDefaultParameters['max_'..obj.parameter_name] - EPlayModeDefaultParameters['min_'..obj.parameter_name])
								+ EPlayModeDefaultParameters['min_'..obj.parameter_name])
		self.modeParameters[obj.parameter_name] = parameter_value
		self:printModeSlider(obj, parameter_value)
	end
	--Console:pushText('Value = '..value)
end
-------------------------------------------------------------------------------

function PlayingModeScreen:setAvailableModeSlider(slider, bValue)
	local opacity_value = bValue and 1 or 0.8
	local color_value = bValue and Colorf(1, 1, 1, 1) or Colorf(0.5, 0.5, 0.5, 1)
	slider:getBarSprite():setColor(color_value)
	slider:getCursorSprite():setColor(color_value)
    slider:setOpacity(opacity_value)
	slider:setRespondable(bValue)
end
-------------------------------------------------------------------------------

function PlayingModeScreen:printModeSlider(slider_object, value)
	local str = StringManager:getString(slider_object.text_const)..' ('..value..' '..StringManager:getString('PLAY_MODE_SECOND')..'):'
	self:getEntityNamed(slider_object.text_field_name):setText(str)
end
-------------------------------------------------------------------------------

function PlayingModeScreen:setupModeButtons()
	self.button_name_list = {}
	self:initModeButton('bttn_casual',		EPlayMode.CASUAL)
	self:initModeButton('bttn_advanced',	EPlayMode.ADVANCED)
	self:initModeButton('bttn_hard',		EPlayMode.HARD)
	self:initModeButton('bttn_custom',		EPlayMode.CUSTOM)
	self:onModeButtonClick(self.mode)
end
-------------------------------------------------------------------------------

function PlayingModeScreen:initModeButton(bttn_name, bttn_mode)

	local bttn = self:getEntityNamed(bttn_name)
		  bttn.onPress:add(bind(self.onModeButtonClick, self, bttn_mode, true))
		  bttn:setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )
	self.button_name_list[bttn_mode] = bttn_name
	return bttn
end
-------------------------------------------------------------------------------

function PlayingModeScreen:onModeButtonClick(theMode, bReInitModeData)
	self.mode = theMode
	if bReInitModeData and (self.mode ~= EPlayMode.CUSTOM) then
		self:reInitModeData()
	end
	for bttn_mode, bttn_name in pairs(self.button_name_list) do
		local bttn = self:getEntityNamed(bttn_name)

		if theMode == bttn_mode then
			bttn:setActive(false)
			bttn:setPressed(true)
		else
			bttn:setActive(true)
			bttn:setPressed(false)
		end
	end
end
-------------------------------------------------------------------------------


-- function PlayingModeScreen:disableCheckBox(checkbox, checkboxBackground)
--     checkbox:setOpacity(0.1)
-- 	checkbox:setRespondable(false)
--     checkboxBackground:setOpacity(0.1)
-- end
-------------------------------------------------------------------------------

-- function PlayingModeScreen:enableCheckBox(checkbox, checkboxBackground)
--     checkbox:setOpacity(1)
-- 	checkbox:setRespondable(true)
--     checkboxBackground:setOpacity(1)
-- end
-------------------------------------------------------------------------------

function PlayingModeScreen:onOkButtonClick()
	SetGameState(PLAY_MODE, self.mode, self.tag)
	Helpers.SaveObjectToGameState(PLAY_MODE_PARAMETERS, self.modeParameters, self.tag)

	if current_scene then
		current_scene:getPanel():getControlsPanel():getHintAnimationLogic():setupFillDuration()

		if current_scene:getPanel():getMode() == EPanelMode.MINIGAME then
			-- skip
			if self.modeParameters.skipEnabled then
				current_scene:getPanel():getControlsPanel():getEntityNamed('bttn_skip'):setVisible(true)
				if not current_scene:getPanel():getControlsPanel():getHintAnimationLogic().is_skip_fill then
					current_scene:getPanel():getControlsPanel():getEntityNamed('bttn_skip'):setRespondable(true)
				end
			else
				current_scene:getPanel():getControlsPanel():getEntityNamed('bttn_skip'):hide()
			end
		elseif current_scene:getPanel():getMode() == EPanelMode.MATCH3 then
			-- HELP
			if self.modeParameters.hintEnabled then				
					current_scene:getPanel():getControlsPanel():getEntityNamed('bttn_help'):setVisible(true)
					if not current_scene:getPanel():getControlsPanel():getHintAnimationLogic().is_hint_fill then
						current_scene:getPanel():getControlsPanel():getEntityNamed('bttn_help'):setRespondable(true)
					end
			else
				current_scene:getPanel():getControlsPanel():getEntityNamed('bttn_help'):hide()
			
			end
		else
			-- hint
			if self.modeParameters.hintEnabled then
				current_scene:getPanel():getControlsPanel():getEntityNamed('bttn_hint'):setVisible(true)
				if not current_scene:getPanel():getControlsPanel():getHintAnimationLogic().is_hint_fill then
					current_scene:getPanel():getControlsPanel():getEntityNamed('bttn_hint'):setRespondable(true)
				end
			else
				current_scene:getPanel():getControlsPanel():getEntityNamed('bttn_hint'):hide()
			end
		end
		if current_scene:getPanel():getControlsPanel():getHintAnimationLogic().resetHintFillSpeed then
			current_scene:getPanel():getControlsPanel():getHintAnimationLogic():resetHintFillSpeed()
		end
	end -- current_scene

	self:onScreenDestroy()

	if self.ok_callback then
		self.ok_callback()
	end
end

function PlayingModeScreen:onCancelButtonClick()
	self:onScreenDestroy()

	local screen = PopUpScreensMgr:GetTop()
    
    if screen:hasEntityNamed('compositionBG') then
        screen:getEntityNamed('compositionBG'):getElementByName('layer_handle_mask'):setOpacity(0)
        screen:getEntityNamed('bttn_play'):show()
        screen:getEntityNamed('compositionStart'):hide()
    end
end

--------------------------------------------------------------------------------
--                      GENERATED CONTENT. DO NOT EDIT.                       --
--------------------------------------------------------------------------------

function PlayingModeScreen:CreateLoaderTable()
	local objects =
	{
		{ name = 'PlayingModeBackground',  entityType = 'SpriteEntity',  x = 512,  y = 384,  order = 20,  editingLocked = false,  treeItemIsExpanded = true,  hotSpot = {x = 265, y = 335},  texturePath = 'Data/Textures/Screens/PlayingMode/playing_mode_bg.png',  width = 530,  height = 670,  },
		{ name = 'bttn_cancel',  entityType = 'ButtonEntity',  parentName = 'PlayingModeBackground',  x = 57,  y = 329,  order = 25,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/GUI/Buttons/bttn_cancel.png',  text = '',  activeRectX = 130,  activeRectY = 60,  },
		{ name = 'bttn_ok',  entityType = 'ButtonEntity',  parentName = 'PlayingModeBackground',  x = -62,  y = 329,  order = 25,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/GUI/Buttons/bttn_ok.png',  text = '',  activeRectX = 130,  activeRectY = 60,  },
		{ name = 'bttn_casual',  entityType = 'ButtonEntity',  parentName = 'PlayingModeBackground',  x = -190,  y = -247,  order = 2,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/PlayingMode/playing_mode_casual_bttn.png',  text = '',  },
		{ name = 'bttn_advanced',  entityType = 'ButtonEntity',  parentName = 'PlayingModeBackground',  x = -60,  y = -246,  order = 2,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/PlayingMode/playing_mode_advancedl_bttn.png',  text = '',  },
		{ name = 'bttn_hard',  entityType = 'ButtonEntity',  parentName = 'PlayingModeBackground',  x = 65,  y = -248,  order = 2,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/PlayingMode/playing_mode_hard_bttn.png',  text = '',  },
		{ name = 'bttn_custom',  entityType = 'ButtonEntity',  parentName = 'PlayingModeBackground',  x = 182,  y = -248,  order = 2,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/PlayingMode/playing_mode_custom_bttn.png',  text = '',  },
		{ name = 'tutorial_text',  entityType = 'TextEntity',  parentName = 'PlayingModeBackground',  x = -222,  y = -186,  order = 50,  scale = 0.9,  editingLocked = false,  treeItemIsExpanded = true,  localizationKey = 'PLAY_MODE_TUTORIAL',  font = 'Data/Fonts/Options.fnt',  height = 33,  hotSpotType = TextEntity.ETextHotSpotTypeLeft,  },
		{ name = 'tutorial_checkbox',  entityType = 'SpriteEntity',  parentName = 'tutorial_text',  x = 484,  y = -3,  order = 50,  editingLocked = false,  treeItemIsExpanded = true,  hotSpot = {x = 31, y = 20},  texturePath = 'Data/Textures/Screens/Phone/task_checkbox_bg.png',  width = 40,  height = 42,  },
		{ name = 'sparkles_text',  entityType = 'TextEntity',  parentName = 'PlayingModeBackground',  x = -220,  y = -139,  order = 50,  scale = 0.9,  editingLocked = false,  treeItemIsExpanded = true,  localizationKey = 'PLAY_MODE_SPARKLES',  font = 'Data/Fonts/Options.fnt',  height = 33,  hotSpotType = TextEntity.ETextHotSpotTypeLeft,  },
		{ name = 'sparkles_checkbox',  entityType = 'SpriteEntity',  parentName = 'sparkles_text',  x = 484,  y = -3,  order = 50,  editingLocked = false,  treeItemIsExpanded = true,  hotSpot = {x = 31, y = 20},  texturePath = 'Data/Textures/Screens/Phone/task_checkbox_bg.png',  width = 38,  height = 40,  },
		{ name = 'sparkles_ho_text',  entityType = 'TextEntity',  parentName = 'PlayingModeBackground',  x = -220,  y = -94,  order = 50,  scale = 0.9,  editingLocked = false,  treeItemIsExpanded = true,  localizationKey = 'PLAY_MODE_SPARKLES_HO',  font = 'Data/Fonts/Options.fnt',  height = 33,  hotSpotType = TextEntity.ETextHotSpotTypeLeft,  },
		{ name = 'sparkles_ho_checkbox',  entityType = 'SpriteEntity',  parentName = 'sparkles_ho_text',  x = 484,  y = -3,  order = 50,  editingLocked = false,  treeItemIsExpanded = true,  hotSpot = {x = 31, y = 20},  texturePath = 'Data/Textures/Screens/Phone/task_checkbox_bg.png',  width = 38,  height = 40,  },
		{ name = 'blackbar_text',  entityType = 'TextEntity',  parentName = 'PlayingModeBackground',  x = -220,  y = -50,  order = 50,  scale = 0.9,  editingLocked = false,  treeItemIsExpanded = true,  localizationKey = 'PLAY_MODE_BLACKBAR',  font = 'Data/Fonts/Options.fnt',  height = 33,  hotSpotType = TextEntity.ETextHotSpotTypeLeft,  },
		{ name = 'blackbar_checkbox',  entityType = 'SpriteEntity',  parentName = 'blackbar_text',  x = 484,  y = -3,  order = 50,  editingLocked = false,  treeItemIsExpanded = true,  hotSpot = {x = 31, y = 20},  texturePath = 'Data/Textures/Screens/Phone/task_checkbox_bg.png',  width = 38,  height = 40,  },
		{ name = 'cursor_text',  entityType = 'TextEntity',  parentName = 'PlayingModeBackground',  x = -220,  y = -5,  order = 50,  scale = 0.9,  editingLocked = false,  treeItemIsExpanded = true,  localizationKey = 'PLAY_MODE_CURSOR_PUNISHMENT',  font = 'Data/Fonts/Options.fnt',  height = 33,  hotSpotType = TextEntity.ETextHotSpotTypeLeft,  },
		{ name = 'cursor_checkbox',  entityType = 'SpriteEntity',  parentName = 'cursor_text',  x = 484,  y = -3,  order = 50,  editingLocked = false,  treeItemIsExpanded = true,  hotSpot = {x = 31, y = 20},  texturePath = 'Data/Textures/Screens/Phone/task_checkbox_bg.png',  width = 38,  height = 40,  },
		{ name = 'hint_text',  entityType = 'TextEntity',  parentName = 'PlayingModeBackground',  x = -220,  y = 40,  order = 50,  scale = 0.9,  editingLocked = false,  treeItemIsExpanded = true,  localizationKey = 'PLAY_MODE_HINT',  font = 'Data/Fonts/Options.fnt',  height = 33,  hotSpotType = TextEntity.ETextHotSpotTypeLeft,  },
		{ name = 'hint_checkbox',  entityType = 'SpriteEntity',  parentName = 'hint_text',  x = 484,  y = -3,  order = 50,  editingLocked = false,  treeItemIsExpanded = true,  hotSpot = {x = 31, y = 20},  texturePath = 'Data/Textures/Screens/Phone/task_checkbox_bg.png',  width = 38,  height = 40,  },
		{ name = 'hint_time_text',  entityType = 'TextEntity',  parentName = 'PlayingModeBackground',  x = -219,  y = 86,  order = 50,  scale = 0.9,  editingLocked = false,  treeItemIsExpanded = true,  localizationKey = 'PLAY_MODE_HINT_TIME',  font = 'Data/Fonts/Options.fnt',  height = 33,  hotSpotType = TextEntity.ETextHotSpotTypeLeft,  },
		{ name = 'hint_slider_anchor',  entityType = 'SpriteEntity',  parentName = 'hint_time_text',  x = 277,  y = 35.5556,  order = 0,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 100,  height = 6,  },
		{ name = 'skip_text',  entityType = 'TextEntity',  parentName = 'PlayingModeBackground',  x = -220,  y = 181,  order = 50,  scale = 0.9,  editingLocked = false,  treeItemIsExpanded = true,  localizationKey = 'PLAY_MODE_SKIP',  font = 'Data/Fonts/Options.fnt',  height = 33,  hotSpotType = TextEntity.ETextHotSpotTypeLeft,  },
		{ name = 'skip_checkbox',  entityType = 'SpriteEntity',  parentName = 'skip_text',  x = 484,  y = -3,  order = 50,  editingLocked = false,  treeItemIsExpanded = true,  hotSpot = {x = 31, y = 20},  texturePath = 'Data/Textures/Screens/Phone/task_checkbox_bg.png',  width = 38,  height = 40,  },
		{ name = 'skip_time_text',  entityType = 'TextEntity',  parentName = 'PlayingModeBackground',  x = -220,  y = 229,  order = 50,  scale = 0.9,  editingLocked = false,  treeItemIsExpanded = true,  localizationKey = 'PLAY_MODE_SKIP_TIME',  font = 'Data/Fonts/Options.fnt',  height = 33,  hotSpotType = TextEntity.ETextHotSpotTypeLeft,  },
		{ name = 'skip_slider_anchor',  entityType = 'SpriteEntity',  parentName = 'skip_time_text',  x = 277,  y = 35.5556,  order = 0,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 100,  height = 6,  },
	}
	return objects
end
