--[[ economics?
10 per spin

10*125(chances) 1250
return 1250 after 125 spins (for 100% return)

t bigwin (x1) 300
t other (x4) 150 600
d bigwin (x5) 30 150
s bigwin (x25) 3 75
returns 1,125 (90%)?
]]
pName = GetStringParam("name","img")
bankbalance = 200
function init()
    --
    -- Buttons
    SlotPull = FindShapes("SlotPull", false)
    for i=1, #SlotPull do
        SetTag(SlotPull[i], "interact", "Spin")
    end

    -- Joints
    wheel1 = FindJoint("SlotJoint1", false)
    wheel2 = FindJoint("SlotJoint2", false)
    wheel3 = FindJoint("SlotJoint3", false)

    lever = FindJoint("LeverJoint", false)

    clock = 0

    slottimer1 = 0
    slottimer2 = 0
    slottimer3 = 0

    slotland1 = 0
    slotland2 = 0
    slotland3 = 0

    local a = FindLocation("slotloc", false)
    slotloc = GetLocationTransform(a)

    -- just here to avoide errors
    checkwintimer = 0

    builduptoggle = false
    spintimeout = 0

    leveranimtimer = 0

    -- sounds
    slot_ambient = LoadLoop("MOD/sounds/slot/slot_ambient.ogg")
    slot_buildup = LoadSound("MOD/sounds/slot/slot_buildup.ogg")
    slot_coins = LoadSound("MOD/sounds/slot/slot_coins.ogg")
    slot_hit = LoadSound("MOD/sounds/slot/slot_hit.ogg")
    slot_lose = LoadSound("MOD/sounds/slot/slot_lose.ogg")
    slot_miscloop = LoadLoop("MOD/sounds/slot/slot_miscloop.ogg")
    slot_miscloop2 = LoadLoop("MOD/sounds/slot/slot_miscloop2.ogg")
    slot_roll = LoadLoop("MOD/sounds/slot/slot_roll.ogg")
    slot_roll2 = LoadLoop("MOD/sounds/slot/slot_roll2.ogg")
    slot_winbig = LoadSound("MOD/sounds/slot/slot_winbig.ogg")
    slot_winsmall = LoadSound("MOD/sounds/slot/slot_winsmall.ogg")
end

function tick(dt) -- for buttons (frame rate based)
    --
    screen = UiGetScreen()
    pName = GetTagValue(screen, "name")

    SlotBroken = IsShapeBroken(FindShape("SlotBase", false))

    -- sounds
    if playhits then
        --
        if clock > slottimer1 and hittoggle1 then
            PlaySound(slot_hit, slotloc.pos, 0.5)
            hittoggle1 = false
        end
        if clock > slottimer2 and hittoggle2 then
            PlaySound(slot_hit, slotloc.pos, 0.5)
            hittoggle2 = false
        end
        if clock > slottimer3 and hittoggle3 then
            PlaySound(slot_hit, slotloc.pos, 0.5)
            hittoggle3 = false
        end
    end

    -- Slot Machine Wheels
    SlotMachinePull(slottimer1, wheel1)
    SlotMachinePull(slottimer2, wheel2)
    SlotMachinePull(slottimer3, wheel3)

    SlotLever()

    if not SlotBroken then
        PlayLoop(slot_ambient, slotloc.pos, 0.3)
    end
end

function update(dt)
    --
    if GetTime() > clock then
        clock = clock + 0.03 --second(s)
    end


    --DebugWatch("clock", clock)
    --DebugWatch("slottimer1", slottimer1)
    --DebugWatch("slottimer2", slottimer2)
    --DebugWatch("slottimer3", slottimer3)
    --DebugWatch("GameTime", GetTime())

    --DebugWatch("wincheck", wincheck)
    --DebugWatch("checkwintimer", checkwintimer)

    DebugWatch("bankbalance", bankbalance)
    --DebugWatch("SlotBroken", SlotBroken)
end

function draw()
    --
    for i = 1, #SlotPull do
        if GetPlayerInteractShape() == SlotPull[i] and InputPressed("interact") and clock > spintimeout then -- spin button
            if bankbalance >= 10 then
                --DebugPrint("spinning")

                slottimer1 = clock + 2
                slottimer2 = clock + 2.33
                slottimer3 = clock + 2.66

                checkwintimer = slottimer3 + 0.3

                winchecktable = {}
                togglethis = true
                hittoggle1 = true
                hittoggle2 = true
                hittoggle3 = true
                playhits = true
                builduptoggle = true

                spintimeout = clock + 3
                bankbalance = bankbalance - 10

                leveranimtimer = clock + 0.15
            else
                DebugPrint("Your Broke!")
            end
        end
    end




    if screen == 0 then
        return
    end

    UiTranslate(UiCenter(), UiMiddle())
    UiAlign("center middle")
    UiImage("MOD/image/slot/" .. pName .. ".png")
