--by Cute<3 cute|studio
--vk.com/offcute

--адаптация под все экраны
local sx,sy = guiGetScreenSize()
local x,y = (sx/1920), (sy/1080)

--шрифт
local exofont = dxCreateFont("assets/font/Exo2.ttf", 28*x)
local exofont_big = dxCreateFont("assets/font/Exo2.ttf", 38*x)


--проверка на позицию курсора
function isCursorOverRectangle(x,y,w,h)
	if isCursorShowing() then
		local mx,my = getCursorPosition() -- relative
		local cursorx,cursory = mx*sx,my*sy
		if cursorx > x and cursorx < x + w and cursory > y and cursory < y + h then
			return true
		end
	end
return false
end

--настройки, остальные в shared.lua
local job_pickup_start = {
-- пикап устройства на работу
{2526.614,-1294.286,1048.5},
}

--переменные
button_text1 = "NaN"

show_jobDx = false
show_jobSelectDx = false
error_alpha = 0
need_lvl = 0

function drawJobDx()
    if show_jobDx == true then
	    local job_level = getElementData(localPlayer,"job_weapon:level") or 1
		local job_exp = getElementData(localPlayer,"job_weapon:exp") or 0
		local job_money = getElementData(localPlayer,"job_weapon:money") or 0
	    dxDrawImage(0*x, 0*y, 1920*x, 1080*y, "assets/bgmain.png", 0,0,0, tocolor(255,255,255,255))
		dxDrawText(""..job_level.."("..job_exp.."/"..max_exp.."exp.)", 440*x, 925*y, 1920*x, 25*y, tocolor(255,255,255,255), 1, exofont, "left", "center", false, false, true, true, false)
		
		dxDrawText(""..button_text1, (-1270)*x, 1870*y, 1920*x, 25*y, tocolor(100,100,100,255), 1, exofont, "center", "center", false, false, true, true, false)
		dxDrawText("Выход", (-200)*x, 1870*y, 1920*x, 25*y, tocolor(100,100,100,255), 1, exofont, "center", "center", false, false, true, true, false)
		
		if isCursorOverRectangle(108*x, 910*y, 450*x, 85*y) then
			if getKeyState("mouse1") then
				if not click then
					click = true
					show_jobDx = false
					showChat(true)
					--exports.Hud:openHud()
        			showCursor(false)
        			local state = getElementData(localPlayer,"job_weapon:onwork")
        			local a1 = getElementData(localPlayer,"job_weapon:markerstart")
        			if not state then
            			setElementData(localPlayer,"job_weapon:onwork",0)
            			setElementData(localPlayer,"job_weapon:working",a1)
            			setElementData(localPlayer,"kolvo_xodok",0)
            			createRes(localPlayer)
            			outputChatBox("Вы начали работу.",255,255,255,true)
						outputChatBox("Спускайтесь. Идите вниз за ресурсами.",255,255,255,true)
            			changeWalkSettings(false)
						setElementData(localPlayer,"job_weapon:money",nil)
        			else
					    triggerServerEvent("giveMoneyWeaponjob",localPlayer,localPlayer,job_money)
						triggerServerEvent("weaponTakePlayer", localPlayer, localPlayer)
					    deleteRes(localPlayer)
            			deleteVerstaks(localPlayer)
						deleteSklad(localPlayer)
            			setElementData(localPlayer,"job_weapon:onwork",nil)
            			setElementData(localPlayer,"job_weapon:working",nil)
						setElementData(localPlayer,"kolvo_xodok",nil)
            			changeWalkSettings(true)
        			end
				end
			else
				click = false
			end
		end	
		
		if isCursorOverRectangle(633*x, 910*y, 450*x, 85*y) then
			if getKeyState("mouse1") then
				if not click then
					click = true
					show_jobDx = false
					showChat(true)
					--exports.Hud:openHud()
        			showCursor(false)
				end
			else
				click = false
			end
		end	
	end
	
	if show_jobSelectDx == true then
	    local job_level = getElementData(localPlayer,"job_weapon:level") or 1
		local job_exp = getElementData(localPlayer,"job_weapon:exp") or 0
		local job_money = getElementData(localPlayer,"job_weapon:money") or 0
		dxDrawImage(0*x, 0*y, 1920*x, 1080*y, "assets/bgselect.png", 0,0,0, tocolor(255,255,255,255))
		
		dxDrawText("Для этого оружия требуется уровень "..need_lvl.." у вас "..job_level.." уровень.", 100*x, 130*y, 1880*x, 100*y, tocolor(255,0,0,error_alpha), 1, exofont_big, "right", "center", false, false, true, true, false)
		
		if isCursorOverRectangle(108*x, 310*y, 450*x, 312*y) then -- пистолет
			if getKeyState("mouse1") then
				if not click then
					click = true
					show_jobSelectDx = false
					showChat(true)
					--exports.Hud:openHud()
					triggerServerEvent("sborkaPistol", localPlayer, localPlayer)
					outputChatBox("Вы начали сборку пистолета.",255,255,255,true)
        			showCursor(false)
					setElementFrozen(localPlayer,true)
					setTimer(function()
                        setElementFrozen(localPlayer,false)
                        deleteVerstaks(localPlayer)
						createSklad(localPlayer)
                        outputChatBox("Вы успешно собрали пистолет.\nОтнесите его на склад.",255,255,255,true)
						setElementData(localPlayer,"job_weapon:exp",job_exp+1)
						setElementData(localPlayer,"job_weapon:money",job_money+pistol_cost)
						if job_exp >= max_exp then 
						    setElementData(localPlayer,"job_weapon:level",job_level+1)
						    setElementData(localPlayer,"job_weapon:exp",0)
						end
                    end,pistol_time,1)
				end
			else
				click = false
			end
		end	
		
		if isCursorOverRectangle(633*x, 310*y, 450*x, 312*y) then -- пп
			if getKeyState("mouse1") then
				if not click then
					click = true
					need_lvl = 2
					if job_level >= 2 then
					    show_jobSelectDx = false
					    showChat(true)
					    --exports.Hud:openHud()
					    triggerServerEvent("sborkaPP", localPlayer, localPlayer)
					    outputChatBox("Вы начали сборку пистолета-пулемета.",255,255,255,true)
        			    showCursor(false)
					    setElementFrozen(localPlayer,true)
					    setTimer(function()
                            setElementFrozen(localPlayer,false)
                            deleteVerstaks(localPlayer)
							createSklad(localPlayer)
                            outputChatBox("Вы успешно собрали пистолет-пулемет.\nОтнесите его на склад.",255,255,255,true)
							setElementData(localPlayer,"job_weapon:exp",job_exp+2)
							if job_exp >= max_exp then 
						    	setElementData(localPlayer,"job_weapon:level",job_level+2)
						    	setElementData(localPlayer,"job_weapon:exp",0)
							end
							setElementData(localPlayer,"job_weapon:money",job_money+autopistol_cost)
                        end,autopistol_time,1)
			        else
					    error_alpha = 255
						setTimer(function()
						    error_alpha = 0
						end,3000,1)
			        end
				end
			else
				click = false
			end
		end	
		
		if isCursorOverRectangle(108*x, 675*y, 450*x, 312*y) then -- автомат 
			if getKeyState("mouse1") then
				if not click then
					click = true
					need_lvl = 3
					if job_level >= 3 then
					    show_jobSelectDx = false
					    showChat(true)
					    --exports.Hud:openHud()
					    triggerServerEvent("sborkaAutomat", localPlayer, localPlayer)
					    outputChatBox("Вы начали сборку автомата.",255,255,255,true)
        			    showCursor(false)
					    setElementFrozen(localPlayer,true)
					    setTimer(function()
                            setElementFrozen(localPlayer,false)
                            deleteVerstaks(localPlayer)
							createSklad(localPlayer)
                            outputChatBox("Вы успешно собрали автомат.\nОтнесите его на склад.",255,255,255,true)
							setElementData(localPlayer,"job_weapon:exp",job_exp+3)
							if job_exp >= max_exp then 
						    	setElementData(localPlayer,"job_weapon:level",job_level+3)
						    	setElementData(localPlayer,"job_weapon:exp",0)
							end
							setElementData(localPlayer,"job_weapon:money",job_money+auto_cost)
                        end,auto_time,1)
					else
					    error_alpha = 255
						setTimer(function()
						    error_alpha = 0
						end,3000,1)
			        end
				end
			else
				click = false
			end
		end	
		
		if isCursorOverRectangle(633*x, 675*y, 450*x, 312*y) then -- винтовка
			if getKeyState("mouse1") then
				if not click then
					click = true
					need_lvl = 4
					if job_level >= 4 then
					    show_jobSelectDx = false
					    showChat(true)
					    --exports.Hud:openHud()
					    triggerServerEvent("sborkaSniper", localPlayer, localPlayer)
					    outputChatBox("Вы начали сборку винтовки.",255,255,255,true)
        			    showCursor(false)
					    setElementFrozen(localPlayer,true)
					    setTimer(function()
                            setElementFrozen(localPlayer,false)
                            deleteVerstaks(localPlayer)
                            outputChatBox("Вы успешно собрали винтовку.\nОтнесите его на склад.",255,255,255,true)
							setElementData(localPlayer,"job_weapon:exp",job_exp+4)
							if job_exp >= max_exp then 
						    	setElementData(localPlayer,"job_weapon:level",job_level+4)
						    	setElementData(localPlayer,"job_weapon:exp",0)
							end
							setElementData(localPlayer,"job_weapon:money",job_money+sniper_cost)
                        end,sniper_time,1)
					else
					    error_alpha = 255
						setTimer(function()
						    error_alpha = 0
						end,3000,1)
			        end
				end
			else
				click = false
			end
		end	
	end
