local _, ss = ...

ss = CreateFrame("Frame") 

local isInVehicle = false
local isTurning = false
local justDrifted = false
local keyboardTurning = false
local driftKeybind = "SPACE"
local jump, leftTurn, rightTurn, leftStrafe, rightStrafe

-----------------------------------------------
--- Main
-----------------------------------------------

local UpdateSpeed = 0.5
ss.Elapsed = UpdateSpeed
ss.Pressed = false

local firstLoad = true

function ss:OnEvent(event, ...)
    self[event](self, event, ...)
end

function ss:ADDON_LOADED(event, addOnName)
    if (addOnName == "SkrrtSkrrt" and firstLoad) then
        ss:ClassWelcome()
        firstLoad = false
    end
end

function ss:PlayEffect()
    if(isInVehicle and IsPlayerMoving() and (isTurning or keyboardTurning)) then
        PlayMusic("Interface/AddOns/SkrrtSkrrt/Boyz.mp3")
        justDrifted = true
    end
end

ss:SetScript("OnUpdate", function(self, elapsed)
    keyboardTurning = (IsKeyDown(leftTurn) or IsKeyDown(rightTurn) or IsKeyDown(leftStrafe) or IsKeyDown(rightStrafe))
    if not IsKeyDown(jump) or not isInVehicle or not IsPlayerMoving() or not (isTurning or keyboardTurning) then
        self.Pressed = false
        self.Elapsed = UpdateSpeed
        ss:StopEffect()
        return
    end
    self.Elapsed = self.Elapsed - elapsed
    if self.Pressed or self.Elapsed > 0 then return end
    self.Pressed = true
    ss:PlayEffect()
end)

function ss:StopEffect()
    if (justDrifted) then
        StopMusic()
        justDrifted = false
    end
end

function ss:ClassWelcome()
    local mapId = C_Map.GetBestMapForUnit("player")
    local undermineMapId = 2346
    if (mapId == undermineMapId) then
        local className = UnitClass("PLAYER")
        local _, _, _, argbHex = GetClassColor(string.upper(className))
        print("\124c".. argbHex.. "Skrrt Skrrt!\124r")
    end
end

-----------------------------------------------
--- Enablers
-----------------------------------------------

function ss:PLAYER_GAINS_VEHICLE_DATA(event)
    isInVehicle = true
end

function ss:PLAYER_STARTED_TURNING(event)
    isTurning = true
end

function ss:VARIABLES_LOADED()
    jump = GetBindingKey("JUMP")
    leftTurn = GetBindingKey("TURNLEFT")
    rightTurn = GetBindingKey("TURNRIGHT")
    leftStrafe = GetBindingKey("STRAFELEFT")
    rightStrafe = GetBindingKey("STRAFERIGHT")
end

function ss:ZONE_CHANGED_NEW_AREA()
    ss:ClassWelcome()
end

-----------------------------------------------
--- Disablers
-----------------------------------------------

function ss:PLAYER_LOSES_VEHICLE_DATA(event)
    isInVehicle = false
    ss:StopEffect(justDrifted)
end

function ss:PLAYER_STOPPED_TURNING()
    isTurning = false
    ss:StopEffect(justDrifted)
end

-----------------------------------------------
--- Register
-----------------------------------------------

ss:RegisterEvent("ADDON_LOADED")
ss:RegisterEvent("PLAYER_GAINS_VEHICLE_DATA")
ss:RegisterEvent("PLAYER_LOSES_VEHICLE_DATA")
ss:RegisterEvent("PLAYER_STARTED_TURNING")
ss:RegisterEvent("PLAYER_STOPPED_TURNING")
ss:RegisterEvent("VARIABLES_LOADED")
ss:RegisterEvent("ZONE_CHANGED_NEW_AREA")
ss:SetPropagateKeyboardInput(true)
ss:SetScript("OnEvent", ss.OnEvent)
