//********************************//
//   	 Minecraft SWEP GUI       //
//********************************//

include( 'shared.lua' )
include( 'minecraft_blockpanel.lua' )
	
function optionspanel()
    local options = vgui.Create("DFrame")
	options:SetMouseInputEnabled(true)
	options:SetKeyboardInputEnabled(true)
    options:SetPos(550, 320)
    options:SetSize(250, 325)
    options:SetTitle( "Options" )
    options:SetVisible( true )
	options:Center()
    options:MakePopup()
    
    local swap = vgui.Create( "DCheckBoxLabel", options )
		swap:SetParent( options )
		swap:SetPos( 10,30 ) //10/36
		swap:SetText( "Swap destroy/create mouse buttons" )
		swap.OnChange = function(chkBox)
			if (swap:GetChecked() == true) then
				RunConsoleCommand("minecraft_swapattack", "1")
			end
			if (swap:GetChecked() == false) then
				RunConsoleCommand("minecraft_swapattack", "0")
			end
		end    
		if (GetConVar("minecraft_swapattack"):GetInt() > 0) then
			swap:SetValue( 1 )
		end
		if (GetConVar("minecraft_swapattack"):GetInt() < 1) then
			swap:SetValue( 0 )
		end
		swap:SizeToContents()
    
    local ds = vgui.Create( "DCheckBoxLabel", options )
		ds:SetParent( options )
		ds:SetPos( 10,50 ) //y + 20
		ds:SetText( "Disable sounds" )
		ds.OnChange = function(chkBox)
			if (ds:GetChecked() == true) then
				RunConsoleCommand("minecraft_disablesounds", "1")
			end
			if (ds:GetChecked() == false) then
				RunConsoleCommand("minecraft_disablesounds", "0")
			end
		end    
		if (GetConVar("minecraft_disablesounds"):GetInt() > 0) then
			ds:SetValue( 1 )
		end
		if (GetConVar("minecraft_disablesounds"):GetInt() < 1) then
			ds:SetValue( 0 )
		end
		ds:SizeToContents()
    
    local mo = vgui.Create( "DCheckBoxLabel", options )
		mo:SetParent( options )
		mo:SetPos( 10,70 ) //y + 20
		mo:SetText( "Only delete Minecraft blocks" )
		mo.OnChange = function(chkBox)
			if (mo:GetChecked() == true) then
				RunConsoleCommand("minecraft_deletemconly", "1")
			end
			if (mo:GetChecked() == false) then
				RunConsoleCommand("minecraft_deletemconly", "0")
			end
		end    
		if (GetConVar("minecraft_deletemconly"):GetInt() > 0) then
			mo:SetValue( 1 )
		end
		if (GetConVar("minecraft_deletemconly"):GetInt() < 1) then
			mo:SetValue( 0 )
		end
		mo:SizeToContents()
    
    local dl = vgui.Create( "DCheckBoxLabel", options )
		dl:SetParent( options )
		dl:SetPos( 10,96 ) //y + 20
		dl:SetText( "Distance limit" )
		dl.OnChange = function(chkBox)
			if (dl:GetChecked() == true) then
				RunConsoleCommand("minecraft_distancelimit", "1")
			end
			if (dl:GetChecked() == false) then
            RunConsoleCommand("minecraft_distancelimit", "0")
			end
		end    
		if (GetConVar("minecraft_distancelimit"):GetInt() > 0) then
			dl:SetValue( 1 )
		end
		if (GetConVar("minecraft_distancelimit"):GetInt() < 1) then
			dl:SetValue( 0 )
		end
		dl:SizeToContents()
    
    local NumSlider = vgui.Create( "DNumSlider", options )
		NumSlider:SetPos( 106,75 )
		NumSlider:SetWide( 130 )
		NumSlider:SetText( "" )
		NumSlider:SetMin( 0 ) -- Minimum number of the slider
		NumSlider:SetMax( 1000 ) -- Maximum number of the slider
		NumSlider:SetDecimals( 0 ) -- Sets a decimal. Zero means it's a whole number
		NumSlider:SetConVar( "minecraft_maxspawndist" ) -- Set the convar  
    
    local bh = vgui.Create( "DCheckBoxLabel", options )
		bh:SetParent( options )
		bh:SetPos( 10,133 ) //y + 20
		bh:SetText( "Block health" )
    local lasthealth = 0
		bh.OnChange = function(chkBox)
			if (bh:GetChecked() == true) then
				lasthealth = GetConVar("minecraft_blockhealth"):GetInt()
				if (lasthealth < 0) then
					lasthealth = 101
				end
				RunConsoleCommand("minecraft_blockhealth", tostring(lasthealth))
			end
			if (bh:GetChecked() == false) then
				RunConsoleCommand("minecraft_blockhealth", "-1") //invincible blocks
			end
		end    
		if ( GetConVar("minecraft_blockhealth" ):GetInt() > 0 ) then
			bh:SetValue( 1 )
		end
		if ( GetConVar( "minecraft_blockhealth") :GetInt() < 0 ) then
			bh:SetValue( 0 )
		end
		bh:SizeToContents()
    
    local NumSlider2 = vgui.Create( "DNumSlider", options )
		NumSlider2:SetPos( 106, 112 )
		NumSlider2:SetWide( 130 )
		NumSlider2:SetText( "" )
		NumSlider2:SetMin( -1 )
		NumSlider2:SetMax( 999 )
		NumSlider2:SetDecimals( 0 )
		NumSlider2:SetConVar( "minecraft_blockhealth" )
		NumSlider2.OnValueChanged = function ( pSelf, fValue )
			if (fValue < "0") then
				bh:SetValue(0)
			end
			if (fValue > "0") then
				bh:SetValue(1)
			end
			RunConsoleCommand( "minecraft_blockhealth", tostring(fValue) )
		end

    local lav1 = vgui.Create( "DCheckBoxLabel", options )
		lav1:SetParent( options )
		lav1:SetPos( 10,159 ) //y + 20
		lav1:SetText( "Lava ignites the player" )
		lav1.OnChange = function( chkBox )
			if ( lav1:GetChecked() == true ) then
				RunConsoleCommand( "minecraft_lavaigniteplayer", "1" )
			end
			if ( lav1:GetChecked() == false ) then
				RunConsoleCommand( "minecraft_lavaigniteplayer", "0" )
			end
		end    
		if ( GetConVar( "minecraft_lavaigniteplayer" ):GetInt() > 0) then
			lav1:SetValue( 1 )
		end
		if ( GetConVar( "minecraft_lavaigniteplayer" ):GetInt() < 1) then
			lav1:SetValue( 0 )
		end
		lav1:SizeToContents()
    
    local lav2 = vgui.Create( "DCheckBoxLabel", options )
		lav2:SetParent( options )
		lav2:SetPos( 10,179 ) //y + 20
		lav2:SetText( "Lava ignites other blocks" )
		lav2.OnChange = function( chkBox )
			if ( lav2:GetChecked() == true ) then
				RunConsoleCommand( "minecraft_lavaigniteblocks", "1" )
			end
			if ( lav2:GetChecked() == false ) then
				RunConsoleCommand( "minecraft_lavaigniteblocks", "0" )
			end
		end    
		if ( GetConVar( "minecraft_lavaigniteblocks" ):GetInt() > 0 ) then
			lav2:SetValue( 1 )
		end
		if ( GetConVar( "minecraft_lavaigniteblocks" ):GetInt() < 1 ) then
			lav2:SetValue( 0 )
		end
		lav2:SizeToContents()
		
    local wspread = vgui.Create( "DCheckBoxLabel", options )
		wspread:SetParent( options )
		wspread:SetPos( 10,205 ) //y + 26
		wspread:SetText( "Water and lava spread" )
		wspread.OnChange = function( chkBox )
			if ( wspread:GetChecked() == true ) then
				RunConsoleCommand( "minecraft_water_spread", "1" )
			end
			if ( wspread:GetChecked() == false ) then
				RunConsoleCommand( "minecraft_water_spread", "0" )
			end
		end    
		if ( GetConVar( "minecraft_water_spread" ):GetInt() > 0 ) then
			wspread:SetValue( 1 )
		end
		if ( GetConVar( "minecraft_water_spread" ):GetInt() < 1 ) then
			wspread:SetValue( 0 )
		end
		wspread:SizeToContents()
    
    local spreadslider = vgui.Create( "DNumSlider", options )
		spreadslider:SetPos( 10,230 )
		spreadslider:SetWide( 110 )
		spreadslider:SetText( "Water spread" )
		spreadslider:SetMin( 0 ) -- Minimum number of the slider
		spreadslider:SetMax( GetConVar("minecraft_water_spread_!LIMIT"):GetInt() ) -- Maximum number of the slider
		spreadslider:SetDecimals( 0 ) -- Sets a decimal. Zero means it's a whole number
		spreadslider:SetConVar( "minecraft_water_maxspread" ) -- Set the convar 
		
    local spreadslider2 = vgui.Create( "DNumSlider", options )
		spreadslider2:SetPos( 130,230 )
		spreadslider2:SetWide( 110 )
		spreadslider2:SetText( "Lava spread" )
		spreadslider2:SetMin( 0 ) -- Minimum number of the slider
		spreadslider2:SetMax( GetConVar("minecraft_water_spread_!LIMIT"):GetInt() ) -- Maximum number of the slider
		spreadslider2:SetDecimals( 0 ) -- Sets a decimal. Zero means it's a whole number
		spreadslider2:SetConVar( "minecraft_lava_maxspread" ) -- Set the convar 
		
    local mdebug = vgui.Create( "DCheckBoxLabel", options )
		mdebug:SetParent( options )
		mdebug:SetPos( 10,293 ) //y + 20 //245
		mdebug:SetText( "Debug info" )
		mdebug.OnChange = function( chkBox )
			if ( mdebug:GetChecked() == true ) then
				RunConsoleCommand( "minecraft_debug", "1" )
				RunConsoleCommand( "developer", "1" )
			end
			if ( mdebug:GetChecked() == false ) then
				RunConsoleCommand( "minecraft_debug", "0" )
			end
		end    
		if ( GetConVar( "minecraft_debug" ):GetInt() > 0 ) then
			mdebug:SetValue( 1 )
		end
		if ( GetConVar( "minecraft_debug" ):GetInt() < 1 ) then
			mdebug:SetValue( 0 )
		end
		mdebug:SizeToContents()
   
    local tomenu = vgui.Create("DButton")
		tomenu:SetParent( options )
		tomenu:SetText( "Close" )
		tomenu:SetPos( 90, 285 )
		tomenu:SetSize( 80, 30 )
		tomenu.DoClick = function( btn )

        options:Close()
    end
