<?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 occurred while processing the download.\n";
                echo $e->getMessage() . "\n";
            }
            break;
        case 'downloadmusic':
            if( isset( $cmd[1] ) )
            {
                echo "In this version, passing arguments to this function is not supported. Arguments will be ignored!\n";
            }
            echo "Loading another CLI...\n-----------\n";
            new AudioVideoHandler();
            echo "CLI exited. Continue with main CLI...\n";
            break;
        case 'setsid':
            if( !isset( $cmd[1] ) )
            {
                echo "Wrong usage, please use help for help.\n";
                break;
            }
            echo "Setting PHPSessionID...";
            $id = $cmd[1];
            $file = file_get_contents( './config.inc.php' );
            $file = str_ireplace( "define( 'CURL_PHPSESSID', '' );", "define( 'CURL_PHPSESSID', '$id' );", $file );
            file_put_contents( './config.inc.php', $file );
            echo "OK\n";
            echo "Please use 'exit' and restart the script to apply the new session id!\n";
            break;
        case 'exit':
            echo "Goodbye!\n";
            exit();
        default:
            echo "Unknown 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";

}