class 'OptionsScreenBase' (BaseScreen)

function OptionsScreenBase:__init(name)
	BaseScreen.__init(self, name)
	
	assert(current_player)
	
	-- save preferences
	do
		self.sound_value = current_player:getPreferences():getSoundVolume()
		self.music_value = current_player:getPreferences():getMusicVolume()
		self.voice_value = current_player:getPreferences():getVoiceVolume()
		if GetCursorEntity():isVisibleSystemCursor() then
			self.custom_cursor = false
		else
			self.custom_cursor = true
		end
	end

	self:setupButtons()
	self:setupSliders()
	self:setupCheckBoxes()
	self:setupTestSoundTrack()
end

function OptionsScreenBase:setupButtons()
	self:getEntityNamed('bttn_change_play_mode').onPress:add(bind(self.onChangePlayModeButtonClick, self))
	self:getEntityNamed('bttn_ok').onPress:add(bind(self.onOkButtonClick, self))
	self:getEntityNamed('bttn_cancel').onPress:add(bind(self.onCancelButtonClick, self))
	
	self:getEntityNamed('bttn_change_play_mode'):setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )
	self:getEntityNamed('bttn_ok'):setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )
	self:getEntityNamed('bttn_cancel'):setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )

	-- Если режим игры не добавлялся, то прячем кнопку bttn_change_play_mode
	if HasGameState(PLAY_MODE) == false then
		local bttn = self:getMainEntity():getEntityNamed('bttn_change_play_mode')
		bttn:hide()
	end

	if current_scene == nil then
		self:getMainEntity():getEntityNamed('bttn_change_play_mode'):hide()
	end
end

function OptionsScreenBase:setupSliders()
	self.music_slider = self:createSlider( self:getEntityNamed('music_slider'):getPosition(), current_player:getPreferences():getMusicVolume(), bind(self.musicChange, self) )
	self.sound_slider = self:createSlider( self:getEntityNamed('sound_slider'):getPosition(), current_player:getPreferences():getSoundVolume(), bind(self.soundChange, self) )
	self.voice_slider = self:createSlider( self:getEntityNamed('voice_slider'):getPosition(), current_player:getPreferences():getVoiceVolume(), bind(self.voiceChange, self) )
	
	if Application:getApplicationDescriptor().optionsSoundVolumeEnabled == false then
	    self:disableSlider(self.sound_slider)
	end
	if Application:getApplicationDescriptor().optionsMusicVolumeEnabled == false then
	    self:disableSlider(self.music_slider)
	    self:disableSlider(self.voice_slider)
	end
end

function OptionsScreenBase:disableSlider(slider)
    slider:setRespondable(false)
    slider:setOpacity(0.1)
end

function OptionsScreenBase:setupTestSoundTrack()
	local test_sound_track_path = self.test_sound_track_path or 'Data/Sounds/Common/ItemFound.ogg'
	local test_voice_track_path = self.test_voice_track_path or 'Data/Sounds/testVoice.ogg'

	if resourceExists(test_sound_track_path) then
		local testSound = CreateSoundTrack(test_sound_track_path)
		testSound:attachTo( self:getMainEntity() )
		testSound:setRepeated(true)

		self.sound_slider.onClickBegin:add(	function()
												testSound:play()
											end)

		self.sound_slider.onClickEnd:add(	function()
												testSound:stop()
											end)
	end

	if resourceExists(test_voice_track_path) then
		local testVoice = CreateVoiceTrack(test_voice_track_path)
		testVoice:attachTo( self:getMainEntity() )
		testVoice:setRepeated(true)

		self.voice_slider.onClickBegin:add(	function()
												testVoice:play()
											end)

		self.voice_slider.onClickEnd:add(	function()
												testVoice:stop()
											end)
	end
end

function OptionsScreenBase:setupCheckBoxes()
	local window = Application:getPlatform():getWindow()
	
	self.checkBoxFullScreen = self:createCheckBox(self:getEntityNamed('fullscreen_checkbox'):getPosition(), window:isFullscreen(), bind(self.fullScreenChange, self) )
	self.checkBoxCustomCurs = self:createCheckBox( self:getEntityNamed('custom_cursor_check_box'):getPosition(), self.custom_cursor, bind(self.customCursorChange, self) )
	self.checkBoxWideScreen = self:createCheckBox( self:getEntityNamed('widescreen_checkbox'):getPosition(), window:isWidescreen(), bind(self.wideScreenChange, self) )
	
	if Application:getApplicationDescriptor().optionsFullScreenEnabled == false then
	    self:disableCheckBox(self.checkBoxFullScreen, self:getEntityNamed('fullscreen_checkbox'))
	end
	if Application:getApplicationDescriptor().optionsWideScreenEnabled == false or not Application:getPlatform():getWindow():isFullscreen() then
	    self:disableCheckBox(self.checkBoxWideScreen, self:getEntityNamed('widescreen_checkbox'))
	end
	if Application:getApplicationDescriptor().optionsUseCustomCursorEnabled == false then
	    self:disableCheckBox(self.checkBoxCustomCurs, self:getEntityNamed('custom_cursor_check_box'))
	end