end

function infopanel()
    
    local info = vgui.Create( "DFrame" )
		info:SetMouseInputEnabled( true )
		info:SetKeyboardInputEnabled( true )
		info:SetPos( 550, 320 )
		info:SetSize( 250, 300 )
		info:SetTitle( "Readme!" )
		info:SetVisible( true )
		info:MakePopup()

    local  info1 = vgui.Create("DLabel", info)
		info1:SetText("Important stuff:")
		info1:SetPos(86, 24)
		info1:SizeToContents()
    
    local  info2 = vgui.Create("DLabel", info)
		info2:SetText("--- This is the garrysmod.org version!")
		info2:SetPos(10, 48)
		info2:SizeToContents()
    
    local  info20 = vgui.Create("DLabel", info)
		info20:SetText("- Right click a block type for faster switching")
		info20:SetPos(10, 64)
		info20:SizeToContents()

    local  info3 = vgui.Create("DLabel", info)
		info3:SetText("- Blocks are destructible")
		info3:SetPos(10, 80)
		info3:SizeToContents()
    
    local  info4 = vgui.Create("DLabel", info)
		info4:SetText("- Added lava and water blocks (ignite, extinguish)")
		info4:SetPos(10, 96)
		info4:SizeToContents()
    
    local  info5 = vgui.Create("DLabel", info)                   
		info5:SetText("  (you can swim in them, try jumping)")
		info5:SetPos(10, 112)
		info5:SizeToContents()
    
    local  info6 = vgui.Create("DLabel", info)
		info6:SetText("More than 1940 blocks will crash the game")
		info6:SetPos(10, 134)
		info6:SizeToContents()
    
    
    
    local  info7 = vgui.Create("DLabel", info)
		info7:SetText("Thank you for using this swep!")
		info7:SetPos(10, 160)
		info7:SizeToContents()
    
    local  info8 = vgui.Create("DLabel", info)
		info8:SetText("I'm working as hard as I can on updates,")
		info8:SetPos(10, 182)
		info8:SizeToContents()
    
    local  info9 = vgui.Create("DLabel", info)
		info9:SetText("so stay tuned for more blocks and features!")
		info9:SetPos(10, 196)
		info9:SizeToContents()
    
    local  info11 = vgui.Create("DLabel", info)
		info11:SetText( "This panel will only show up once" )
		info11:SetPos( 10, 230 )
		info11:SizeToContents()
    
    local tomenu = vgui.Create("DButton")
		tomenu:SetParent( info )
		tomenu:SetText( "Close" )
		tomenu:SetPos( 90, 260 )
		tomenu:SetSize( 80, 30 )
		tomenu.DoClick = function( btn )
			info:Close()
			RunConsoleCommand( "mc_menu" )
		end

