require "StandardMouseButton"
require "StandardRadioButton"
require "CDialog"

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

function Menu_ProfileDialog:Create(  )
	-- The main screen container
	local instance = root.dialogs[ self.name ];
	instance:SetPosition( 139, 99 )
	CDialog:Attach( instance )
	
	-- The ok button
	local okButton = instance.button
	local removeButton = instance.deletebutton
	
	local radioButton1 = instance.radio1
	local radioButton2 = instance.radio2
	local radioButton3 = instance.radio3
	local radioButton4 = instance.radio4
	local radioButton5 = instance.radio5
	local radioButtons = {}
	local addNewOnClose = false
	--local selectedIndex = 1
	
	instance:SetOnUpdateEnabled( true )
	
	-- The main init function
	function instance:Init( )
	
		instance.cursorX = 0;
		instance.pleaseDelete = nil;
		
		StandardMouseButton( okButton );
		okButton:Init( )
		
		StandardMouseButton( removeButton )
		removeButton:Init( )
	
		StandardRadioButton( radioButton1 )
		radioButton1:Init( )
		StandardRadioButton( radioButton2 )
		radioButton2:Init( )
		StandardRadioButton( radioButton3 )
		radioButton3:Init( )
		StandardRadioButton( radioButton4 )
		radioButton4:Init( )
		StandardRadioButton( radioButton5 )
		radioButton5:Init( )
		
		radioButtons[ 1 ] = radioButton1
		radioButtons[ 2 ] = radioButton2
		radioButtons[ 3 ] = radioButton3
		radioButtons[ 4 ] = radioButton4
		radioButtons[ 5 ] = radioButton5
		
		-- set up all the profile buttons	
		CreateProfileButton( 1, instance:GetProfileText(0), instance.profile1 )
		CreateProfileButton( 2, instance:GetProfileText(1), instance.profile2 )
		CreateProfileButton( 3, instance:GetProfileText(2), instance.profile3 )
		CreateProfileButton( 4, instance:GetProfileText(3), instance.profile4 )
		CreateProfileButton( 5, instance:GetProfileText(4), instance.profile5 )
		log( tostring( instance.selectedIndex ) )
		radioButtons[ tonumber( instance.selectedIndex ) ]:Check( )
		
		-- if this is a repeat play (user has won and presed Play Again from the victory page) 
		-- then simulate a click on the first empty profile to help the user out
		if ( instance.repeatPlay ~= -1 ) then
			radioButtons[ instance.repeatPlay ]:OnLeftMouseUp()
		end
	end

	function instance:SetCursorX( x )
		instance.cursorX = x
	end
	
	function instance:SetSelectedIndex( i )
		instance.selectedIndex = i
	end
	
	function instance:SetRepeatPlay( r )
		instance.repeatPlay = r
	end

	function instance:OnUpdate( )
		-- if a delete has been requested, carry it out
		
		if (instance.pleaseDelete ~= nil) then
			log( "deleteing string: " .. instance.pleaseDelete )
			
			-- find the requested button
			for i=1,5,1 do
				local button = instance["profile"..i]	
				
				-- clear it
				if (button.text == instance.pleaseDelete) then
					button.text = "New Profile"
					button.profileName:SetText("|300|Profile UI|New Profile|" )
				
					instance.pleaseDelete = nil;	
				end
			end
		end

		self:OnUpdateDialog()
	end

	function radioButton1:Init( )
		function radioButton1:Pressed( )
			radioButtons[ instance.selectedIndex ]:UnCheck( )
			instance.selectedIndex = 1
			instance:CheckProfileSwitch( )
		end
	end
	function radioButton2:Init( )
		function radioButton2:Pressed( )
			log( "press" )
			radioButtons[ instance.selectedIndex ]:UnCheck( )
			instance.selectedIndex = 2
			instance:CheckProfileSwitch( )
		end
	end
	function radioButton3:Init( )
		function radioButton3:Pressed( )
			log( "press" )
			radioButtons[ instance.selectedIndex ]:UnCheck( )
			instance.selectedIndex = 3
			instance:CheckProfileSwitch( )
		end
	end
	
	function radioButton4:Init( )
		function radioButton4:Pressed( )
			log( "press" )
			radioButtons[ instance.selectedIndex ]:UnCheck( )
			instance.selectedIndex = 4
			instance:CheckProfileSwitch( )
		end
	end
	
	function radioButton5:Init( )
		function radioButton5:Pressed( )
			log( "press" )
			radioButtons[ instance.selectedIndex ]:UnCheck( )
			instance.selectedIndex = 5
			instance:CheckProfileSwitch( )
		end
	end

	-- Handle keyboard input for the dialog
	function instance:CharDown( i_Key )
		log( "On Key Down" .. i_Key )
	end
	
	
	function instance:CheckProfileSwitch( )
		--log( 'Prev index ' .. instance.selectedIndex )
		-- make the index 0 based, as the C++ function expects indices in the range [0, MAX_PROFILES - 1], 
		-- while the selected index for the radio button table is in the range [1, MAX_PROFILES]
		local used = instance:GetProfileStatus( instance.selectedIndex - 1 )
		
		--log( tostring( profileStatus1 ) )
		
		if( addNewOnClose ) then
			instance:ScreenEvent( "ClearInputProfile", "Menu_ProfileDialog" );
		end
		
		addNewOnClose = false;
		
		if( used == 1 ) then
			log ( "used profile slot" )
			okButton.text:SetFrame( 0 );
			instance:ScreenEvent( "Profile Switch", tostring( instance.selectedIndex) );
		else
			log( "new profile slot" )
			addNewOnClose = true;
			okButton.text:SetFrame( 1 );
			instance:ScreenEvent( "New Entry Profile Entry", tostring( instance.selectedIndex) );
		end
	end

	-- The ok button
	function okButton:Init( )
		-- handle mouseover for buttons here....
		function okButton:Pressed( )
			if( addNewOnClose == true ) then
				instance:ScreenEvent( "AddNewProfile", "Menu_ProfileDialog" )		
			end
			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 ok button
	function removeButton:Init( )
		-- handle mouseover for buttons here....
		function removeButton:Pressed( )			
			if( addNewOnClose ~= true ) then
				instance:ScreenEvent( "Confirm Delete Prompt", "Menu_ProfileDialog" );
			end
		end
	end

	
	function CreateProfileButton( index, text, newInstance )
		
		newInstance.index = index
		newInstance.text = text
		
		newInstance.profileName:SetText( text )
		newInstance.bar:SetVisible( false )
		
		function newInstance:OnLeftMouseUp( )
			-- do some profile stuff here....
			log( "pb: " .. newInstance.index .. " clicked" )
		end
		
		function newInstance:OnUpdate( )
			if( addNewOnClose and newInstance.index == instance.selectedIndex ) then
				if( newInstance.bar:GetVisible( ) == false ) then
					newInstance.bar:SetVisible( true )
				end
				
				if( instance.cursorX ~= nil ) then
					newInstance.bar:SetPlaying( true )
					newInstance.bar:SetX( tonumber( instance.cursorX ) + 4 );
				end
			else
				if( newInstance.bar:GetVisible( ) == true ) then
					newInstance.bar:SetVisible( false )
				end
			end
		end
	end
	
	
	function CreateDeleteButton( index, text, newInstance )
		
		newInstance.index = index
		newInstance.text = text
		
		function newInstance:OnLeftMouseUp( )
			-- do some profile stuff here....
			log( "pb: delete " .. newInstance.index .. " clicked" )
			
			if (newInstance.text ~= "New Profile") then
				instance:ScreenEvent( "Confirm Delete Prompt", newInstance.text )
			end
		end
		
		-- handle mouseover for buttons here....
		function newInstance:OnMouseEnter( )
			log("pb: del mouse enter")
			newInstance:SetFrame(1)
		end
		
		function newInstance:OnMouseLeave( )
			log("pb: del mouse leave")
			newInstance:SetFrame(0)
		end
	end
end

Menu_ProfileDialog:Create( )
