//ATM, Cl_init
//Coded By: Fish

include( 'shared.lua' )
--
require( "datastream" )

ENT.RenderGroup = RENDERGROUP_BOTH

local PersonPIN = {}
local PersonPINSelect = {}

function ENT:Draw()
    self.Entity:DrawModel()
end

function ATM( um )
	datastream.StreamToServer( "CheckPIN", { "" } )
end
usermessage.Hook( "ATMCall", ATM )

local function CommaTheCash( num )
    if ( !num ) then 
        return
    end
    if ( num ) then
        local len = string.len( num )
        for i = len - 3, 1, -3 do 
            num = string.sub( num, 1, i ) .. "," .. string.sub( num, i + 1 )
        end
        return num
    end
end

local function PINAlreadySet( handler, id, encoded, decoded )

	PersonPIN = {}
	PersonPINSelect = {}

	DermaPanel5 = vgui.Create( "DFrame" )
	DermaPanel5:SetPos( ScrW() / 2 - 100, ScrH() / 2 - 150 )
	DermaPanel5:SetSize( 165, 250 )
	DermaPanel5:SetTitle( "ATM Banker v3" ) //Feel free to change the name
	DermaPanel5:SetVisible( true )
	DermaPanel5:SetDraggable( true )
	DermaPanel5:ShowCloseButton( true )
	DermaPanel5:MakePopup()

	PropertySheet = vgui.Create( "DPropertySheet", DermaPanel5 )
	PropertySheet:SetPos( 0, 25 )
	PropertySheet:SetSize( 165, 225 )

	local TabA = vgui.Create( "DPanelList", PropertySheet )
	TabA:SetPos( 0, 0 )

	local DLabel2 = vgui.Create( "DLabel", TabA )
	DLabel2:SetPos( 35, 85  )
	DLabel2:SetColor( Color( 255, 255, 255, 255 ) )
	DLabel2:SetFont( "TabLarge" )
	DLabel2:SetText( "PIN:" )
	DLabel2:SizeToContents()

	myText1 = vgui.Create( "DTextEntry", TabA )
	myText1:SetSize( 50, 15 ) //SetSize( 33.5, 15 )
	myText1:SetPos( 58, 85 )
	myText1:RequestFocus()
	myText1.OnEnter = function()
		datastream.StreamToServer( "PINValid", { tostring( util.CRC( myText1:GetValue() ) ), PersonPIN[1] } )
	end

	local button1 = vgui.Create( "DButton", TabA )
	button1:SetSize( 55, 15 )
	button1:SetPos( 45.5, 105 )
	button1:SetText( "Accept" )
	button1.DoClick = function()
		datastream.StreamToServer( "PINValid", { tostring( util.CRC( myText1:GetValue() ) ), PersonPIN[1] } )
	end
	
	local DLabel1 = vgui.Create( "DLabel", TabA )
	DLabel1:SetPos( 33.5, 135 )
	DLabel1:SetColor( Color( 255, 255, 255, 255 ) )
	DLabel1:SetFont( "TabLarge" )
	DLabel1:SetText( "Select Account:" )
	DLabel1:SizeToContents()

	table.insert( PersonPIN, tostring( LocalPlayer() ) )

	List = vgui.Create( "DMultiChoice", TabA )
	List:SetPos( 25.5, 152.5 ) //SetPos( 22.5, 43.5 )
	List:SetSize( 100, 20 )
	List:SetText( tostring( LocalPlayer():Nick() ) )
	for k, v in pairs( player.GetAll() ) do
	List:AddChoice( tostring( v:Nick() ) )
	end
	List.OnSelect = function( panel, index, value, data )
	PersonPIN = {}
	for k, v in pairs( player.GetAll() ) do
	if tostring( v:Nick() ) == tostring( value ) then
	table.insert( PersonPIN, v )
			end
		end
	end

	local button2 = vgui.Create( "DButton", TabA )
	button2:SetSize( 65, 25 )
	button2:SetPos( 42.5, 30 )
	button2:SetText( "Lost My PIN" )
	button2.DoClick = function()

		----

		DermaPanel5:SetVisible( false )
		DermaPanel5:Close()

		--

		DermaPanel4 = vgui.Create( "DFrame" )
		DermaPanel4:SetPos( ScrW() / 2 - 100, ScrH() / 2 - 100 )
		DermaPanel4:SetSize( 150, 100 )
		DermaPanel4:SetTitle( "ATM PIN SET-UP" )
		DermaPanel4:SetVisible( true )
		DermaPanel4:SetDraggable( false )
		DermaPanel4:ShowCloseButton( true )
		DermaPanel4:MakePopup()
		
		local DLabel5 = vgui.Create( "DLabel", DermaPanel4 )
		DLabel5:SetPos( 22, 25 )
		DLabel5:SetColor( Color( 255, 255, 255, 255 ) )
		DLabel5:SetFont( "TabLarge" )
		DLabel5:SetText( "Please Create a PIN." )
		DLabel5:SizeToContents()

		local DLabel6 = vgui.Create( "DLabel", DermaPanel4 )
		DLabel6:SetPos( 20, 39.5 )
		DLabel6:SetColor( Color( 255, 0, 0, 255 ) )
		DLabel6:SetFont( "TabLarge" )
		DLabel6:SetText( "*DIGITS + LETTERS*" )
		DLabel6:SizeToContents()

		local myText5 = vgui.Create( "DTextEntry", DermaPanel4 )
		myText5:SetSize( 50, 18 )
		myText5:SetPos( 45, 57.5 )
		myText5:RequestFocus()

		local button5 = vgui.Create( "DButton", DermaPanel4 )
		button5:SetSize( 65, 18 )
		button5:SetPos( 37.5, 78.5 )
		button5:SetText( "Set PIN" )
		button5.DoClick = function()
			datastream.StreamToServer( "SetPIN", { myText5:GetValue() } )
		end
		
		LocalPlayer():ChatPrint( "Security Note: DO NOT USE FREQUENTLY-USED, PERSONAL PASSWORDS." )
		
	end
	
	PropertySheet:AddSheet( "ATM", TabA, "gui/silkicons/shield", false, false, "ATM Security" )

	--------------------------------------------------------------------------------------------

	if LocalPlayer():IsSuperAdmin() then
		local TabB = vgui.Create( "DPanelList", PropertySheet )
		TabB:SetPos( 0, 0 )

		List2 = vgui.Create( "DMultiChoice", TabB )
		List2:SetPos( 25.5, 170 )
		List2:SetSize( 100, 20 )
		List2:SetText( tostring( LocalPlayer():Nick() ) )
		for k, v in pairs( player.GetAll() ) do
		List2:AddChoice( tostring( v:Nick() ) )
		end
		List2.OnSelect = function( panel, index, value, data )
		PersonPINSelect = {}
		for k, v in pairs( player.GetAll() ) do
		if tostring( v:Nick() ) == tostring( value ) then
		table.insert( PersonPINSelect, v )
				end
			end
		end

		table.insert( PersonPINSelect, LocalPlayer() ) 

		local myText11 = vgui.Create( "DTextEntry", TabB )
		myText11:SetSize( 50, 15 )
		myText11:SetPos( 25.5, 92 )
		myText11.OnEnter = function()
		datastream.StreamToServer( "SetPINOf", { myText11:GetValue(), PersonPINSelect[1] } )
		end

		local button13 = vgui.Create( "DButton", TabB )
		button13:SetSize( 50, 15 )
		button13:SetPos( 75, 92 )
		button13:SetText( "Set PIN" )
		button13.DoClick = function()
			datastream.StreamToServer( "SetPINOf", { myText11:GetValue(), PersonPINSelect[1] } )
		end

		local myText18 = vgui.Create( "DTextEntry", TabB )
		myText18:SetSize( 50, 15 )
		myText18:SetPos( 25.5, 124 )
		myText18.OnEnter = function()
			datastream.StreamToServer( "WithdrawMoneyOf", { tonumber( myText18:GetValue() ), PersonPINSelect[1] } )
		end

		local button9 = vgui.Create( "DButton", TabB )
		button9:SetSize( 50, 15 )
		button9:SetPos( 75, 124 )
		button9:SetText( "Withdraw" )
		button9.DoClick = function()
			datastream.StreamToServer( "WithdrawMoneyOf", { tonumber( myText18:GetValue() ), PersonPINSelect[1] } )
		end

		local myText7 = vgui.Create( "DTextEntry", TabB )
		myText7:SetSize( 50, 15 )
		myText7:SetPos( 25.5, 108 )
		myText7.OnEnter = function()
			datastream.StreamToServer( "DepositMoneyOf", { tonumber( myText7:GetValue() ), PersonPINSelect[1] } )
		end

		local button10 = vgui.Create( "DButton", TabB )
		button10:SetSize( 50, 15 )
		button10:SetPos( 75, 108 )
		button10:SetText( "Deposit" )
		button10.DoClick = function()
			datastream.StreamToServer( "DepositMoneyOf", { tonumber( myText7:GetValue() ), PersonPINSelect[1] } )
		end

		local button4 = vgui.Create( "DButton", TabB )
		button4:SetSize( 100, 15 )
		button4:SetPos( 25.5, 154 )
		button4:SetText( "Reset PIN" )
		button4.DoClick = function()
			datastream.StreamToServer( "ResetPINOf", { PersonPINSelect[1] } )
		end

		local button5 = vgui.Create( "DButton", TabB )
		button5:SetSize( 100, 15 )
		button5:SetPos( 25.5, 139 )
		button5:SetText( "Reset Bank Money" )
		button5.DoClick = function()
			datastream.StreamToServer( "ResetBankedMoneyOf", { PersonPINSelect[1] } )
		end

		local button7 = vgui.Create( "DButton", TabB )
		button7:SetSize( 100, 15 )
		button7:SetPos( 25.5, 54 )
		button7:SetText( "Remove All ATMs" )
		button7.DoClick = function()
			LocalPlayer():ConCommand( "atm_removeall" )
		end

		local button11 = vgui.Create( "DButton", TabB )
		button11:SetSize( 100, 15 )
		button11:SetPos( 25.5, 70 )
		button11:SetText( "Spawn All ATMs" )
		button11.DoClick = function()
			LocalPlayer():ConCommand( "atm_spawnall" )
		end

		local button3 = vgui.Create( "DButton", TabB )
		button3:SetSize( 100, 20 )
		button3:SetPos( 25, 5 )
		button3:SetText( "Print PIN Codes" )
		button3.DoClick = function()
			LocalPlayer():ConCommand( "pincodes_send" )
		end

		local button8 = vgui.Create( "DButton", TabB )
		button8:SetSize( 100, 20 )
		button8:SetPos( 25, 25 )
		button8:SetText( "Print Banked Money" )
		button8.DoClick = function()
			LocalPlayer():ConCommand( "money_send" )
		end
			PropertySheet:AddSheet( "S-Admin", TabB, "gui/silkicons/group", false, false, "ATM SuperAdmin Settings" )
		end
	end
	
	datastream.Hook( "PINAlreadySet", PINAlreadySet )

	local function PINSet( handler, id, encoded, decoded )
		DermaPanel4:SetVisible( false )
		DermaPanel4:Close()
	end
	datastream.Hook( "PINSet", PINSet )

	local function PINAccepted( handler, id, encoded, decoded )

		DermaPanel5:SetVisible( false )
		DermaPanel5:Close()

		--

		DermaPanel = vgui.Create( "DFrame" )
		DermaPanel:SetPos( ScrW() / 2 - 100, ScrH() / 2 - 100 )
		DermaPanel:SetSize( 145, 205 )
		DermaPanel:SetTitle( "ATM" )
		DermaPanel:SetVisible( true )
		DermaPanel:SetDraggable( false )
		DermaPanel:ShowCloseButton( true )
		DermaPanel:MakePopup()

		local DermaPanelList = vgui.Create( "DPanelList", DermaPanel )
		DermaPanelList:SetPos( 0, 22.5 )
		DermaPanelList:SetSize( 145, 205 )

		local DLabel7 = vgui.Create( "DLabel", DermaPanelList )
		DLabel7:SetPos( 14, 5 )
		DLabel7:SetColor( Color( 255, 204, 51, 255 ) )
		DLabel7:SetFont( "TabLarge" )
		DLabel7:SetText( "-BANK ACCOUNT INFO-" )
		DLabel7:SizeToContents()

		local DLabel8 = vgui.Create( "DLabel", DermaPanelList )
		DLabel8:SetPos( 14, 20 )
		DLabel8:SetColor( Color( 51, 204, 255, 255 ) )
		DLabel8:SetFont( "TabLarge" )
		for k, v in pairs( player.GetAll() ) do
			if tostring( v ) == tostring( PersonPIN[1] ) then
				DLabel8:SetText( v:Nick() )
			end
		end
		DLabel8:SizeToContents()

		local DLabel9 = vgui.Create( "DLabel", DermaPanelList )
		DLabel9:SetPos( 14, 35 )
		DLabel9:SetColor( Color( 0, 255, 0, 255 ) )
		DLabel9:SetFont( "TabLarge" )
		DLabel9:SetText( "$" .. CommaTheCash( tonumber( decoded[2] ) ) )
		DLabel9:SizeToContents()

		local DLabel1 = vgui.Create( "DLabel", DermaPanelList )
		DLabel1:SetPos( 50, 50 )
		DLabel1:SetColor( Color( 255, 255, 255, 255 ) )
		DLabel1:SetFont( "TabLarge" )
		DLabel1:SetText( "Deposit" )
		DLabel1:SizeToContents()

		local myText = vgui.Create( "DTextEntry", DermaPanelList )
		myText:SetSize( 85, 20 )
		myText:SetPos( 28, 65 )
		myText:RequestFocus()
		myText.OnEnter = function()
			datastream.StreamToServer( "DepositMoney1", { tonumber( myText:GetValue() ), PersonPIN[1] } )
		end

		local button = vgui.Create( "DButton", DermaPanelList )
		button:SetSize( 50, 20 )
		button:SetPos( 47.5, 90 )
		button:SetText( "Deposit!" )
		button.DoClick = function()
			datastream.StreamToServer( "DepositMoney1", { tonumber( myText:GetValue() ), PersonPIN[1] } )
		end

		--

		local DLabel2 = vgui.Create( "DLabel", DermaPanelList )
		DLabel2:SetPos( 45, 115 )
		DLabel2:SetColor( Color( 255, 255, 255, 255 ) )
		DLabel2:SetFont( "TabLarge" )
		DLabel2:SetText( "Withdraw" )
		DLabel2:SizeToContents()

		local myText2 = vgui.Create( "DTextEntry", DermaPanelList )
		myText2:SetSize( 85, 20 )
		myText2:SetPos( 28, 130 )
		myText2.OnEnter = function()
			datastream.StreamToServer( "Withdraw", { tonumber( myText2:GetValue() ), PersonPIN[1] } )
		end

		local button = vgui.Create( "DButton", DermaPanelList )
		button:SetSize( 55, 20 )
		button:SetPos( 45, 155 )
		button:SetText( "Withdraw!" )
		button.DoClick = function()
			datastream.StreamToServer( "Withdraw", { tonumber( myText2:GetValue() ), PersonPIN[1] } )
		end
	end
	datastream.Hook( "PINAccepted", PINAccepted )

	----

	local function PINAccepted1( handler, id, encoded, decoded )

		DermaPanel:SetVisible( false )
		DermaPanel:Close()

		--

		DermaPanel = vgui.Create( "DFrame" )
		DermaPanel:SetPos( ScrW() / 2 - 100, ScrH() / 2 - 100 )
		DermaPanel:SetSize( 145, 205 )
		DermaPanel:SetTitle( "ATM" )
		DermaPanel:SetVisible( true )
		DermaPanel:SetDraggable( false )
		DermaPanel:ShowCloseButton( true )
		DermaPanel:MakePopup()

		local DermaPanelList = vgui.Create( "DPanelList", DermaPanel )
		DermaPanelList:SetPos( 0, 22.5 )
		DermaPanelList:SetSize( 145, 205 )

		local DLabel7 = vgui.Create( "DLabel", DermaPanelList )
		DLabel7:SetPos( 14, 5 )
		DLabel7:SetColor( Color( 255, 204, 51, 255 ) )
		DLabel7:SetFont( "TabLarge" )
		DLabel7:SetText( "-BANK ACCOUNT INFO-" )
		DLabel7:SizeToContents()

		local DLabel8 = vgui.Create( "DLabel", DermaPanelList )
		DLabel8:SetPos( 14, 20 )
		DLabel8:SetColor( Color( 51, 204, 255, 255 ) )
		DLabel8:SetFont( "TabLarge" )
		for k, v in pairs( player.GetAll() ) do
			if tostring( v ) == tostring( PersonPIN[1] ) then
				DLabel8:SetText( v:Nick() )
			end
		end
		DLabel8:SizeToContents()

		local DLabel9 = vgui.Create( "DLabel", DermaPanelList )
		DLabel9:SetPos( 14, 35 )
		DLabel9:SetColor( Color( 0, 255, 0, 255 ) )
		DLabel9:SetFont( "TabLarge" )
		DLabel9:SetText( "$" .. CommaTheCash( tonumber( decoded[2] ) ) )
		DLabel9:SizeToContents()

		local DLabel1 = vgui.Create( "DLabel", DermaPanelList )
		DLabel1:SetPos( 50, 50 )
		DLabel1:SetColor( Color( 255, 255, 255, 255 ) )
		DLabel1:SetFont( "TabLarge" )
		DLabel1:SetText( "Deposit" )
		DLabel1:SizeToContents()

		local myText = vgui.Create( "DTextEntry", DermaPanelList )
		myText:SetSize( 85, 20 )
		myText:SetPos( 28, 65 )
		if decoded[3] == "d" then
			myText:RequestFocus()
		end
		myText.OnEnter = function()
			datastream.StreamToServer( "DepositMoney1", { tonumber( myText:GetValue() ), PersonPIN[1] } )
		end

		local button = vgui.Create( "DButton", DermaPanelList )
		button:SetSize( 50, 20 )
		button:SetPos( 47.5, 90 )
		button:SetText( "Deposit!" )
		button.DoClick = function()
			datastream.StreamToServer( "DepositMoney1", { tonumber( myText:GetValue() ), PersonPIN[1] } )
		end

		--

		local DLabel2 = vgui.Create( "DLabel", DermaPanelList )
		DLabel2:SetPos( 45, 115 )
		DLabel2:SetColor( Color( 255, 255, 255, 255 ) )
		DLabel2:SetFont( "TabLarge" )
		DLabel2:SetText( "Withdraw" )
		DLabel2:SizeToContents()

		local myText2 = vgui.Create( "DTextEntry", DermaPanelList )
		myText2:SetSize( 85, 20 )
		myText2:SetPos( 28, 130 )
		if decoded[3] == "w" then
			myText2:RequestFocus()
		end
		myText2.OnEnter = function()
			datastream.StreamToServer( "Withdraw", { tonumber( myText2:GetValue() ), PersonPIN[1] } )
		end

		local button = vgui.Create( "DButton", DermaPanelList )
		button:SetSize( 55, 20 )
		button:SetPos( 45, 155 )
		button:SetText( "Withdraw!" )
		button.DoClick = function()
			datastream.StreamToServer( "Withdraw", { tonumber( myText2:GetValue() ), PersonPIN[1] } )
		end
	end
	
	datastream.Hook( "PINAccepted1", PINAccepted1 )

	local function PINSet( handler, id, encoded, decoded )
		DermaPanel4:SetVisible( false )
		DermaPanel4:Close()
	end
	datastream.Hook( "PINSet", PINSet )

	-------

	local function NoPINCode( handler, id, encoded, decoded )
		
		DermaPanel4 = vgui.Create( "DFrame" )
		DermaPanel4:SetPos( ScrW() / 2 - 100, ScrH() / 2 - 100 )
		DermaPanel4:SetSize( 150, 100 )
		DermaPanel4:SetTitle( "ATM PIN SET-UP" )
		DermaPanel4:SetVisible( true )
		DermaPanel4:SetDraggable( false )
		DermaPanel4:ShowCloseButton( true )
		DermaPanel4:MakePopup()

		local DLabel5 = vgui.Create( "DLabel", DermaPanel4 )
		DLabel5:SetPos( 13.5, 25 )
		DLabel5:SetColor( Color( 255, 255, 255, 255 ) )
		DLabel5:SetFont( "TabLarge" )
		DLabel5:SetText( "Please Create a PIN." )
		DLabel5:SizeToContents()

		local DLabel6 = vgui.Create( "DLabel", DermaPanel4 )
		DLabel6:SetPos( 27.5, 39.5 )
		DLabel6:SetColor( Color( 255, 0, 0, 255 ) )
		DLabel6:SetFont( "TabLarge" )
		DLabel6:SetText( "*4 Digits Only!*" )
		DLabel6:SizeToContents()

		local myText5 = vgui.Create( "DTextEntry", DermaPanel4 )
		myText5:SetSize( 33.5, 18 )
		myText5:SetPos( 53.5, 57.5 )
		myText5:RequestFocus()

		local button5 = vgui.Create( "DButton", DermaPanel4 )
		button5:SetSize( 65, 18 )
		button5:SetPos( 37.5, 78.5 )
		button5:SetText( "Set PIN" )
		button5.DoClick = function()
			datastream.StreamToServer( "SetPIN", { myText5:GetValue() } )
		end
		
	end
	datastream.Hook( "NoPIN", NoPINCode )