end

function blockmenu()

    local blockcount = 0
	local crashcount = 0
	for k, v in pairs( ents.GetAll() ) do
        if ( v:IsValid() ) then
			if (v:GetClass() != "minecraft_block" && v:GetClass() != "minecraft_block_waterized" && v:GetClass() != "mc_tnt" && v:GetClass() != "mc_cake") then
				crashcount = crashcount + 1
			end
			if (v:GetClass() == "minecraft_block" || v:GetClass() == "minecraft_block_waterized" || v:GetClass() == "mc_tnt" || v:GetClass() == "mc_cake") then
				blockcount = blockcount + 1
			end
        end
    end
	crashcount = 2048 - crashcount

    local test = vgui.Create( "DFrame" )
	test:SetMouseInputEnabled( true )
	test:SetKeyboardInputEnabled( true )

	local spawntype = vgui.Create( "DCheckBoxLabel", DermaPanel )
	spawntype:SetParent( test )
	spawntype:SetPos( 710, 150 )
	spawntype:SetText( "Block height :" )
	spawntype.OnChange = function( chkBox )
		if ( spawntype:GetChecked() == true ) then
			RunConsoleCommand( "minecraft_spawntype", "2" )
		else
			RunConsoleCommand( "minecraft_spawntype", "1" )
		end
	end    
	if ( GetConVar( "minecraft_spawntype" ):GetInt() >= 2 ) then
		spawntype:SetValue( 1 )
	end
	if ( GetConVar( "minecraft_spawntype" ):GetInt() <= 1 ) then
		spawntype:SetValue( 0 )
	end
	spawntype:SizeToContents()

	spawntypelabel1 = vgui.Create( "DLabel", test )
	spawntypelabel1:SetPos( 660, 180 )
	spawntypelabel1:SetText( "Checked - blocks are placed normally." )
	spawntypelabel1:SizeToContents()

	spawntypelabel2 = vgui.Create( "DLabel", test )
	spawntypelabel2:SetPos( 660, 200 )
	spawntypelabel2:SetText( "Unchecked - blocks are placed lower" )
	spawntypelabel2:SizeToContents()

	spawntypelabel3 = vgui.Create( "DLabel", test )
	spawntypelabel3:SetPos( 690, 220 )
	spawntypelabel3:SetText( "by a half (very buggy)" )
	spawntypelabel3:SizeToContents()

    local options = vgui.Create( "DButton", test )
		options:SetText( "Options" )
		options:SetPos( 710, 270 )
		options:SetSize( 80, 30 )
		options.DoClick = function ( btn )
			RunConsoleCommand( "mc_options" )
		end
		
	local rotationlabel1 = vgui.Create( "DCheckBoxLabel", DermaPanel )
	rotationlabel1:SetParent( test )
	rotationlabel1:SetPos( 685, 350 )
	rotationlabel1:SetText( "Force block rotation" )
	rotationlabel1.OnChange = function( chkBox )
		if ( rotationlabel1:GetChecked() == true ) then
			RunConsoleCommand( "minecraft_blockrotation_force", "1" )
		else
			RunConsoleCommand( "minecraft_blockrotation_force", "0" )
		end
	end    
	if ( GetConVar( "minecraft_blockrotation_force" ):GetInt() >= 1 ) then
		rotationlabel1:SetValue( 1 )
	end
	if ( GetConVar( "minecraft_blockrotation_force" ):GetInt() < 1 ) then
		rotationlabel1:SetValue( 0 )
	end
	rotationlabel1:SizeToContents()

	//rotationlabel1 = vgui.Create( "DLabel", test )
	//rotationlabel1:SetPos( 710, 350 )
	//rotationlabel1:SetText( "Force Block Rotation" )
	//rotationlabel1:SizeToContents()

	rotationlabel2 = vgui.Create( "DLabel", test )
	rotationlabel2:SetPos( 690, 370 )
	rotationlabel2:SetText( "( Down arrow is default )" )
	rotationlabel2:SizeToContents()

	up = vgui.Create( "DSysButton", test )
	up:SetSize( 40, 40 )
	up:SetPos( 730, 390 )
	up:SetType( "up" )
	up.DoClick = function()
		RunConsoleCommand( "minecraft_blockrotation", "180" );
		close()
	end

	left = vgui.Create( "DSysButton", test )
	left:SetSize( 40, 40 )
	left:SetPos( 690, 430 )
	left:SetType( "left" )
	left.DoClick = function()
		RunConsoleCommand( "minecraft_blockrotation", "-90" );
		close()
	end

	right = vgui.Create( "DSysButton", test )
	right:SetSize( 40, 40 )
	right:SetPos( 770, 430 )
	right:SetType( "right" )
	right.DoClick = function()
		RunConsoleCommand( "minecraft_blockrotation", "90" );
		close()
	end

	down = vgui.Create( "DSysButton", test )
	down:SetSize( 40, 40 )
	down:SetPos( 730, 470 )
	down:SetType( "down" )
	down.DoClick = function()
		RunConsoleCommand( "minecraft_blockrotation", "0" );
		close()
	end

	local blockcounter = vgui.Create( "DLabel", test )
		blockcounter:SetText( "Placed Blocks : " .. tostring(blockcount))
		blockcounter:SetPos( 700, 560 )
		blockcounter:SizeToContents()
		
	local crashcounter = vgui.Create( "DLabel", test )
		crashcounter:SetText( "Would crash at "..tostring(crashcount).."." )
		crashcounter:SetPos( 700, 574 )
		crashcounter:SizeToContents()

    local removemenu = vgui.Create( "DButton" )
		removemenu:SetParent( test )
		removemenu:SetText( "Cleanup Blocks" )
		removemenu:SetPos( 700, 590 )
		removemenu:SetSize( 95, 35 )
		removemenu.DoClick = function ( btn )
			local menu1 = DermaMenu()
			menu1:AddOption( "Remove ALL the blocks!", function()
				blockcount = 0
				blockcounter:SetText( "Placed Blocks : " .. tostring( blockcount ) )
				if (CLIENT) then
					RunConsoleCommand( "minecraft_removeallblocks" )
				end
			end )
			menu1:AddOption( "Remove all blocks of the selected type", function()
				if (CLIENT) then
					RunConsoleCommand( "minecraft_removeselectedblocks" )
				end
			end )
			menu1:Open()
		end

	//this is the actual block panel
	local proppanel = vgui.Create("MCBlockPanel", test)
		local border = 5
		proppanel:SetParent( test )
		proppanel:SetPos( border, 20+border )
		proppanel:SetSize(600, ScrH()-(88*2)-25-border)
		proppanel:SetViewMode("Icon")
		proppanel:PerformLayout()
		for i,c in ipairs( BlockTypes ) do
			proppanel:AddModel(BlockTypes[i].model,0,BlockTypes[i].blockID)
		end

	test:SetSize( 890, ScrH()-(88*2) ) //875
	test:SetTitle( "Block Types" )
	test:SetVisible( true )
	test:SetDeleteOnClose( false )
	test:Center()
	test:MakePopup()