end
addEventHandler("onClientRender", root, drawJobDx)

for a=1,#job_pickup_start do
    local x,y,z = job_pickup_start[a][1],job_pickup_start[a][2],job_pickup_start[a][3]
    local marker = createPickup(x, y, z, 3, 1275)
    setElementData(marker,"job_weapon:markerstart",a)
	setElementDimension(marker, 1)
    setElementInterior(marker, 2)
end

function createRes(player)
    if player == getLocalPlayer() then
        local marker1 = createMarker(2559.152,-1287.208,1044-0.85, "cylinder", 2, 255, 255, 0, 50)
		local marker2 = createMarker(2543.124,-1287.208,1044-0.85, "cylinder", 2, 255, 255, 0, 50)
		local marker3 = createMarker(2551.057,-1287.208,1044-0.85, "cylinder", 2, 255, 255, 0, 50)
	    setElementDimension(marker1, 1)
        setElementInterior(marker1, 2)
	    setElementData(marker1,"job_weapon:markerres",true)
		setElementData(player,"job_weapon:markerres1",marker1)
	    setElementDimension(marker2, 1)
        setElementInterior(marker2, 2)
	    setElementData(marker2,"job_weapon:markerres",true)
		setElementData(player,"job_weapon:markerres2",marker2)		
	    setElementDimension(marker3, 1)
        setElementInterior(marker3, 2)
	    setElementData(marker3,"job_weapon:markerres",true)
		setElementData(player,"job_weapon:markerres3",marker3)	

        local marker4 = createMarker(2559.152,-1300,1044-0.85, "cylinder", 2, 255, 255, 0, 50)
		local marker5 = createMarker(2543.124,-1300,1044-0.85, "cylinder", 2, 255, 255, 0, 50)
		local marker6 = createMarker(2551.057,-1300,1044-0.85, "cylinder", 2, 255, 255, 0, 50)	
        setElementDimension(marker4, 1)
        setElementInterior(marker4, 2)
	    setElementData(marker4,"job_weapon:markerres",true)
		setElementData(player,"job_weapon:markerres4",marker4)
	    setElementDimension(marker5, 1)
        setElementInterior(marker5, 2)
	    setElementData(marker5,"job_weapon:markerres",true)
		setElementData(player,"job_weapon:markerres5",marker5)		
	    setElementDimension(marker6, 1)
        setElementInterior(marker6, 2)
	    setElementData(marker6,"job_weapon:markerres",true)
		setElementData(player,"job_weapon:markerres6",marker6)			
    end
