---@diagnostic disable: undefined-global
local vanillaPatterns = loadCelesteAsset("Assets/FrostHelper/LuaBoss/vanillaPatterns")


local function customBeams()
    setFlag("flag_touch_switch_goshigoshi", false)
    setFlag("flag_touch_switch_goshigoshi", true)
    shoot()
    wait(0.1)

    shoot()
    wait(0.1)
    shoot()

    wait(0.1)
    shoot()

    wait(0.1)
    shoot()

    shoot()
    wait(0.1)

    shoot()
    wait(0.1)
    shoot()

    wait(0.1)
    shoot()

    wait(0.1)
    shoot()

    shoot()
    wait(0.1)

    shoot()
    wait(0.1)
    shoot()

    wait(0.1)
    shoot()

    wait(0.1)
    shoot()

    shoot()
    wait(0.1)

    shoot()
    wait(0.1)
    shoot()

    wait(0.1)
    shoot()

    wait(0.1)
    shoot()
    shoot()
    wait(0.1)

    shoot()
    wait(0.1)
    shoot()

    wait(0.1)
    shoot()

    wait(0.1)
    shoot()

    shoot()
    wait(0.1)

    shoot()
    wait(0.1)
    shoot()

    wait(0.1)
    shoot()

    wait(0.1)
    shoot()

    wait(0.3)

    local offset = 0

    while true do
        seedRandomWithSessionTime()
        notify(math.random(1, 4))


        --[[
        beam()
        beam({
            followTime = 0.5,
            lockTime = 0.,
            --activeTime = 1,
            rotationSpeed = 20000
        })
        
        beam({
            angle = 0
        })
        beam({
            angle = 90
        })
        beam({
            angle = 180
        })
        beam({
            angle = 270
        })
        ]]

        startCoroutine(function ()
            
            while true do
                setFlag("dark", true)
                wait(1)
                setFlag("dark", false)
                wait(1)
            end
        end)

        offsetB = 20
        repeat
            beam({
                angle = 0 + offset
            },
            {
                angle = 90 + offset
            },
            {
                angle = 180 + offset
            },
            {
                angle = 270 + offset
            },
            {
                angle = 45 + offset
            },
            {
                angle = 225 + offset
            },
            {
                angle = 325 + offset
            },
            {
                angle = 90 + offsetB
            },
            {
                angle = 180 + offsetB
            },
            {
                angle = 270 + offsetB
            },
            {
                angle = 45 + offsetB
            },
            {
                angle = 225 + offsetB
            },
            {
                angle = 325 + offsetB
            }
        )

            offset = offset + 128
            wait(0.1)

         until( offset > 164 )
        
        wait(0.3)


    end