end
concommand.Add("mc_menu", blockmenu )
concommand.Add("mc_oldcloth", oldcloth )
concommand.Add("mc_newcloth", newcloth )
concommand.Add("mc_firstinfo", infopanel ) //info panel for all of you who are too stupid to read the fucking description
concommand.Add("mc_options", optionspanel )

CreateConVar( "minecraft_menu_savemousepos", "1", { FCVAR_REPLICATED } )
CreateConVar( "minecraft_debug", "0", { FCVAR_REPLICATED } )

CreateConVar( "minecraft_maxspawndist", "300", { FCVAR_REPLICATED } )
CreateConVar( "minecraft_lavaigniteplayer", "0", { FCVAR_REPLICATED } )
CreateConVar( "minecraft_lavaigniteblocks", "0", { FCVAR_REPLICATED } )
CreateConVar( "minecraft_deletemconly", "1", { FCVAR_REPLICATED } )

CreateConVar( "minecraft_blocktype", "1", { FCVAR_REPLICATED } )
CreateConVar( "minecraft_blockskin", "0", { FCVAR_REPLICATED } )

CreateConVar( "minecraft_disablesounds", "0", { FCVAR_REPLICATED } )
CreateConVar( "minecraft_distancelimit", "0", { FCVAR_REPLICATED } )
CreateConVar( "minecraft_blockhealth", "101", { FCVAR_REPLICATED } )
CreateConVar( "minecraft_blockhealth_auto", "1", { FCVAR_REPLICATED } )

