@name Audio Player FailCake
@inputs GX GY Use OnScreen Screen:wirelink
@persist Music:array Lengths:array Length:string PreviousSong Seconds Page I
@persist Volume Pitch ScrollVolume ScrollPitch
@persist HoverVolume HoverPitch HoverList
@persist ColorUp ColorDown ColorHover ColorScrollbar ColorList ColorHoverList ColorHeader ColorHeaderBG
@trigger GX GY Use OnScreen

Y = ceil(max(GY - 0.035, 0) / 0.0522)
X = ceil(max(GX - 0.025, 0) / 0.03167)

if (clk("Tick"))
{
    ElapsedTime = toString(floor(Seconds / 60)) + ":" + (Seconds % 60 > 9 ? toString(Seconds % 60) : "0" + toString(Seconds % 60))
    Screen:writeString(ElapsedTime + "/" + Length, 20, 0, ColorHeader, ColorHeaderBG)
    
    if (ElapsedTime == Length)
    {
        soundPurge()
        timer("Next song", 1500)
        exit()
    }

    Seconds++
    timer("Tick", 1000)
}

if (clk("Next song"))
{
    Random = randint(Music:count())
    
    if (Random == PreviousSong)
    {
        timer("Next song", 0)
        exit()
    }
    
    PreviousSong = Random
    
    Title = Music:string(Random), Title = (Title:length() <= 19 ? Title : Title:sub(0, 16) + "...")
    Screen:writeString(Title + " ":repeat(29 - Title:length()), 0, 0, ColorHeader, ColorHeaderBG)
    Page = ceil(Random / 10)
    Screen:writeString("Page " + toString(Page) + "/" + toString(ceil(Music:count() / 10)),  10, 17, 464, 333)
    
    soundPlay(0, 0, "music/" + Title + ".mp3")
    soundVolume(0, Volume / 26)
    soundPitch(0, 100 + (Pitch - 13) * 3)
    
    Length = Lengths:string(Random)
    Seconds = 0
    I = 1
    
    timer("Display music", 0)
    timer("Tick", 0)
}

if (!OnScreen & ~OnScreen & HoverList != 0)
{
    Number = toString((Page - 1) * 10 + HoverList - 6), Number = (Number:length() > 1 ? Number : "0" + Number)
    Screen:writeString(Number, 0, HoverList - 6 + 5, ColorList)
    
    HoverList = 0
}

if (!Use)
{
    if (Y != HoverList & OnScreen)
    {
        if (HoverList != 0 & Lengths:string((Page - 1) * 10 + HoverList - 6))
        {
            Number = toString((Page - 1) * 10 + HoverList - 6), Number = (Number:length() > 1 ? Number : "0" + Number)
            Screen:writeString(Number, 0 , HoverList - 6 + 5, ColorList)
        }
        
        if (Y >= 7 & Y <= 16 & Lengths:string((Page - 1) * 10 + Y - 6))
        {
            HoverList = Y
            
            Number = toString((Page - 1) * 10 + HoverList - 6), Number = (Number:length() > 1 ? Number : "0" + Number)
            Screen:writeString(Number, 0 , HoverList - 6 + 5, ColorHoverList)
        }
        else
        {
            HoverList = 0
        }
    }
    
    if (HoverVolume != (Y == 3 & X >= Volume + 1 & X <= Volume + 3 & OnScreen))
    {
        HoverVolume = !HoverVolume
        Screen:writeString("   ", Volume, 2, 0, (HoverVolume ? ColorHover : ColorUp))
    }
    elseif (HoverPitch != (Y == 5 & X >= Pitch + 1 & X <= Pitch + 3 & OnScreen))
    {
        HoverPitch = !HoverPitch
        Screen:writeString("   ", Pitch, 4, 0, (HoverPitch ? ColorHover : ColorUp))
    }
    
    Screen:writeString(" <<< ",  0, 17, 999, (Y == 18 & X >= 0 & X <= 5 & Page != 1 ? ColorHover : ColorUp))
    Screen:writeString(" >>> ",  24, 17, 999, (Y == 18 & X >= 25 & X <= 29 & Page != ceil(Music:count() / 10) ? ColorHover : ColorUp))
    
    if (~Use)
    {
        if (ScrollVolume)
        {
            ScrollVolume = 0
            Screen:writeString("   ", Volume, 2, 0, (HoverVolume ? ColorHover : ColorUp))
        }
        elseif (ScrollPitch)
        {
            ScrollPitch = 0
            Screen:writeString("   ", Pitch, 4, 0, (HoverPitch ? ColorHover : ColorUp))
        }
    }
}

if (ScrollVolume)
{
    X = min(X, 28)
    X = max(X, 2)
    
    if (X - 2 != Volume)
    {
        Screen:writeString("   ", Volume, 2, 0, ColorScrollbar)
        Volume = X - 2
        Screen:writeString("   ", Volume, 2, 0, ColorDown)
        
        soundVolume(0, Volume / 26)
    }
    
    exit()
}
elseif (ScrollPitch)
{
    X = min(X, 28)
    X = max(X, 2)
    
    if (X - 2 != Pitch)
    {
        Screen:writeString("   ", Pitch, 4, 0, ColorScrollbar)
        Screen:writeString("   ", 13, 4, 0, ColorScrollbar + 111)
        Pitch = X - 2
        Screen:writeString("   ", Pitch, 4, 0, ColorDown)
        
        soundPitch(0, 100 + (Pitch - 13) * 3)
    }
    
    exit()
}

