#!/usr/bin/env php
<?php
declare(strict_types=1);//optional but good practice IMO. Google it.

use BHayes\CLI\CLI;
use BHayes\CLI\Colour;
use BHayes\CLI\UserErrorResponse;
use BHayes\CLI\UserResponse;
use BHayes\CLI\UserSuccessResponse;

require_once 'vendor/autoload.php'; //if installed via composer

/**
 * This is the documentation that will appear when you type --help.
 */
class Example
{

    /**
     * This one is easy to run.
     * try runMe with --help to see this text.
     */
    public function runMe(string $optional = null)
    {
        //either return output or just output directly its up to you.
        echo "I work with no arguments.";
        if ($optional !== null) {
            echo " But thanks for providing me with: ";
            var_dump($optional);
        }
    }

    /**
     * This command will only run when all the requirements are met.
     */
    public function tryMe(bool $bool, string $string, float $float, int $int)
    {
        return "You did it! You gave me bool a string, a float and an int.";
    }

    /**
     * This method will accept any number of string arguments while the
     * the others will fail if you pass them too many arguments.
     *
     * @param string ...$bunchOfStrings
     */
    public function variadic(string ...$bunchOfStrings)
    {
        echo "You said ";
        if (empty($bunchOfStrings)) {
            echo "nothing.";
        }
        print_r($bunchOfStrings);
        echo "\n";
    }

    /**
     * Demos prompts.
     *
     * @throws UserResponse
     */
    public function survey():string
    {
        if (! CLI::confirm('Shall we begin?')) {
            return "Cancelled";
        }
        $colour = CLI::prompt('Whats your favorite colour?');
        $colourCode = Colour::code($colour);
        throw new UserResponse("I love $colour too!", $colourCode, '☺');
    }


    /**
     * Tests the UserResponse throwable.
     *
     * @param bool|null $success
     * @throws UserResponse
     */
    public function throwsUserResponse(bool $success = null)
    {
        if ($success === true) {
            throw new UserSuccessResponse();//all params optional
        }
        if ($success === false) {
            throw new UserErrorResponse('Some error message user needs to see!');
        }
        throw new UserResponse('Try this again with true or false.');
    }

    /**
     * foo is now an option because it has been declared public.
     * @var bool
     */
    public $foo = false;

    /**
     * Run me with and without `--foo` and see the result.
     */
    public function bar()
    {
        if (!$this->foo) return "Try running this again with the --foo option.";
        return $this;
    }
};

$cli = new CLI(new Example());
$cli->run();
