<?php
require_once 'config.inc.php';


for ($i = 0; $i < 50; $i++)
{
    echo "\r\n";
}

/**
 * @throws Exception
 */
function main()
{

    $cmd = readline( "KM@CLI# " );
    $cmd = explode( ' ', $cmd );
    switch( $cmd[0] )
    {
        case 'help':
            help( $cmd );
            break;
        case 'getfile':
            if( !isset( $cmd[1] ) )
            {
                echo "Wrong usage, please use help for help.\n";
                break;
            }
            $downloader = new Downloader();
            try
            {
                $downloader->requestSong( $cmd[1] );
            } catch ( RuntimeException $e )
            {
                echo "An error occured while processing the download.\n";
                echo $e->getMessage() . "\n";
            }
            break;
        case 'downloadmusic':
            echo "This function has an old CLI! Opening CLI...\n-----------\n";
            new AudioVideoHandler();
            echo "CLI exited. Continue with new CLI...\n";
            break;
        case 'setsid':
            if( !isset( $cmd[1] ) )
            {
                echo "Wrong usage, please use help for help.\n";
                break;
            }
            echo "Function not completed yet!\n";
            break;
        case 'exit':
            echo "Have a nice dayyyy!!\n";
            exit();
        default:
            echo "Unkown command. Use help for help.\n";
            break;
    }
    return;
}


### Global System
while( true )
{
    try
    {
        main();
    } catch( Exception $e )
    {
        echo "\n\n\nWhoops! - An error occured.";
        echo "\n" . $e->getMessage();
        exit();
    }
}


## Commands

function help( $a_cmd )
{
    echo "--- Help ---\n";
    if( isset( $a_cmd[1] ) )
    {
        switch( $a_cmd[1] )
        {
            case 'getfile':
                echo "getfile id(int)";
                break;
            case 'downloadmusic':
                echo "downloadmusic [artist(string)] [title(title)]";
                break;
            case 'setsid':
                echo "setsid id(string)";
                break;
            default:
                echo "help for this command not found!";
                break;
        }
    }
    else
    {
        echo "
        getfile - Download beatmap with an id
        downloadmusic - Download mp3 and mp4 for an beatmap
        help - Show this help
        setsid - Set the php session id
        ";
    }
    echo "\n--- Help ---\n";

}