if ( SERVER ) then
	AddCSLuaFile( "shared.lua" )
	
	util.AddNetworkString("anterg0_guitarTab")
	util.AddNetworkString("anterg0_guitarPlaySong")
end

if CLIENT then
	SWEP.DrawCrosshair = false
	SWEP.PrintName = "Русская гитара"
end

SWEP.Base = "weapon_base"

SWEP.PrintName			= "Русская гитара"
SWEP.Slot						= 2
SWEP.SlotPos				= 4
SWEP.DrawAmmo			= false
SWEP.ViewModel			= "models/weapons/tayley/v_guitar.mdl"
SWEP.HoldType 				= "slam"
SWEP.ViewModelFOV 		= 70
SWEP.ViewModelFlip 		= false
SWEP.WorldModel 			= "models/anterg0_Guitar/marauder_guitar.mdl"
-- Other settings
SWEP.Weight					= 5
SWEP.AutoSwitchTo		= false
SWEP.AutoSwitchFrom	= false
SWEP.Spawnable			= true
SWEP.AdminSpawnable	= true

-- SWEP info
SWEP.Author			= "DuckFlit"
SWEP.Category 		=	"[FT] Атмосфера"
SWEP.Instructions	= "Левый клик дабы играть случайную песню. Правый клик чтобы выбрать песню."

-- Primary fire settings
SWEP.Primary.Damage					= -1
SWEP.Primary.NumShots				= -1
SWEP.Primary.Delay						= 2
SWEP.Primary.ClipSize					= -1
SWEP.Primary.DefaultClip				= 0
SWEP.Primary.Tracer						= -1
SWEP.Primary.Force						= -1
SWEP.Primary.TakeAmmoPerBullet	= false
SWEP.Primary.Automatic				= false
SWEP.Primary.Ammo						= "none"

SWEP.ReloadTimer							= 1

local songsTbl = {
	
        [1] = {"Спецназ - Денис Майданов", "weapons/guitar/ad.mp3"},
        [2] = {"Мужество Ведёт на Небеса", "weapons/guitar/ac.mp3"},
        [3] = {"Витязи", "weapons/guitar/ag.mp3"},
        [4] = {"Чёрный Октябрь ", "weapons/guitar/aj.mp3"},
        [5] = {"За что мы пьём", "weapons/guitar/ak.mp3"},
        [6] = {"Набирает высоту", "weapons/guitar/ar.mp3"},
        [7] = {"Стрим - Песня Бритвы", "weapons/guitar/ay.mp3"},
        [8] = {"Простите меня", "weapons/guitar/am.mp3"},
	    [9] = {"Владимир Высоцкий - Солдаты Группы", "weapons/guitar/ap.mp3"},
		[10] = {"ГСН-Российскийспецназ", "weapons/guitar/fa.mp3"},
		[11] = {"Егор Авдеев - 333 с ночи до зари", "weapons/guitar/fr.mp3"},
		[12] = {"Николай Анисимов - По самому прямому назначению", "weapons/guitar/fy.mp3"},
		[13] = {"UKRAINE - Гімн Українскої Артилерії", "weapons/guitar/jj.mp3"},
}

if CLIENT then

	SWEP.WepSelectIcon = surface.GetTextureID("vgui/selection/guitar_ico")
	function SWEP:DrawWeaponSelection(x, y, wide, tall, alpha)
		x = x + wide / 2
		y = y + tall / 2

		tall = tall * 0.75

		x = x - tall / 2
		y = y - tall / 2 - 10

		surface.SetDrawColor(255, 255, 255, alpha)
		surface.SetTexture(self.WepSelectIcon)

		surface.DrawTexturedRect(x, y, tall, tall)
	end
	
end

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

function SWEP:Holster()
	self.Weapon:EmitSound(Sound("weapons/guitar/squak2.mp3"))
	return true
end

function SWEP:OnRemove()
	self:Holster()
end

