Mana = 100.0
Think = 0
LastCast = 0
CurrentSpell = "divine bolt"

function onInit( )
end

function Holster()
	_GModText_Hide(Owner, 8988)
end

function onThink( )
	if _CurTime() >= Think+0.35 then
		if Mana < 100 then
			Mana = Mana+3.9
		end
		DrawMana()
		Think = _CurTime()
	end
	if _PlayerIsKeyDown(Owner, IN_ATTACK2) then
		DrawSelectSpells()
		if _PlayerIsKeyDown(Owner, IN_FORWARD) and _PlayerIsKeyDown(Owner, IN_MOVERIGHT) then
			SelectSpell("levitate")
		elseif _PlayerIsKeyDown(Owner, IN_FORWARD) and _PlayerIsKeyDown(Owner, IN_MOVELEFT) then
			SelectSpell("resurrect")
		elseif _PlayerIsKeyDown(Owner, IN_FORWARD) then
			SelectSpell("sun burst")
		elseif _PlayerIsKeyDown(Owner, IN_BACK) and _PlayerIsKeyDown(Owner, IN_MOVERIGHT) then
			SelectSpell("mass heal")
		elseif _PlayerIsKeyDown(Owner, IN_BACK) and _PlayerIsKeyDown(Owner, IN_MOVELEFT) then
			SelectSpell("pixie swarm")
		elseif _PlayerIsKeyDown(Owner, IN_BACK) then
			SelectSpell("sanctuary")
		elseif _PlayerIsKeyDown(Owner, IN_MOVELEFT) then
			SelectSpell("divine bolt")
		elseif _PlayerIsKeyDown(Owner, IN_MOVERIGHT) then
			SelectSpell("toss")
		end 
	else
		_GModRect_Hide(Owner, 8988)
	end
end

function SelectSpell(str)
	CurrentSpell = str
	_PrintMessage(Owner, 4, "Current Spell: "..string.upper(str))
end

function onPrimaryAttack( )
	if _CurTime() < LastCast+1 or _PlayerIsKeyDown(Owner, IN_ATTACK2) then return end

	local ManaCost = 0
	if CurrentSpell == "divine bolt" then ManaCost = 45
	elseif CurrentSpell == "resurrect" then ManaCost = 75
	elseif CurrentSpell == "sun burst" then ManaCost = 70
	elseif CurrentSpell == "mass heal" then ManaCost = 60
	elseif CurrentSpell == "pixie swarm" then ManaCost = 90
	elseif CurrentSpell == "sanctuary" then ManaCost = 90
	elseif CurrentSpell == "levitate" then ManaCost = 40
	elseif CurrentSpell == "toss" then ManaCost = 35 end

	if Mana < ManaCost then
		_PrintMessage(Owner, 4, "Insufficient Mana")
		return
	end
	Mana = Mana - ManaCost
	_RunString("CastSpell3("..Number()..", "..Owner..")")
	LastCast = _CurTime()
end

function Number(str)
	if CurrentSpell == "divine bolt" then return 25
	elseif CurrentSpell == "resurrect" then return 32
	elseif CurrentSpell == "sun burst" then return 31
	elseif CurrentSpell == "mass heal" then return 29
	elseif CurrentSpell == "pixie swarm" then return 27
	elseif CurrentSpell == "sanctuary" then return 26
	elseif CurrentSpell == "levitate" then return 30
	elseif CurrentSpell == "toss" then return 28 end
end

function onSecondaryAttack()
end

function DrawSelectSpells()
_GModRect_Start("divinemage/spellselect")
 _GModRect_SetPos(0.4, 0.75, 0.2, 0.21)
 _GModRect_SetColor(0, 0, 0, 255)
 _GModRect_SetTime(10, 0, 0)
_GModRect_Send(Owner, 8988)
end

function DrawMana()
if Owner <= 0 then return end
_GModRect_Start("gmod/white")
 _GModRect_SetPos(0.41, 0.96, Mana*0.0018, 0.05)
 _GModRect_SetColor( 200, 200, 255, 255 )
 _GModRect_SetTime(9999, 0, 0)
_GModRect_Send(Owner, 1801)
end

function onReload( )
	return false
end

function getWeaponSwapHands()
	return false	
end

function getWeaponFOV() return 75 end
function getWeaponSlot() return 5	end
function getWeaponSlotPos() return 6 end
function getFiresUnderwater() return true	end
function getReloadsSingly() return false end
function getDamage() return 10 end
function getPrimaryShotDelay()	return 0.1 end
function getPrimaryIsAutomatic() return false	end
function getBulletSpread() return vector3( 0.00, 0.00, 0.00 ) end
function getViewKick()	return vector3( 0.0, 0.0, 0.0)	end
function getViewKickRandom() return vector3( 0.0, 0.0, 0.0 ) end
function getNumShotsPrimary() return 1 end
function getPrimaryAmmoType() return "none" end
function getDamageSecondary() return 0 end
function getSecondaryShotDelay() return 1 end
function getSecondaryIsAutomatic()	return false end
function getBulletSpreadSecondary() return vector3( 0.001, 0.001, 0.001 ) end
function getViewKickSecondary() return vector3( 0.5, 0.0, 0.0) end
function getViewKickRandomSecondary() return vector3( 0.0, 0.0, 0.0 ) end
function getNumShotsSecondary() return 1 end
function getSecondaryAmmoType() return "none" end
function getViewModel( ) return "models/weapons/v_stunbaton.mdl" end
function getWorldModel( ) return "models/weapons/w_stunbaton.mdl" end
function getClassName() return "divinemage_wand" end
function getMaxClipPrimary() return 1 end
function getMaxClipSecondary()	return -1 end
function getDefClipPrimary() return 1	end
function getDefClipSecondary()	return 0 end
function getAnimPrefix() return "melee" end
function getPrintName() return "Divine Mage's Wand" end
function getPrimaryScriptOverride() return 3 end
function getSecondaryScriptOverride() return 3 end
function getDeathIcon( ) return "d_magegeneric" end