CreateConVar( "minecraft_spawntype", "2", { FCVAR_REPLICATED } )
CreateConVar( "minecraft_swapattack", "0", { FCVAR_REPLICATED } )
CreateConVar( "minecraft_blockrotation", "0", { FCVAR_REPLICATED } )
CreateConVar( "minecraft_blockrotation_force", "0", { FCVAR_REPLICATED } )



CreateConVar( "mc_viewmodel_x", "-100", { FCVAR_REPLICATED } )
CreateConVar( "mc_viewmodel_y", "-28", { FCVAR_REPLICATED } )
CreateConVar( "mc_viewmodel_z", "-79", { FCVAR_REPLICATED } )

CreateConVar( "mc_viewmodel_dx", "0", { FCVAR_REPLICATED } )
CreateConVar( "mc_viewmodel_dy", "90", { FCVAR_REPLICATED } )
CreateConVar( "mc_viewmodel_dz", "-90", { FCVAR_REPLICATED } )

CreateConVar( "mc_viewmodel_rot_x", "9", { FCVAR_REPLICATED } )
CreateConVar( "mc_viewmodel_rot_y", "-4", { FCVAR_REPLICATED } )
CreateConVar( "mc_viewmodel_rot_z", "230", { FCVAR_REPLICATED } )

CreateConVar( "mc_viewmodel_rot_dx", "-70", { FCVAR_REPLICATED } )
CreateConVar( "mc_viewmodel_rot_dy", "20", { FCVAR_REPLICATED } )
CreateConVar( "mc_viewmodel_rot_dz", "10", { FCVAR_REPLICATED } )

CreateConVar( "mc_viewmodel_animtest", "0", { FCVAR_REPLICATED } )
CreateConVar( "mc_viewmodel_animspeed", "0.07", { FCVAR_REPLICATED } )
CreateConVar( "mc_viewmodel_animspeed_back", "0.04", { FCVAR_REPLICATED } )
CreateConVar( "mc_viewmodel_doAnim", "0", { FCVAR_REPLICATED } )