if (Use & ~Use)
{
    if (Y == 3 & X >= Volume + 1 & X <= Volume + 3)
    {
        ScrollVolume = 1
        Screen:writeString("   ", Volume, 2, 0, ColorDown)
    }
    elseif (Y == 5 & X >= Pitch + 1 & X <= Pitch + 3)
    {
        ScrollPitch = 1
        Screen:writeString("   ", Pitch, 4, 0, ColorDown)
    }
    elseif (Y >= 7 & Y <= 16)
    {
        Title = Music:string((Page - 1) * 10 + Y - 6), Title = (Title:length() <= 19 ? Title : Title:sub(0, 16) + "...")
        Screen:writeString(Title + " ":repeat(29 - Title:length()), 0, 0, ColorHeader, ColorHeaderBG)
        
        soundPurge()
        soundPlay(0, 0, "music/" + Title + ".mp3")
        soundVolume(0, Volume / 26)
        soundPitch(0, 100 + (Pitch - 13) * 3)
        
        Seconds = 0
        Length = Lengths:string((Page - 1) * 10 + Y - 6)
        PreviousSong = (Page - 1) * 10 + Y - 6
        
        timer("Tick", 0)
    }
    elseif (Y == 18 & X >= 0 & X <= 5 & Page > 1)
    {
        I = 1, Page--
        Screen:writeString(" <<< ",  0, 17, 999, ColorDown)
        Screen:writeString("Page " + toString(Page) + "/" + toString(ceil(Music:count() / 10)),  10, 17, ColorHoverList, 333)
        timer("Display music", 0)
    }
    elseif (Y == 18 & X >= 25 & X <= 29 & Page < ceil(Music:count() / 10))
    {
        I = 1, Page++
        Screen:writeString(" >>> ",  24, 17, 999, ColorDown)
        Screen:writeString("Page " + toString(Page) + "/" + toString(ceil(Music:count() / 10)),  10, 17, ColorHoverList, 333)
        timer("Display music", 0)
    }
}

if (clk("Display music"))
{
    #Clear line
    Screen:writeCell(2039, I + 5)
    
    if (Music:string((Page - 1) * 10 + I))
    {
        Title = Music:string((Page - 1) * 10 + I), Title = (Title:length() <= 19 ? Title : Title:sub(0, 16) + "...")
        Number = toString((Page - 1) * 10 + I), Number = (Number:length() > 1 ? Number : "0" + Number)
        Screen:writeString(Number, 0 , I + 5, ColorList), Screen:writeString(Title, 3, I + 5)
        Screen:writeString(Lengths:string((Page - 1) * 10 + I), 25, I + 5)
    }
    
    I++
    
    if (I > 10)
    {
        exit()
    }
    
    timer("Display music", 0)
}

if (clk("Init"))
{
    ColorHeader    = 111
    ColorHeaderBG  = 999
    ColorUp        = 666
    ColorDown      = 459
    ColorHover     = 679
    ColorScrollbar = 333
    ColorList      = 333
    ColorHoverList = 999
    
    Volume = 26
    Pitch  = 13
    
    #Clear arrays (Duplicator...)
    Lengths = array(), Music = array()
    
    #Add music
    Lengths:pushString("2:11"), Music:pushString("HL1_song3")
    Lengths:pushString("1:36"), Music:pushString("HL1_song5")
    Lengths:pushString("1:40"), Music:pushString("HL1_song6")
    Lengths:pushString("1:33"), Music:pushString("HL1_song9")
    Lengths:pushString("1:44"), Music:pushString("HL1_song10"),        
    Lengths:pushString("0:34"), Music:pushString("HL1_song11")
    Lengths:pushString("1:30"), Music:pushString("HL1_song14")
    Lengths:pushString("2:00"), Music:pushString("HL1_song15")
    Lengths:pushString("2:03"), Music:pushString("HL1_song17")
    Lengths:pushString("1:55"), Music:pushString("HL1_song19")
    Lengths:pushString("1:24"), Music:pushString("HL1_song20")
    Lengths:pushString("1:24"), Music:pushString("HL1_song21")
    Lengths:pushString("1:17"), Music:pushString("HL1_song24")
    Lengths:pushString("1:01"), Music:pushString("HL1_song25_REMIX3")
    Lengths:pushString("0:37"), Music:pushString("HL1_song26")
    Lengths:pushString("1:24"), Music:pushString("HL2_intro")
    Lengths:pushString("0:39"), Music:pushString("HL2_song0")
    Lengths:pushString("1:38"), Music:pushString("HL2_song1")
    Lengths:pushString("2:52"), Music:pushString("HL2_song2")
}
