#import_plugin RadioPlayer as Player

// Project: RadioPlayer 
// Created: 2018-07-09

// show all errors
SetErrorMode(2)

#constant swidth = 1024
#constant sheight = 768

// set window properties
SetWindowTitle( "RadioPlayer" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window

// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts

//Player.playmusicfromurl("http://as-hls-uk-live.akamaized.net/pool_7/live/bbc_radio_two/bbc_radio_two.isml/bbc_radio_two-audio%3d320000.norewind.m3u8")

type _stations
	name as string
	url as String
	txtid as integer
endtype
global stations as _stations[]
global addstation as _stations
global stationcounter=0

global textsize = 60

textoffset = 0
volume = 100
buffer = 0

/*
PLS file normally
[playlist]
NumberOfEntries=1
File1=http://uk7.internet-radio.com:8226/stream
Title1=Box UK Radio danceradiouk
Length1=-1
Version=2
*/
transition = 0
currentstation = 0
add_station("Box UK Radio danceradiouk" , "http://uk7.internet-radio.com:8226/stream")
add_station("60s/70s/80s/90s/00s", "http://uk2.internet-radio.com:8358/stream")
add_station("Radio2","http://as-hls-uk-live.akamaized.net/pool_7/live/bbc_radio_two/bbc_radio_two.isml/bbc_radio_two-audio%3d128000.m3u8")

title = CreateText("Radio Player")
b=0
do
    for a=0 to stations.length
		SetTextPosition(stations[a].txtid, (swidth/2 - GetTextTotalWidth(stations[a].txtid)/2), sheight/2 - GetTextTotalHeight(stations[a].txtid)/2 + (a*textsize) - textoffset)
		if a = currentstation
			SetTextColor(stations[a].txtid,0,255,0,b)
			inc b,5
		else
			SetTextColor(stations[a].txtid, 0,100,0,200)
			SetTextSize(stations[a].txtid,textsize - (a*20))
		endif
	next
	
		
	if GetRawKeyPressed(38) 
		if currentstation>0 
			dec textoffset,textsize
			dec currentstation
			transition=1
			volumedown=0
		endif
	endif
	
	if GetRawKeyPressed(40) 
		if currentstation < stations.length
			inc textoffset,textsize
			inc currentstation
			transition = 1
			volumedown=0
		endif
	endif
		

	if volumedown=0 
		dec volume,1
		Player.SetVolume ( volume )
	
	endif
	if volumedown=1
		inc volume,1
		Player.SetVolume ( volume )
	endif
	
	if volume>100 then volume=100

	if transition=1 
		if volume<0 
		
			Player.playmusicfromurl(stations[currentstation].url)
			transition = 0
			volumedown=1
			
		endif
	endif
	
	print(GetRawLastKey())
	if GetRawKeyPressed(87) 
		
		inc volume
		if volume >= 100 then volume=100
		Player.SetVolume (volume)
	endif

	if GetRawKeyPressed(83)
		dec volume
		if volume<0 then volume = 0
	
		Player.SetVolume ( volume )
	endif

	if GetRawKeyPressed(65) 
		inc buffer,100
		Player.SetBuffer(buffer)
		
	endif
	
	if GetRawKeyPressed(68)
		dec buffer, 100
		Player.SetBuffer(buffer)
	endif

    Print( "Press UP and DOWN buttons" )
    Sync()
loop

function add_station (freindlyname as string, url as string)

	addstation.txtid = CreateText(freindlyname)
	addstation.name = freindlyname
	addstation.url = url
	
	
	SetTextSize(addstation.txtid, textsize)
	stations.insert(addstation)

	inc stationcounter
endfunction	
