require "StandardMouseButton"
require "CDialog"

-- reserve constructor
OptionsDialog = 
{
	name = "OptionsDialog"
}

function OptionsDialog:Create(  )
	
	-- The main screen container
	local instance = root.dialogs[ self.name ];
	instance:SetPosition( 139, 99 )
	CDialog:Attach( instance )
	
	-- The full screen button
	local fullScreenButton = instance._UI_FullScreen
	
	local softwareButton = instance._UI_Software
	
	local relaxedButton = instance._UI_Relaxed
	
	-- The ok button
	local okButton = instance.UI_OKButton
	
	-- THe cancel button
	local cancelButton = instance.UI_CancelButton
	
	-- The music slider
	local musicSlider = instance.musicSlider
	
	-- The music slider
	local ambientSlider = instance.ambientSlider
	
	-- The music slider
	local effectsSlider = instance.effectsSlider
	
	instance.testNum = 55;

	-- The main init function
	function instance:Init( )
		log( "init" )
			
		StandardMouseButton( okButton );
		okButton:Init( )
		StandardMouseButton( cancelButton );
		cancelButton:Init( )
		fullScreenButton:Init( )
		musicSlider:Init( )
		ambientSlider:Init( )
		effectsSlider:Init( )
		softwareButton:Init( )
		relaxedButton:Init( )
		
	end
	
	function instance:OnUpdate( )
		if( instance.inGame ~= nil and instance.inGame == true ) then
			instance:SetPosition( 208, 99 )	
		end

		self:OnUpdateDialog()
	end

	-- Handle keyboard input for the dialog
	function instance:CharDown( i_Key )
		log( "On Key Down" .. i_Key )
	end

	-- The ok button
	function okButton:Init( )
		function okButton:Pressed( )
			instance:CloseAndSave( )
		end
		self:AddAsGlobalHandler()
		function okButton:OnKeyDown( i_Key )
			-- press ok on pressing return button
			if ( i_Key == 13 ) then
				okButton:Pressed()
			end
		end
	end
	
	-- The cancel button
	function cancelButton:Init( )
		function cancelButton:Pressed( )
			instance:CloseAndCancel( )
		end
	end
	
	-- The software button
	function softwareButton:Init( )
	
		if( instance.Software == true ) then
			softwareButton:SetFrameFromLabel( "checked" )
		else
			softwareButton:SetFrameFromLabel( "unchecked" )
		end
			
		function softwareButton:OnMouseEnter( )
		--	PlaySound( "Roll" )
			if( instance.Software == true ) then
				self:SetFrameFromLabel( "checkedover" )	
			else
				self:SetFrameFromLabel( "uncheckedover" )
			end
		end
		
		function softwareButton:OnMouseLeave( )
			if( instance.Software == true ) then
				self:SetFrameFromLabel( "checked" )	
			else
				self:SetFrameFromLabel( "unchecked" )
			end
		end
		function softwareButton:OnLeftMouseDown( )
			if( instance.Software == true ) then
				log( "checked down" )
				self:SetFrameFromLabel( "checkeddown" )
			else
				log( "unchecked down" )
				self:SetFrameFromLabel( "uncheckeddown" )
			end
		end
		
		function softwareButton:OnLeftMouseUp( )
			PlaySound( "Click" )
			if( instance.Software == true ) then
				instance.Software = false
				self:SetFrameFromLabel( "uncheckedover" );
			else
				instance.Software = true
				self:SetFrameFromLabel( "checkedover" );
			end
			
			if( instance.Software == true ) then
				log( "true"  )
			elseif( instance.Software == false ) then
				log( "false" )
			end	
		end
	
	end	
	
	-- The relaxed button
	function relaxedButton:Init( )
	
		if( instance.Relaxed == true ) then
			relaxedButton:SetFrameFromLabel( "checked" )
		else
			relaxedButton:SetFrameFromLabel( "unchecked" )
		end
			
		function relaxedButton:OnMouseEnter( )
			--PlaySound( "Roll" )
			if( instance.Relaxed == true ) then
				self:SetFrameFromLabel( "checkedover" )	
			else
				self:SetFrameFromLabel( "uncheckedover" )
			end
		end
		
		function relaxedButton:OnMouseLeave( )
			if( instance.Relaxed == true ) then
				self:SetFrameFromLabel( "checked" )	
			else
				self:SetFrameFromLabel( "unchecked" )
			end
		end
		function relaxedButton:OnLeftMouseDown( )
			if( instance.Relaxed == true ) then
				log( "checked down" )
				self:SetFrameFromLabel( "checkeddown" )
			else
				log( "unchecked down" )
				self:SetFrameFromLabel( "uncheckeddown" )
			end
		end
		
		function relaxedButton:OnLeftMouseUp( )
			PlaySound( "Click" )
			if( instance.Relaxed == true ) then
				instance.Relaxed = false
				self:SetFrameFromLabel( "uncheckedover" );
			else
				instance.Relaxed = true
				self:SetFrameFromLabel( "checkedover" );
			end
			
			if( instance.Relaxed == true ) then
				log( "true"  )
			elseif( instance.Relaxed == false ) then
				log( "false" )
			end	
		end
	
	end	
	
	-- The fullscreen button
	function fullScreenButton:Init( )
	
		if( instance.FullScreen == true ) then
			fullScreenButton:SetFrameFromLabel( "checked" )
		else
			fullScreenButton:SetFrameFromLabel( "unchecked" )
		end
			
		function fullScreenButton:OnMouseEnter( )
			--PlaySound( "Roll" )
			if( instance.FullScreen == true ) then
				self:SetFrameFromLabel( "checkedover" )	
			else
				self:SetFrameFromLabel( "uncheckedover" )
			end
		end
		
		function fullScreenButton:OnMouseLeave( )
			if( instance.FullScreen == true ) then
				self:SetFrameFromLabel( "checked" )	
			else
				self:SetFrameFromLabel( "unchecked" )
			end
		end
		function fullScreenButton:OnLeftMouseDown( )
			if( instance.FullScreen == true ) then
				log( "checked down" )
				self:SetFrameFromLabel( "checkeddown" )
			else
				log( "unchecked down" )
				self:SetFrameFromLabel( "uncheckeddown" )
			end
		end
		
		function fullScreenButton:OnLeftMouseUp( )
			PlaySound( "Click" )
			if( instance.FullScreen == true ) then
				instance.FullScreen = false
				self:SetFrameFromLabel( "uncheckedover" );
			else
				instance.FullScreen = true
				self:SetFrameFromLabel( "checkedover" );
			end
			
			if( instance.FullScreen == true ) then
				log( "true"  )
			elseif( instance.FullScreen == false ) then
				log( "false" )
			end	
		end
	
	end
	
	-- The music volume slider
	function musicSlider:Init( )
	
		-- The handle for the music slider
		local musicSliderHandle = root.dialogs.OptionsDialog.musicSlider.handle;
		musicSliderHandle.xoffset = 0;
		musicSliderHandle:SetX( instance.MusicVolume * musicSlider:GetWidth() );
		
		-- make sure we get updates
		musicSliderHandle:SetOnUpdateEnabled( true );
		
		function musicSliderHandle:Init( )
			log( "down" )
			local drag = false
			
			function musicSliderHandle:OnMouseEnter( )
			--	PlaySound( "Roll" )
				self:SetFrameFromLabel( "over" )
			end
			function musicSliderHandle:OnMouseLeave( )
				self:SetFrameFromLabel( "rest" )
			end
			
			function musicSliderHandle:OnLeftMouseUp( )
				self:SetFrameFromLabel( "over" )
				PlaySound( "Click" )
				drag = false
			end
			
			function musicSliderHandle:OnLeftMouseUpOutside( )
				self:SetFrameFromLabel( "rest" )
				PlaySound( "Click" )
				drag = false
			end
					
			function musicSliderHandle:OnLeftMouseDown( )
				
				self:SetFrameFromLabel( "down" )
				self.xoffset = self:GlobalToLocalX( GetMouseX( ) )
				drag = true;
			end
			
			function musicSliderHandle:OnUpdate( )
				log("foo")
				if( drag == true ) then
					log("bar")
					local width = musicSlider:GetWidth( )
					
					local newPos = musicSlider:GlobalToLocalX( GetMouseX( ) )-self.xoffset;
					
					if( newPos  > width - musicSliderHandle:GetWidth( ) ) then 
						newPos = width - musicSliderHandle:GetWidth( )
					end	
					
					if( newPos < 0 ) then
						newPos = 0
					end
					
					musicSliderHandle:SetPosition( newPos, musicSliderHandle:GetY( ) )
					
					local oldVolume = instance.MusicVolume;
					
					instance.MusicVolume = ( ( newPos/width ) )
								
				end
			end
			
		end
		
		musicSliderHandle:Init( )
	end
	
	-- The ambient volume slider
	function ambientSlider:Init( )
	
		-- The handle for the ambient slider
		local ambientSliderHandle = root.dialogs.OptionsDialog.ambientSlider.handle;
		ambientSliderHandle.xoffset = 0;
		ambientSliderHandle:SetX( instance.AmbientVolume * ambientSlider:GetWidth() );
		
		-- make sure we get updates
		ambientSliderHandle:SetOnUpdateEnabled( true );
		
		function ambientSliderHandle:Init( )
			log( "down" )
			local drag = false
			
			function ambientSliderHandle:OnMouseEnter( )
			--	PlaySound( "Roll" )
				self:SetFrameFromLabel( "over" )
			end
			function ambientSliderHandle:OnMouseLeave( )
				self:SetFrameFromLabel( "rest" )
			end
			
			function ambientSliderHandle:OnLeftMouseUp( )
				PlaySound( "Click" )
				self:SetFrameFromLabel( "over" )
				drag = false
				PlaySoundEx( "EnvironmentVolume", instance.AmbientVolume )
			end
			
			function ambientSliderHandle:OnLeftMouseUpOutside( )
				PlaySound( "Click" )
				self:SetFrameFromLabel( "rest" )
				drag = false
				PlaySoundEx( "EnvironmentVolume", instance.AmbientVolume )
			end
					
			function ambientSliderHandle:OnLeftMouseDown( )
				self:SetFrameFromLabel( "down" )
				self.xoffset = self:GlobalToLocalX( GetMouseX( ) )
				drag = true;
			end
			
			function ambientSliderHandle:OnUpdate( )
				if( drag == true ) then
				
					local width = ambientSlider:GetWidth( )
					
					local newPos = ambientSlider:GlobalToLocalX( GetMouseX( ) )-self.xoffset;
					
					if( newPos  > width - ambientSliderHandle:GetWidth( ) ) then 
						newPos = width - ambientSliderHandle:GetWidth( )
					end	
					
					if( newPos < 0 ) then
						newPos = 0
					end
					
					ambientSliderHandle:SetPosition( newPos, ambientSliderHandle:GetY( ) )
					
					local oldVolume = instance.ambientVolume;
					
					instance.AmbientVolume = ( ( newPos/width ) )
				end
			end
			
		end
		
		ambientSliderHandle:Init( )
	end
	
	-- The effects volume slider
	function effectsSlider:Init( )
	
		-- The handle for the effects slider
		local effectsSliderHandle = root.dialogs.OptionsDialog.effectsSlider.handle;
		effectsSliderHandle.xoffset = 0;
		effectsSliderHandle:SetX( instance.EffectsVolume * effectsSlider:GetWidth() );
		
		-- make sure we get updates
		effectsSliderHandle:SetOnUpdateEnabled( true );
		
		function effectsSliderHandle:Init( )
			log( "down" )
			local drag = false
			
			function effectsSliderHandle:OnMouseEnter( )
			--	PlaySound( "Roll" )
				self:SetFrameFromLabel( "over" )
			end
			function effectsSliderHandle:OnMouseLeave( )
				self:SetFrameFromLabel( "rest" )
			end
			
			function effectsSliderHandle:OnLeftMouseUp( )
				PlaySound( "Click" )
				self:SetFrameFromLabel( "over" )
				drag = false
				PlaySoundEx( "SliderMatch", instance.EffectsVolume )
			end
			
			function effectsSliderHandle:OnLeftMouseUpOutside( )
				PlaySound( "Click" )
				self:SetFrameFromLabel( "rest" )
				drag = false
				PlaySoundEx( "SliderMatch", instance.EffectsVolume )
			end
					
			function effectsSliderHandle:OnLeftMouseDown( )
				self:SetFrameFromLabel( "down" )
				self.xoffset = self:GlobalToLocalX( GetMouseX( ) )
				drag = true;
			end
			
			function effectsSliderHandle:OnUpdate( )
				if( drag == true ) then
				
					local width = effectsSlider:GetWidth( )
					
					local newPos = effectsSlider:GlobalToLocalX( GetMouseX( ) )-self.xoffset;
					
					if( newPos  > width - effectsSliderHandle:GetWidth( ) ) then 
						newPos = width - effectsSliderHandle:GetWidth( )
					end	
					
					if( newPos < 0 ) then
						newPos = 0
					end
					
					effectsSliderHandle:SetPosition( newPos, effectsSliderHandle:GetY( ) )
					
					local oldVolume = instance.effectsVolume;
					
					instance.EffectsVolume = ( ( newPos/width ) )
				end
			end
			
		end
		
		effectsSliderHandle:Init( )
	end
end

OptionsDialog:Create( )
