include("shared.lua")
include("zrmine/sh/zrmine_config.lua")
SWEP.PrintName = "Pickaxe" -- The name of your SWEP
SWEP.Slot = 1
SWEP.SlotPos = 2
SWEP.DrawAmmo = false
SWEP.DrawCrosshair = true -- Do you want the SWEP to have a crosshair?
--[[---------------------------------------------------------
DRAW HUD
-----------------------------------------------------------]]
local OF_Y = zrmine.config.PickaxeUI_Offset.y
local OF_X = zrmine.config.PickaxeUI_Offset.x
local OreIcon = Material("materials/zerochain/zrms/ui/zrms_ore.png", "smooth")
local ShineIcon = Material("materials/zerochain/zrms/ui/zrms_box_shine.png", "smooth")
local mainOffsetX = -1

function SWEP:DrawResourceItem(Info, color, xpos, ypos)
	local bgColor = Color(255, 255, 255, 50)

	if (zrmine.config.PickaxeThemeLight) then
		bgColor = Color(255, 255, 255, 50)
	else
		bgColor = Color(0, 0, 0, 200)
	end

	draw.RoundedBox(5, (ScrW() / 2.495 + OF_X + xpos) + mainOffsetX, ScrH() / 1.053 + OF_Y + ypos, ScrW() / 26, ScrH() / 20, bgColor)
	surface.SetDrawColor(color)
	surface.SetMaterial(OreIcon)
	surface.DrawTexturedRect((ScrW() / 2.5 + OF_X + xpos) + mainOffsetX, ScrH() / 1.067 + OF_Y + ypos, ScrW() / 25, ScrH() / 13)
	draw.NoTexture()
	draw.DrawText(math.Round(Info, 1), "zrmine_pickaxe_font2", (ScrW() / 2.38 + OF_X + xpos) + mainOffsetX, ScrH() / 1.035 + OF_Y + ypos, Color(255, 255, 255), TEXT_ALIGN_CENTER)
	surface.SetDrawColor(Color(255, 255, 255, 25))
	surface.SetMaterial(ShineIcon)
	surface.DrawTexturedRect((ScrW() / 2.507 + OF_X + xpos) + mainOffsetX, ScrH() / 1.054 + OF_Y + ypos, ScrW() / 23, ScrH() / 18.4)
	draw.NoTexture()
end

local function LerpColor(t, c1, c2)
	local c3 = Color(0, 0, 0)
	c3.r = Lerp(t, c1.r, c2.r)
	c3.g = Lerp(t, c1.g, c2.g)
	c3.b = Lerp(t, c1.b, c2.b)
	c3.a = Lerp(t, c1.a, c2.a)

	return c3
end

function SWEP:Initialize()
	self:SetHoldType(self.HoldType)
end

local xpbar = Material("materials/zerochain/zrms/ui/zrms_xpbar.png", "smooth")
local oldXP = -1
local oldLVL = -1
local UpdateXP_x = 0
local UpdateXpAnimation = true
local diffXP = 0

