AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")
include("zrmine/sh/zrmine_config.lua")
SWEP.Weight = 5

--SWEP:Initialize\\
--Tells the script what to do when the player "Initializes" the SWEP.
function SWEP:Initialize()
	self:SetHoldType(self.HoldType)
	self.HitCounter = 0
	self.NextXP = math.random(zrmine.config.Pickaxe_MinNextXP, zrmine.config.Pickaxe_MaxNextXP)
end

function SWEP:Deploy()
	self:SendWeaponAnim(ACT_VM_DRAW)
end

function SWEP:CreateDecal(tr)
	local Pos1 = tr.HitPos + tr.HitNormal
	local Pos2 = tr.HitPos - tr.HitNormal
	util.Decal("Dark", Pos1, Pos2)
end

function SWEP:HasSpaceInINV()
	local amount = self:GetcIron() + self:GetcBronze() + self:GetcSilver() + self:GetcGold() + self:GetcCoal()

	if (amount < self:GetOreInv()) then
		return true
	else
		return false
	end
end

local vmAnims = {ACT_VM_HITCENTER, ACT_VM_HITKILL}

function SWEP:PrimaryAttack()
	local tr = self.Owner:GetEyeTrace()
	local trEnt = tr.Entity

	if ((self:GetCoolDown() - CurTime()) < 0) then
		if (IsValid(trEnt) and trEnt:GetClass() == "zrms_ore") then
			if (self.Owner:GetPos():Distance(trEnt:GetPos()) <= 100) then
				if (not self:HasSpaceInINV()) then
					if ((self.lastHit or CurTime()) > CurTime()) then return end
					self.lastHit = CurTime() + 1
					zrmine.f.Notify(self.Owner, zrmine.language.Pickaxe_InvFull, 1)

					return
				end

				if (trEnt:HasResource() == false) then return end
				local harvestType = trEnt:GetResourceType()
				local harvestMul = zrmine.config.Pickaxe_HarvestMul[harvestType]
				local harvestamount = self:GetHarvestAmount() * harvestMul.Amount
				local harvestedResourceType = trEnt:Harvest(self.Owner, harvestamount)

				for k, v in pairs(harvestedResourceType) do
					if (k == "Iron") then
						self:SetcIron(self:GetcIron() + v)
					elseif (k == "Bronze") then
						self:SetcBronze(self:GetcBronze() + v)
					elseif (k == "Silver") then
						self:SetcSilver(self:GetcSilver() + v)
					elseif (k == "Gold") then
						self:SetcGold(self:GetcGold() + v)
					elseif (k == "Coal") then
						self:SetcCoal(self:GetcCoal() + v)
					end
				end

				local harvestIntervalMul = self:GetHarvestInterval() * harvestMul.Speed
				self:SetNextCoolDown(harvestIntervalMul)
				self:SetCoolDown(CurTime() + self:GetNextCoolDown())

				-- XP Stuff
				if (self.Owner.zrms.lvl < (table.Count(zrmine.config.Pickaxe_Lvl) - 1)) then
					self.HitCounter = self.HitCounter + 1

					if (self.HitCounter > self.NextXP) then
						self.HitCounter = 0
						zrmine.lvlsys.AddXP(self.Owner, 1 * harvestMul.XP)
						self.NextXP = math.random(zrmine.config.Pickaxe_MinNextXP, zrmine.config.Pickaxe_MaxNextXP)
						self:UpdateLvlVar()
					end
				end

				--Vrondakis
				if (LevelSystemConfiguration or manolis) then
					self.Owner:addXP(zrmine.config.Vrondakis["Mining_pickaxe"].XP, " ", true)
				end

				if (harvestedResourceType[1] ~= "Nothing") then
					zrmine.f.CreateEffectTable("pickaxe_hit01", "zrmine_sfx_pickaxeHit", trEnt, tr.HitNormal:Angle(), tr.HitPos)
				end

				self:SendWeaponAnim(vmAnims[math.random(#vmAnims)])
				self.Owner:SetAnimation(PLAYER_ATTACK1)
				self.Owner:ViewPunch(Angle(-1, 0, 0))
			end
		else
			self:SetNextCoolDown(1)
			self:SetCoolDown(CurTime() + self:GetNextCoolDown())
			self:SendWeaponAnim(ACT_VM_MISSCENTER)
			self.Owner:SetAnimation(PLAYER_ATTACK1)
			self.Owner:ViewPunch(Angle(-1, 0, 0))
		end
	end
end

function SWEP:ResourceCap(amount, cap)
	if (amount > cap) then
		amount = cap
	end

	return amount
end

function SWEP:SecondaryAttack()
	local tr = self.Owner:GetEyeTrace()
	local trEnt = tr.Entity

	if (self.Owner:GetPos():Distance(trEnt:GetPos()) <= 100 and IsValid(trEnt) and (trEnt:GetClass() == "zrms_crusher" or trEnt:GetClass() == "zrms_gravelcrate")) then
		if (trEnt:GetClass() == "zrms_crusher" and trEnt:Return_StoredAmount() >= zrmine.config.Crusher_Capacity) then
			zrmine.f.Notify(self.Owner, zrmine.language.Crusher_Full, 1)

			return
		end

		if (trEnt:GetClass() == "zrms_gravelcrate" and trEnt:Return_StoredAmount() >= zrmine.config.GravelCrates_Capacity) then
			zrmine.f.Notify(self.Owner, zrmine.language.Crate_Full, 1)

			return
		end

		local rTable = {}
		rTable["Iron"] = self:GetcIron() or 0
		rTable["Bronze"] = self:GetcBronze() or 0
		rTable["Silver"] = self:GetcSilver() or 0
		rTable["Gold"] = self:GetcGold() or 0
		rTable["Coal"] = self:GetcCoal() or 0
		local iron = 0
		local bronze = 0
		local silver = 0
		local gold = 0
		local coal = 0

		for k, v in pairs(rTable) do
			if (v > 0) then
				local needAmount

				if (trEnt:GetClass() == "zrms_crusher") then
					needAmount = zrmine.config.Crusher_Capacity - trEnt:Return_StoredAmount()
				elseif (trEnt:GetClass() == "zrms_gravelcrate") then
					needAmount = zrmine.config.GravelCrates_Capacity - trEnt:Return_StoredAmount()
				end

				local capAmount = self:CalcNeedAmount(v, needAmount)

				if (k == "Iron") then
					iron = capAmount
					self:SetcIron(self:GetcIron() - iron)
				elseif (k == "Bronze") then
					bronze = capAmount
					self:SetcBronze(self:GetcBronze() - bronze)
				elseif (k == "Silver") then
					silver = capAmount
					self:SetcSilver(self:GetcSilver() - silver)
				elseif (k == "Gold") then
					gold = capAmount
					self:SetcGold(self:GetcGold() - gold)
				elseif (k == "Coal") then
					coal = capAmount
					self:SetcCoal(self:GetcCoal() - coal)
				end

				break
			end
		end

		local gTable = {}
		gTable["Iron"] = iron
		gTable["Bronze"] = bronze
		gTable["Silver"] = silver
		gTable["Gold"] = gold
		gTable["Coal"] = coal
		local amount = iron + bronze + silver + gold + coal

		if (amount > 0) then
			if (trEnt:GetClass() == "zrms_crusher") then
				trEnt:Add_Resource(gTable, self.Owner)
			elseif (trEnt:GetClass() == "zrms_gravelcrate") then
				trEnt:Add_Resource(gTable, self.Owner)
			end

			zrmine.f.CreateEffectTable(nil, "zrmine_sfx_pickaxeHit", trEnt, tr.HitNormal:Angle(), tr.HitPos)
		end
	end
end

function SWEP:CalcNeedAmount(hAmount, nAmount)
	local famount

	if (hAmount > nAmount) then
		famount = self:ResourceCap(nAmount, self:GetFillCap())
	else
		famount = self:ResourceCap(hAmount, self:GetFillCap())
	end

	return famount
end

-- Updates our Level Vars
function SWEP:UpdateLvlVar()
	local ply = self.Owner
	local plyLvl = zrmine.lvlsys.ReturnLvl(ply)
	self:SetPlayerXP(zrmine.lvlsys.HasXP(ply))
	self:SetPlayerLVL(plyLvl)
	self:SetHarvestAmount(zrmine.config.Pickaxe_Lvl[plyLvl].HarvestAmount)
	self:SetHarvestInterval(zrmine.config.Pickaxe_Lvl[plyLvl].HarvestInterval)
	self:SetOreInv(zrmine.config.Pickaxe_Lvl[plyLvl].OreInv)
	self:SetFillCap(zrmine.config.Pickaxe_Lvl[plyLvl].FillCap)
	local maxLvl = table.Count(zrmine.config.Pickaxe_Lvl)

	if (plyLvl >= math.Round(maxLvl * 0.8)) then
		self:SetSkin(2)
	elseif (plyLvl >= math.Round(maxLvl * 0.3)) then
		self:SetSkin(1)
	else
		self:SetSkin(0)
	end
end

--Tells the script what to do when the player "Initializes" the SWEP.
	function SWEP:Equip()
		self:SendWeaponAnim(ACT_VM_DRAW) -- View model animation
		self.Owner:SetAnimation(PLAYER_IDLE) -- 3rd Person Animation
		self:UpdateLvlVar()
	end

	function SWEP:Reload()
	end

	function SWEP:ShouldDropOnDie()
		return false
	end