function SWEP:PrimaryAttack()
	self:SetNextPrimaryFire(CurTime() + self.Primary.Delay);
	self.Weapon:EmitSound(Sound("weapons/guitar/squak2.mp3"), 50, math.random(85,100), 1, CHAN_SWEP)
	
	self.Weapon:SendWeaponAnim( ACT_VM_PRIMARYATTACK )
	self.Owner:SetAnimation( PLAYER_ATTACK1 )
	if SERVER then
		local songChoice = math.random(1, #songsTbl)
		self.Weapon:SetNWBool("guitar_isPlaying", true)
		self.Weapon:SetNWInt("guitar_songNum", songChoice)
	end
	timer.Simple(0.5, function()
		local song = songsTbl[self.Weapon:GetNWInt("guitar_songNum")][2]
		if(song == nil) then
			song = ""
		end
		self.Weapon:EmitSound(song, 80, 100, 1, CHAN_SWEP)
	end)
end

function SWEP:SecondaryAttack()
	local ply = self.Owner
	if SERVER then
		net.Start("anterg0_guitarTab")
		net.Send(ply)
	end
	return false
end

function SWEP:Think()
end

function SWEP:Reload()
	if(self.Weapon:GetNextSecondaryFire() <= CurTime() ) then
		self.Weapon:SetNextSecondaryFire(CurTime() + self.ReloadTimer)
		self.Weapon:SetNWBool("guitar_isPlaying", false)
		self.Weapon:EmitSound(Sound("weapons/guitar/squak2.mp3"), 50, math.random(85,100), 1, CHAN_SWEP)
		return false
	end
end

function SWEP:Deploy()
	self:SetWeaponHoldType(self.HoldType)
	return true
end

SWEP.RenderGroup = RENDERGROUP_BOTH

function SWEP:DrawWorldModelTranslucent()
	self:DrawModel()
	
	if self.time == nil then self.time = 0 end
	surface.SetFont("Trebuchet24")
	local tag = "Now playing ... " 
	if(self.Weapon:GetNWInt("guitar_songNum") ~= 0) then
		songName = songsTbl[self.Weapon:GetNWInt("guitar_songNum")][1]
		tag = "Now playing ... "  .. songName
	end
	local textwidth = surface.GetTextSize(tag)
	
	if LocalPlayer():GetPos():Distance(self:GetPos()) < 550 then
		if(self.Weapon:GetNWBool("guitar_isPlaying") == false) then return end 
		local alpha = (LocalPlayer():GetPos():Distance(self:GetPos()) / 100.0)
		alpha = math.Clamp(2.5 - alpha, 0 ,1)
		local a = Angle(0,0,0)
		a:RotateAroundAxis(Vector(1,0,0),90)
		a.y = LocalPlayer():GetAngles().y - 90
		cam.Start3D2D(self:GetPos() + Vector(0,0,45 + (math.sin(self.time*0.5)*2)), a , 0.15)
			draw.SimpleText(tag,"Trebuchet24", -textwidth*0.5,0,Color(205,225,255,255*alpha) , 0 , 1)
		cam.End3D2D()
	end
	
	self.time = self.time + FrameTime()
end

net.Receive("anterg0_guitarTab", function(len, ply)
	local songNum = net.ReadFloat()
	
	local wep = ply:GetActiveWeapon()
	if(wep:GetClass() == "guitar") then
		wep:SetNWBool("guitar_isPlaying", true)
		wep:SetNWInt("guitar_songNum", songNum)
		wep:EmitSound( songsTbl[songNum][2], 80, 100, 1, CHAN_SWEP)
		
	end
end)

// DERMA STUFF

if CLIENT then

	local PANEL = {}

	function PANEL:Init()
		local w = ScrW()*0.3
		local h = ScrH()*0.4
		self:SetSize(w, h)
		self:SetPos(ScrW()*0.7, ScrH()/2 - (h/2))
		
		self:MakePopup()
		self:SetDeleteOnClose(true)
		self:SetDraggable(true)
		self:ShowCloseButton(true)
		self:SetTitle("")
		
		self.Paint = function(_, w, h)
			draw.RoundedBox(0, 0, 0, w, h, Color(0, 0, 0, 225))
		end
		
		self.MusicOptions = vgui.Create("anterg0_Guitar:DScroll", self)

	end

	vgui.Register("anterg0_Guitar:Menu", PANEL, "DFrame")

	net.Receive("anterg0_guitarTab", function()
		if(IsValid(guitPanel)) then return end
		guitPanel = vgui.Create("anterg0_Guitar:Menu")
	end)
	
	// MUSIC
	local PANEL = {}
	
	function PANEL:Init()
		local x,y = self:GetParent():GetSize()
		self:SetSize(x, y + 60)
		self:SetPos(0, 30)
		
		--Modify Scroll Bar--
		local sbar = self:GetVBar()
		sbar:SetHideButtons(true)
		sbar.Paint = function(_, w, h)
			draw.RoundedBox(0, 0, 0, (w/2), h, Color(0, 0, 0, 225))
		end
		
		sbar.btnGrip.Paint = function(_, w, h)
			draw.RoundedBox(0, 0, 0, (w/2), h, Color(255, 255, 255, 255))
		end
		
		self.Paint = function(_, w, h)
			draw.RoundedBox(0, 0, 0, w, h, Color(0, 0, 0, 0))
		end
		
		for k, v in ipairs(songsTbl) do
			local btn = self:Add("DButton")
			btn:Dock(TOP)
			btn:DockMargin(10, 0, 10, 10)
			btn:SetSize(0, 25)
			btn:SetText("")
			btn.HoverLerp = 0
			btn.Paint = function(_, w, h)
				if btn:IsHovered() then
					btn.HoverLerp = Lerp( FrameTime() * 10, btn.HoverLerp, 1 )
				else
					btn.HoverLerp = Lerp( FrameTime() * 3, btn.HoverLerp, 0.1)
				end
				draw.RoundedBox(0, 0, 0, w, h, Color(50,50,50,255*btn.HoverLerp))
				draw.SimpleText(v[1], "Trebuchet18", w/2, 2, Color(255,255,255, 255), 1, 0)
			end
			
			btn.PaintOver = function(_, w, h)
				surface.SetDrawColor(Color(255, 255, 255, 125), 1, 1)
				surface.DrawOutlinedRect(0, 0, w, h)
			end
			
			btn.DoClick = function()
				local wep = LocalPlayer():GetActiveWeapon()
				if(wep ~= nil or IsValid(wep)) then
					LocalPlayer():EmitSound(Sound("weapons/guitar/squak2.mp3") )
					wep:EmitSound( v[2], 100, 100, 1, CHAN_SWEP )
				end
				net.Start("anterg0_guitarTab")
					net.WriteFloat(k)
				net.SendToServer()
				
				self:GetParent():Close()
			end

		end
		
		self:Dock(FILL)
		self:DockMargin(0, 0, 0, 10)
	end

	vgui.Register("anterg0_Guitar:DScroll", PANEL, "DScrollPanel")
end