function SWEP:DrawHUD()
	local bgColor = Color(255, 255, 255, 50)

	if (zrmine.config.PickaxeThemeLight) then
		bgColor = Color(255, 255, 255, 50)
	else
		bgColor = Color(0, 0, 0, 200)
	end

	-- The Inv Cap
	draw.RoundedBox(2.5, ScrW() / 2.495 + OF_X, ScrH() / 1.078 + OF_Y, ScrW() / 5, ScrH() / 75, bgColor)
	local invCap = (5.05 * self:GetOreInv())
	local curAmount = self:GetcIron() + self:GetcBronze() + self:GetcSilver() + self:GetcGold() + self:GetcCoal()

	if (curAmount > self:GetOreInv()) then
		curAmount = self:GetOreInv()
	end

	draw.RoundedBox(2.5, ScrW() / 2.49 + OF_X, ScrH() / 1.077 + OF_Y, ScrW() / invCap * curAmount, ScrH() / 90, Color(121, 111, 92))
	draw.DrawText(math.Round(curAmount, 1) .. zrmine.config.BuyerNPC_Mass, "zrmine_pickaxe_font4", ScrW() / 2 + OF_X, ScrH() / 1.080 + OF_Y, Color(255, 255, 255), TEXT_ALIGN_CENTER)
	-- The XP bar
	local xpInfo
	local plyLvl = self:GetPlayerLVL()
	draw.RoundedBox(2.5, ScrW() / 2.495 + OF_X, ScrH() / 1.097 + OF_Y, ScrW() / 5, ScrH() / 75, bgColor)
	local xpCap = (5.05 * zrmine.config.Pickaxe_Lvl[plyLvl].NextXP)
	local curXP = self:GetPlayerXP()

	if (curXP ~= oldXP or plyLvl ~= oldLVL) then
		self:UpdateSkin()
		oldLVL = plyLvl
	end

	if (curXP > zrmine.config.Pickaxe_Lvl[plyLvl].NextXP) then
		curXP = zrmine.config.Pickaxe_Lvl[plyLvl].NextXP
	end

	if (plyLvl < (table.Count(zrmine.config.Pickaxe_Lvl) - 1)) then
		xpInfo = self:GetPlayerXP() .. " XP"
		draw.RoundedBox(2.5, ScrW() / 2.49 + OF_X, ScrH() / 1.096 + OF_Y, ScrW() / xpCap * curXP, ScrH() / 90, Color(45, 143, 203))
	else
		xpInfo = "Max"
		draw.RoundedBox(2.5, ScrW() / 2.49 + OF_X, ScrH() / 1.096 + OF_Y, ScrW() / 5.05, ScrH() / 90, Color(254, 180, 84))
	end

	surface.SetDrawColor(Color(255, 255, 255, 50))
	surface.SetMaterial(xpbar)
	surface.DrawTexturedRect(ScrW() / 2.49 + OF_X, ScrH() / 1.096 + OF_Y, ScrW() / 5.05, ScrH() / 90)
	draw.NoTexture()
	draw.DrawText(xpInfo, "zrmine_pickaxe_font4", ScrW() / 2 + OF_X, ScrH() / 1.1 + OF_Y, Color(255, 255, 255), TEXT_ALIGN_CENTER)

	-- XP Update Animation
	if (oldXP ~= curXP) then
		diffXP = curXP - oldXP
		oldXP = curXP
		UpdateXpAnimation = true
	end

	if (UpdateXpAnimation) then
		UpdateXP_x = UpdateXP_x + 2
		local progress = 1 / 125 * UpdateXP_x
		local c = Color(255, 255, 255)

		if (diffXP < 0) then
			c = LerpColor(progress, Color(255, 0, 0, 255), Color(255, 0, 0, 0))
			draw.DrawText(diffXP, "zrmine_pickaxe_font2", ScrW() / 2 + UpdateXP_x + OF_X, ScrH() / 1.102 + OF_Y, c, TEXT_ALIGN_CENTER)
		else
			c = LerpColor(progress, Color(0, 255, 0, 255), Color(0, 255, 0, 0))
			draw.DrawText("+" .. diffXP, "zrmine_pickaxe_font2", ScrW() / 2 + UpdateXP_x + OF_X, ScrH() / 1.102 + OF_Y, c, TEXT_ALIGN_CENTER)
		end

		if (UpdateXP_x > 125) then
			UpdateXpAnimation = false
			UpdateXP_x = 0
			UpdateXP_alpha = 255
		end
	end

	self:DrawResourceItem(self:GetcIron(), Color(104, 85, 87), 0, -ScrH() / 175)
	self:DrawResourceItem(self:GetcBronze(), Color(158, 72, 39), ScrW() / 24.7, -ScrH() / 175)
	self:DrawResourceItem(self:GetcSilver(), Color(125, 125, 125), ScrW() / 12.37, -ScrH() / 175)
	self:DrawResourceItem(self:GetcGold(), Color(189, 143, 49), ScrW() / 8.238, -ScrH() / 175)
	self:DrawResourceItem(self:GetcCoal(), Color(85, 85, 85), ScrW() / 6.18, -ScrH() / 175)

	-- [[Instructions]]
	if (self.Instructions and GetConVar("zrms_cl_pickaxe_help"):GetInt() == 1) then
		draw.SimpleTextOutlined(self.Instructions, "zrmine_pickaxe_font3", ScrW() / 2, ScrH() / 45, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_BOTTOM, 2, Color(0, 0, 0, 75))
	end

	-- CoolDown
	draw.RoundedBox(2.5, ScrW() / 2.5 + OF_X, ScrH() / 1.153 + OF_Y, ScrW() / 5, ScrH() / 25, bgColor)
	local cd = self:GetCoolDown() - CurTime()

	if (cd > 0) then
		local barMax = (5.3 * self:GetNextCoolDown())
		draw.RoundedBox(2.5, ScrW() / 2.487 + OF_X, ScrH() / 1.15 + OF_Y, ScrW() / barMax * cd, ScrH() / 28, LerpColor((1 / self:GetNextCoolDown()) * cd, Color(95, 210, 68, 255), Color(210, 68, 68, 255)))
	else
		self:SetCoolDown(-1)
	end

	draw.SimpleTextOutlined(self.PrintName .. " - Level: " .. plyLvl, "zrmine_pickaxe_font1", ScrW() / 2 + OF_X, ScrH() / 1.11 + OF_Y, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_BOTTOM, 2, Color(0, 0, 0, 75))
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 return end
				if (trEnt:GetResourceAmount() <= 0) then return end
				self:SendWeaponAnim(vmAnims[math.random(#vmAnims)])
				self.Owner:SetAnimation(PLAYER_ATTACK1)
			end
		else
			self:SendWeaponAnim(ACT_VM_MISSCENTER)
			self.Owner:SetAnimation(PLAYER_ATTACK1)
		end
	end
end

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

	if (IsValid(trEnt) and (trEnt:GetClass() == "zrms_crusher" or trEnt:GetClass() == "zrms_gravelcrate") and self.Owner:GetPos():Distance(trEnt:GetPos()) <= 100 and (self:GetCoolDown() - CurTime()) < 0) then
		if ((self.lastHit or CurTime()) > CurTime()) then return end
		self.lastHit = CurTime() + 1
		self.Owner:DoAttackEvent()
		self:SendWeaponAnim(ACT_VM_MISSCENTER)
	end
end

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

function SWEP:UpdateSkin()
	local ply = LocalPlayer()
	local vm = ply:GetViewModel()
	local plyLvl = self:GetPlayerLVL()
	local maxLvl = table.Count(zrmine.config.Pickaxe_Lvl)

	if (plyLvl >= math.Round(maxLvl * 0.8)) then
		vm:SetSubMaterial(0, "zerochain/props_mining/pickaxe/zrms_pickaxe_head_skin03")
		vm:SetSubMaterial(1, "zerochain/props_mining/pickaxe/zrms_pickaxe_skin03_diff")
	elseif (plyLvl >= math.Round(maxLvl * 0.3)) then
		vm:SetSubMaterial(0, "zerochain/props_mining/pickaxe/zrms_pickaxe_head_skin02")
		vm:SetSubMaterial(1, "zerochain/props_mining/pickaxe/zrms_pickaxe_skin02_diff")
	else
		vm:SetSubMaterial(0, "zerochain/props_mining/pickaxe/zrms_pickaxe_head_skin01")
		vm:SetSubMaterial(1, "zerochain/props_mining/pickaxe/zrms_pickaxe_diff")
	end
end

function SWEP:Holster()
	self:SendWeaponAnim(ACT_VM_HOLSTER)
	local ply = LocalPlayer()
	local vm = ply:GetViewModel()

	if (IsValid(vm)) then
		vm:SetSubMaterial(0, nil)
		vm:SetSubMaterial(1, nil)
	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
end
