if not ATTACHMENT then
	ATTACHMENT = {}
end

ATTACHMENT.Name = "RT Scope Base"
ATTACHMENT.Description = {}

ATTACHMENT.WeaponTable = {
	["RTDrawEnabled"] = true,

	-- ["RTScopeFOV"] = 90 / 1 / 2, -- Default FOV / Scope Zoom / screenscale
	-- ["RTScopeAttachment"] = -1,

	-- ["RTReticleMaterial"] = Material("scope/gdcw_acog"),
	-- ["RTReticleColor"] = color_white,
	-- ["RTReticleScale"] = 1,

	-- ["RTShadowMaterial"] = Material("vgui/scope_shadowmask_test"),
	-- ["RTShadowColor"] = color_white,
	-- ["RTShadowScale"] = 2,
}

local cd = {}

local fallbackReticle = Material("scope/gdcw_scopesightonly")
local fallbackShadow = Material("vgui/scope_shadowmask_test")

local flipcv = GetConVar("cl_tfa_viewmodel_flip")

function ATTACHMENT:RTCode(wep, rt, scrw, scrh)
	if not wep.OwnerIsValid or not wep:VMIV() then return end

	local rtw, rth = rt:Width(), rt:Height()

	-- clearing view
	render.OverrideAlphaWriteEnable(true, true)
	surface.SetDrawColor(color_white)
	surface.DrawRect(-rtw, -rth, rtw * 2, rth * 2)

	local vm = wep.OwnerViewModel

	local ang = vm:GetAngles()

	local isang = wep:GetStatL("IronSightsAngle") * wep:GetIronSightsProgress()

	ang:RotateAroundAxis(ang:Forward(), -isang.z)
	ang:RotateAroundAxis(ang:Right(), -isang.x)
	ang:RotateAroundAxis(ang:Up(), -isang.y)

	ang:RotateAroundAxis(ang:Forward(), isang.z)

	local scopeAtt = wep:GetStatL("RTScopeAttachment", -1)

	if scopeAtt > 0 then
		local AngPos = vm:GetAttachment(scopeAtt)

		if AngPos then
			ang = AngPos.Ang

			if flipcv:GetBool() then
				ang.y = -ang.y
			end
		end
	end

	cd.angles = ang
	cd.origin = wep:GetOwner():GetShootPos()
	cd.x = 0
	cd.y = 0
	cd.w = rtw
	cd.h = rth
	cd.fov = wep:GetStatL("RTScopeFOV", 90 / wep:GetStatL("ScopeZoom", 1) / 2)
	cd.drawviewmodel = false
	cd.drawhud = false

	-- main RT render view
	render.Clear(0, 0, 0, 255, true, true)
	render.SetScissorRect(0, 0, rtw, rth, true)

	if wep:GetIronSightsProgress() > 0.005 then
		render.RenderView(cd)
	end

	render.SetScissorRect(0, 0, rtw, rth, false)
	render.OverrideAlphaWriteEnable(false, true)

	cam.Start2D()

	-- ADS transition darkening
	draw.NoTexture()
	surface.SetDrawColor(ColorAlpha(color_black, 255 * (1 - wep:GetIronSightsProgress())))
	surface.DrawRect(0, 0, rtw, rth)

	surface.SetMaterial(wep:GetStatL("RTReticleMaterial", fallbackReticle))
	surface.SetDrawColor(wep:GetStatL("RTReticleColor", color_white))
	local retScale = wep:GetStatL("RTReticleScale", 1)
	surface.DrawTexturedRect(rtw / 2 - rtw * retScale / 2, rth / 2 - rth * retScale / 2, rtw * retScale, rth * retScale)

	surface.SetMaterial(wep:GetStatL("RTShadowMaterial", fallbackShadow))
	surface.SetDrawColor(wep:GetStatL("RTShadowColor", color_white))
	local shadScale = wep:GetStatL("RTShadowScale", 2)
	surface.DrawTexturedRect(rtw / 2 - rtw * shadScale / 2, rth / 2 - rth * shadScale / 2, rtw * shadScale, rth * shadScale)

	cam.End2D()
end

if not TFA_ATTACHMENT_ISUPDATING then
	TFAUpdateAttachments()
end