end





function SlotMachinePull(SlotTimer, Wheel) -- running all the time
    --
    if clock < SlotTimer then
        SetJointMotor(Wheel, 20.72)
        --sounds
        PlayLoop(slot_roll2, slotloc.pos, 0.3)
        if builduptoggle then
            PlaySound(slot_buildup, slotloc.pos, 0.5)
            builduptoggle = false
        end
        playhits = true
    elseif (CheckRange(GetJointMovement(Wheel))) ~= "White" then
        SetJointMotor(Wheel, 0)
    end

    if clock > checkwintimer and clock < checkwintimer + 1 and togglethis then
        for i=1, 1 do
            --
            CheckWin()

            togglethis = false
        end
    end
end

function CheckRange(input)
    --
    if input > -9 and input <= 9 then
        return "Seven"
    elseif input > 9 and input <= 45 then
        return "White"
    elseif input > 45 and input <= 81 then
        return "Cherry"
    elseif input > 81 and input <= 117 then
        return "White"
    elseif input > 117 and input <= 153 then
        return "Banana"
    elseif input > 153 or input <= -153 then
        return "White"
    elseif input > -153 and input <= -117 then
        return "Bigwin"
    elseif input > -117 and input <= -81 then
        return "White"
    elseif input > -81 and input <= -45 then
        return "Bar"
    elseif input > -45 and input <= -9 then
        return "White"
    else
        return "White"
    end
end

function CheckWin()
    --
    local xSeven  = 0
    local xCherry = 0
    local xBanana = 0
    local xBar    = 0
    local xBigwin = 0

    local table = { a = (CheckRange(GetJointMovement(wheel1))), b = (CheckRange(GetJointMovement(wheel2))), c = (CheckRange(GetJointMovement(wheel3))) }

    for i, v in pairs(table) do
        --
        if v == "Seven" then
            xSeven = xSeven + 1
        elseif v == "Cherry" then
            xCherry = xCherry + 1
        elseif v == "Banana" then
            xBanana = xBanana + 1
        elseif v == "Bar" then
            xBar = xBar + 1
        elseif v == "Bigwin" then
            xBigwin = xBigwin + 1
        end
    end

    if xSeven == 3 then
        --DebugPrint("tripleSeven")
        PlaySound(slot_winbig, slotloc.pos, 0.5)
        PlaySound(slot_coins, slotloc.pos, 1)

        bankbalance = bankbalance + 150
        DebugPrint("Won $150")
    elseif xCherry == 3 then
        --DebugPrint("tripleCherry")
        PlaySound(slot_winbig, slotloc.pos, 0.5)
        PlaySound(slot_coins, slotloc.pos, 1)

        bankbalance = bankbalance + 150
        DebugPrint("Won $150")
    elseif xBanana == 3 then
        --DebugPrint("tripleBanana")
        PlaySound(slot_winbig, slotloc.pos, 0.5)
        PlaySound(slot_coins, slotloc.pos, 1)

        bankbalance = bankbalance + 150
        DebugPrint("Won $150")
    elseif xBar == 3 then
        --DebugPrint("tripleBar")
        PlaySound(slot_winbig, slotloc.pos, 0.5)
        PlaySound(slot_coins, slotloc.pos, 1)

        bankbalance = bankbalance + 150
        DebugPrint("Won $150")
    elseif xBigwin == 3 then
        --DebugPrint("tripleBigwin")
        PlaySound(slot_winbig, slotloc.pos, 0.5)
        PlaySound(slot_coins, slotloc.pos, 1)

        bankbalance = bankbalance + 300
        DebugPrint("Won $300")
    elseif xBigwin == 2 then
        --DebugPrint("doubleBigwin")
        PlaySound(slot_winsmall, slotloc.pos, 0.5)
        PlaySound(slot_coins, slotloc.pos, 1)

        bankbalance = bankbalance + 30
        DebugPrint("Won $30")
    elseif xBigwin == 1 then
        --DebugPrint("singleBigwin")
        PlaySound(slot_winsmall, slotloc.pos, 0.5)
        PlaySound(slot_coins, slotloc.pos, 1)

        bankbalance = bankbalance + 3
        DebugPrint("Won $3")
    else
        --DebugPrint("nothing")
        PlaySound(slot_lose, slotloc.pos, 0.5)
    end

end

------------------------------------ Lever ------------------------------------

function SlotLever()
    --
    --DebugWatch((GetJointMovement(lever)))

    if leveranimtimer > clock then
        SetJointMotor(lever, 6.5)
    else
        if (GetJointMovement(lever)) <= -1 and (GetJointMovement(lever)) > -10 then
            SetJointMotor(lever, 0)
        else
            SetJointMotor(lever, -3)
        end
    end
end