end

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

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

function OptionsScreenBase:__finalize()
end

function OptionsScreenBase:onChangePlayModeButtonClick()
	local callback = function()
		if not getPlayModeParameters().tutorialEnabled then -- GetGameState(PLAY_MODE):get(0) ~= EPlayMode.CASUAL
			if InteractiveHelp then
				InteractiveHelp.deactivate()
			end

			if current_scene:getTutorial() then
				current_scene:getTutorial():setRequested(true)
				current_scene:getTutorial():setActive(false)
			end
		end
	end

	createPlayingModeScreen(callback)
end

function OptionsScreenBase:onOkButtonClick()
	self:getEntityNamed('bttn_ok'):setRespondable(false)

	local window = Application:getPlatform():getWindow()

	local isFullScreen = self.checkBoxFullScreen:isChecked()
	window:setFullscreen(isFullScreen)
	
	if self.checkBoxWideScreen then
		local isWideScreen = self.checkBoxWideScreen:isChecked()
		window:setWidescreen(isWideScreen)
	end

	self:onScreenDestroy()
end

function OptionsScreenBase:onCancelButtonClick()
	current_player:getPreferences():setSoundVolume(self.sound_value)
	current_player:getPreferences():setMusicVolume(self.music_value)
	current_player:getPreferences():setVoiceVolume(self.voice_value)
	current_player:getPreferences():setShowCustomCursor(self.custom_cursor)
	
	AudioManager:setSoundVolume(self.sound_value)
	AudioManager:setMusicVolume(self.music_value)
	AudioManager:setVoiceVolume(self.voice_value)
	
	GetCursorEntity():setVisibleSystemCursor(not self.custom_cursor)

	self:onScreenDestroy()
end

function OptionsScreenBase:createSlider(pos, startValue, functionChange)
	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( self:getMainEntity() )
	slider:setActiveRectSize(270, 44)
	slider:setXY(pos.x, pos.y)
	slider:setOrder(20)
	slider:setValue(startValue)
	slider.onValueChange:add(functionChange)
	
	return slider
end

function OptionsScreenBase:createCheckBox(pos, startChecked, functionChange)
	spriteCheck = CreateSpriteEntity('Data/Textures/Screens/Options/CheckboxTick.png')
	
	local checkBox = createCheckBoxEntity(nil, spriteCheck)
	checkBox:attachTo( self:getMainEntity() )
	checkBox:setXY(pos.x, pos.y)
	checkBox:setOrder(20)
	checkBox:setChecked(startChecked)
	checkBox:setActiveRectSize(45, 45)
	checkBox.onCheck:add(functionChange)
	
	return checkBox
end

function OptionsScreenBase:musicChange(entity, value)
	current_player:getPreferences():setMusicVolume(value)
	AudioManager:setMusicVolume(value)
end

function OptionsScreenBase:soundChange(entity, value)
	current_player:getPreferences():setSoundVolume(value)
	AudioManager:setSoundVolume(value)
end

function OptionsScreenBase:voiceChange(entity, value)
	current_player:getPreferences():setVoiceVolume(value)
	AudioManager:setVoiceVolume(value)
end

function OptionsScreenBase:fullScreenChange(entity, value)
	playCommonSound('CheckBoxClick')
end

function OptionsScreenBase:customCursorChange(entity, value)
	current_player:getPreferences():setShowCustomCursor(value)
	GetCursorEntity():setVisibleSystemCursor(not value)

	playCommonSound('CheckBoxClick')
end

function OptionsScreenBase:wideScreenChange(entity, value)
	playCommonSound('CheckBoxClick')
end

function OptionsScreenBase:onScreenDestroy()
	ProfilesManager:save()
	PopUpScreensMgr:pop()
end

function OptionsScreenBase:onScreenModeChange()
	if not Application:getPlatform():getWindow():isFullscreen() then
	    self:disableCheckBox(self.checkBoxWideScreen, self:getEntityNamed('widescreen_checkbox'))
	else
	    self:enableCheckBox(self.checkBoxWideScreen, self:getEntityNamed('widescreen_checkbox'))
	end
end