end

function deleteRes(player)
    if player == getLocalPlayer() then
        local marker1 = getElementData(player,"job_weapon:markerres1")
		local marker2 = getElementData(player,"job_weapon:markerres2")
		local marker3 = getElementData(player,"job_weapon:markerres3")
		local marker4 = getElementData(player,"job_weapon:markerres4")
		local marker5 = getElementData(player,"job_weapon:markerres5")
		local marker6 = getElementData(player,"job_weapon:markerres6")
        if marker1 then
            if isElement(marker1) then destroyElement(marker1) end
        end
		if marker2 then
            if isElement(marker2) then destroyElement(marker2) end
        end
		if marker3 then
            if isElement(marker3) then destroyElement(marker3) end
        end
		if marker4 then
            if isElement(marker4) then destroyElement(marker4) end
        end
		if marker5 then
            if isElement(marker5) then destroyElement(marker5) end
        end
		if marker6 then
            if isElement(marker6) then destroyElement(marker6) end
        end
    end
end

function createVerstaks(player)
    if player == getLocalPlayer() then
	    local verstak1 = createMarker(2556.128,-1293.251,1044-0.85, "cylinder", 4, 0, 255, 0, 0)
		setElementDimension(verstak1, 1)
        setElementInterior(verstak1, 2)
		setElementData(verstak1,"job_weapon:verstak",true)
		setElementData(player,"job_weapon:verstak1",verstak1)
		
		local verstak2 = createMarker(2543.128,-1293.251,1044-0.85, "cylinder", 3, 0, 255, 0, 0)
		setElementDimension(verstak2, 1)
        setElementInterior(verstak2, 2)
		setElementData(verstak2,"job_weapon:verstak",true)
		setElementData(player,"job_weapon:verstak2",verstak2)
	end
end

function deleteVerstaks(player)
    if player == getLocalPlayer() then
        local verstak1 = getElementData(player,"job_weapon:verstak1")
		local verstak2 = getElementData(player,"job_weapon:verstak2")
        if verstak1 then
            if isElement(verstak1) then destroyElement(verstak1) end
        end
		if verstak2 then
            if isElement(verstak2) then destroyElement(verstak2) end
        end
    end