end
local function customBeamsB()
    shoot()
    wait(0.1)

    shoot()
    wait(0.1)
    shoot()

    wait(0.1)
    shoot()

    wait(0.1)
    shoot()

    shoot()
    wait(0.1)

    shoot()
    wait(0.1)
    shoot()

    wait(0.1)
    shoot()

    wait(0.1)
    shoot()

    shoot()
    wait(0.1)

    shoot()
    wait(0.1)
    shoot()

    wait(0.1)
    shoot()

    wait(0.1)
    shoot()

    shoot()
    wait(0.1)

    shoot()
    wait(0.1)
    shoot()

    wait(0.1)
    shoot()

    wait(0.1)
    shoot()
    shoot()
    wait(0.1)

    shoot()
    wait(0.1)
    shoot()

    wait(0.1)
    shoot()

    wait(0.1)
    shoot()

    shoot()
    wait(0.1)

    shoot()
    wait(0.1)
    shoot()

    wait(0.1)
    shoot()

    wait(0.1)
    shoot()    shoot()
    wait(0.1)

    shoot()
    wait(0.1)
    shoot()

    wait(0.1)
    shoot()

    wait(0.1)
    shoot()

    shoot()
    wait(0.1)

    shoot()
    wait(0.1)
    shoot()

    wait(0.1)
    shoot()

    wait(0.1)
    shoot()

    shoot()
    wait(0.1)

    shoot()
    wait(0.1)
    shoot()

    wait(0.1)
    shoot()

    wait(0.1)
    shoot()

    shoot()
    wait(0.1)

    shoot()
    wait(0.1)
    shoot()

    wait(0.1)
    shoot()

    wait(0.1)
    shoot()
    shoot()
    wait(0.1)

    shoot()
    wait(0.1)
    shoot()

    wait(0.1)
    shoot()

    wait(0.1)
    shoot()

    shoot()
    wait(0.1)

    shoot()
    wait(0.1)
    shoot()

    wait(0.1)
    shoot()

    wait(0.1)
    shoot()
    local offset = 0

    while true do
        seedRandomWithSessionTime()
        notify(math.random(1, 4))


        --[[
        beam()
        beam({
            followTime = 0.5,
            lockTime = 0.,
            --activeTime = 1,
            rotationSpeed = 20000
        })
        
        beam({
            angle = 0
        })
        beam({
            angle = 90
        })
        beam({
            angle = 180
        })
        beam({
            angle = 270
        })
        ]]

        startCoroutine(function ()
            while true do
                setFlag("dark", true)
                wait(1)
                setFlag("dark", false)
                wait(1)
            end
        end)

        repeat
            beam({
                angle = 0 + offset
            },
            {
                angle = 90 + offset
            },
            {
                angle = 180 + offset
            },
            {
                angle = 270 + offset
            })

            offset = offset + 128
            wait(0.05)

         until( offset > 1280 )
        
        wait(0.3)


    end
end

local function customBullets()

    while true do
        beginCharge()
        wait(0.30)

        local offset = 0

        repeat

                shoot(
                {
                    angle = 30 + offset,
                },
                {
                    angle = 60 + offset,
                },
                {
                    angle = 210 + offset,
                },
                {
                    angle = 240 + offset,
                },
                {
                    angle = 270 + offset,
                },
                {
                    angle = 300 + offset,
                },
                {
                    angle = 330 + offset,
                },
                {
                    angle = 360 + offset,
                })

            offset = offset + 13
            wait(0.15)

         until( offset > 666 )

        startCoroutine(function ()
            while true do
                setFlag("dark", true)
                wait(1)
                setFlag("dark", false)
                wait(1)
            end
        end)

        repeat
            beam()

            offset = offset + 28
            wait(0.1)

         until( offset > 164 )
        
        wait(1.5)

    end
end


local function coroutines()
    -- the code inside the callback will run in the background without blocking the attacking ai.
    -- the coroutine will automatically end once the boss gets hit.
    startCoroutine(function ()
        while true do
            setFlag("dark", true)
            wait(1)
            setFlag("dark", false)
            wait(1)


        end
    end)

    while true do

        beginCharge()
        wait(0.15)

        shoot({
            angleOffset = 30,
        })
        shoot({
            angleOffset = -30,
        })
        shoot({
            angleOffset = -60,
        })
        shoot({
            angleOffset = -60,
        })
        shoot({
            waveStrength = 20,
        })

        wait(0.3)
    end


end

local ais = {
    customBeams,
    customBullets,
    customBeamsB,
    coroutines,
    customBeamsB,
    customBullets,
    customBeams,
    customBullets,
    customBeams,
    customBullets,
    customBeams,
    customBullets,
    customBeams,
    customBullets,
}

function ai()
    setFlag("dark", false)
    local aiFunc = ais[nodeIndex + 1]
    if aiFunc then
        aiFunc()
    end
end

function onEnd()
    setFlag("dark", false)
end

function onHit()
    wait(0.25)

    if isFinalNode then
        shatterSpinners()
        shatterSpinners({
            types = { "refill" }
        })
    else
        shatterSpinners({
            types = { "FrostHelper/IceSpinner" },
            filter = function (spinner)
                return spinner.AttachGroup == nodeIndex - 1
            end
        })
    end
end