__name__ = 'KILLFX'
__version__ = '1.0.0'
__author__ = 'Softik Inquisitor'

local inicfg = require 'inicfg'
local imgui = require 'imgui'
local sampev = require "samp.events"

local soundsDir = 'moonloader/resource/'
local soundFile = 'kill.mp3'
local settingsFile = 'killfx.ini'
local onSpectate = false
local spectateId = 0

local settings = inicfg.load({
    main = {
        enable = true,
        onlyDefaultWorld = true
    },
    volume = {
        kill = 0.8
    }
}, settingsFile)
if not doesFileExist('moonloader/config/'..settingsFile) then inicfg.save(settings, settingsFile) end

local enableSound = imgui.ImBool(settings.main.enable)
local onlyDefaultWorld = imgui.ImBool(settings.main.onlyDefaultWorld)
local volume = imgui.ImFloat(settings.volume.kill)
local showWindow = imgui.ImBool(false)

function chatMessage(text)
    sampAddChatMessage('{ff4747}['..string.upper(__name__)..'] {d5dedd}'..text, -1)
end

function playSound()
    if not enableSound.v then return end
    if not doesFileExist(soundsDir..soundFile) then
        chatMessage(" " .. soundFile .. "  !")
        return
    end
    local audio = loadAudioStream(soundsDir..soundFile)
    if audio then
        setAudioStreamVolume(audio, volume.v)
        setAudioStreamState(audio, 1)
        lua_thread.create(function()
            while getAudioStreamState(audio) == 1 do wait(50) end
            collectgarbage()
        end)
    end
end

function imgui.OnDrawFrame()
    if showWindow.v then
        local sw, sh = getScreenResolution()
        imgui.SetNextWindowPos(imgui.ImVec2(sw / 2, sh / 2), imgui.Cond.FirstUseEver, imgui.ImVec2(0.5, 0.5))
        imgui.SetNextWindowSize(imgui.ImVec2(200, 150), imgui.Cond.FirstUseEver)
        imgui.Begin(string.upper(__name__), showWindow, imgui.WindowFlags.NoCollapse + imgui.WindowFlags.NoResize)
        if imgui.Checkbox('Enable Sound', enableSound) then
            settings.main.enable = enableSound.v
            inicfg.save(settings, settingsFile)
        end
        if imgui.Checkbox('Only in Default World', onlyDefaultWorld) then
            settings.main.onlyDefaultWorld = onlyDefaultWorld.v
            inicfg.save(settings, settingsFile)
        end
        imgui.Text('Volume')
        if imgui.SliderFloat('##volume', volume, 0.0, 1.0, '%.2f') then
            settings.volume.kill = volume.v
            inicfg.save(settings, settingsFile)
        end
        imgui.End()
    end
end

function onReceiveRpc(id, bs)
    if id == 55 and enableSound.v then
		local _, id = sampGetPlayerIdByCharHandle(PLAYER_PED)	
		if _ then color = sampGetPlayerColor(id) end
        if onlyDefaultWorld.v and (getActiveInterior() ~= 0 or color == 2866272215) then return end
        raknetBitStreamResetReadPointer(bs)
        local killerId = raknetBitStreamReadInt16(bs)
        local playerId = raknetBitStreamReadInt16(bs)
        if (killerId == select(2, sampGetPlayerIdByCharHandle(PLAYER_PED))) or (onSpectate and killerId == spectateId) then
            playSound()
        end
    end
end
 
function main()
    if not isSampfuncsLoaded() or not isSampLoaded() then return end
    while not isSampAvailable() do wait(100) end
    if not doesFileExist(soundsDir..soundFile) then
        chatMessage(" " .. soundFile .. "  ,    !")
        enableSound.v = false
    end
    sampRegisterChatCommand('killfx', function() showWindow.v = not showWindow.v end)
    while true do
        wait(0)
        imgui.Process = showWindow.v
    end
end
 
function sampev.onSpectatePlayer(playerId, camType)
	spectateId = playerId
end

function sampev.onTogglePlayerSpectating(state)
	onSpectate = state
end