end

function createSklad(player)
    if player == getLocalPlayer() then
	    local sklad = createMarker(job_marker_end[1],job_marker_end[2],job_marker_end[3]-0.85, "cylinder", 3, 255, 0, 0, 100)
		setElementDimension(sklad, 1)
        setElementInterior(sklad, 2)
		setElementData(sklad,"job_weapon:sklad",true)
		setElementData(player,"job_weapon:sklad",sklad)
	end
end

function deleteSklad(player)
    if player == getLocalPlayer() then
        local sklad = getElementData(player,"job_weapon:sklad")
        if sklad then
            if isElement(sklad) then destroyElement(sklad) end
        end
    end
end

function onClientPickupHit(player)
    if player == getLocalPlayer() then
        local veh = getPedOccupiedVehicle(player)
        local state = getElementData(player,"job_weapon:onwork")
        local a1 = getElementData(source,"job_weapon:markerstart")
        if a1 then
            if not veh then
			    --if getElementData(localPlayer, "Должность") == "Лесоруб" then
				    show_jobDx = true
					showChat(false)
		            --exports.Hud:closeHud()
                    showCursor(true)
                    setElementData(player,"job_weapon:markerstart",a1)
                    if not state then
                        button_text1 = "Начать"
                    else
                        button_text1 = "Завершить"
                    end
				--[[else
				    exports["DXchat"]:outputContextBar("#FF0000[Ошибка]: #FFFFFFВы не работаете здесь!")
				end]]--
            end
        end
    end
end
addEventHandler("onClientPickupHit", getRootElement(), onClientPickupHit)

function onClientMarkerHit(player, mdim )
    if player == getLocalPlayer() then
        if mdim then
            local veh = getPedOccupiedVehicle(player)
            local state = getElementData(player,"job_weapon:onwork")
			if getElementData(source,"job_weapon:markerres") then
			    if not veh then
                    setElementFrozen(player,true)
                    triggerServerEvent("playAnim_Resources", player, player)
                        setTimer(function()
                            if isElement(player) and getElementData(player,"job_weapon:onwork") then
                                deleteRes(player)
                                setElementFrozen(player,false)
                                createVerstaks(player)
                                setElementData(player,"kolvo_xodok",tonumber(getElementData(player,"kolvo_xodok")) + 1)
                                outputChatBox("Вы успешно взяли ресурсы для изготовления оружия.\nПодходите к рабочему столу.",255,255,255,true)
                            end
                        end,3000,1)
                end
			elseif getElementData(source,"job_weapon:verstak") then
			    if not veh then
					show_jobSelectDx = true
					showChat(false)
					--exports.Hud:closeHud()
        			showCursor(true)
				end
            elseif getElementData(source,"job_weapon:sklad") then
			    if not veh then
				    setElementFrozen(player,true)
                    triggerServerEvent("playAnim_Sklad", player, player)
					setTimer(function()
					    deleteSklad(player)
					    createRes(player)
						setElementFrozen(player,false)
					    outputChatBox("Вы положили оружие на склад.\nИдите за ресурсами.",255,255,255,true)
						triggerServerEvent("weaponTakePlayer", player, player)
					end,3000,1)
				end
            end
        end
    end
end
addEventHandler("onClientMarkerHit", getRootElement(), onClientMarkerHit )

function changeWalkSettings (arg)
    toggleControl("sprint", arg)
    toggleControl("jump", arg)
    toggleControl("aim_weapon", arg)
    toggleControl("next_weapon", arg)
    toggleControl("previous_weapon", arg)
    toggleControl("fire", arg)
    toggleControl("enter_exit", arg)
end 

function job_wasted()
    local player = source
	    if player == getLocalPlayer() then
        local state = getElementData(player,"job_weapon:onwork")
        if state then
            local job_money = getElementData(localPlayer,"job_weapon:money") or 0
            deleteRes(localPlayer)
            deleteVerstaks(localPlayer)
			deleteSklad(localPlayer)
            triggerServerEvent("giveMoneyWeaponjob",player,player,job_money)
			triggerServerEvent("weaponTakePlayer", player, player)
			outputChatBox("Работа завершена.",255,255,255,true)
            outputChatBox("Заработанно: #7979dc"..job_money.." #FFFFFFруб.",255,255,255,true)
			setElementData(localPlayer,"job_weapon:onwork",nil)
            setElementData(localPlayer,"job_weapon:working",nil)
			setElementData(localPlayer,"job_weapon:money",nil)
			setElementData(localPlayer,"kolvo_xodok",nil)
            changeWalkSettings(true)
        end
	end
end
addEventHandler("onClientPlayerWasted", root, job_wasted)