<?php

use Illuminate\Foundation\Inspiring;

/*
|--------------------------------------------------------------------------
| Console Routes
|--------------------------------------------------------------------------
|
| This file is where you may define all of your Closure based console
| commands. Each Closure is bound to a command instance allowing a
| simple approach to interacting with each command's IO methods.
|
*/

Artisan::command('inspire', function () {
    $this->comment(Inspiring::quote());
})->describe('Display an inspiring quote');

Artisan::command('routine {port?}', function ($port = 8000) {
    $this->call('config:cache');
    $this->call('view:clear');
    $this->call('serve', [
        '--port' => $port
    ]);
})->describe('Command for routine');

Artisan::command('project:deploy {commandsOptions?}', function ($commandsOptions = "") {
    $arrAllowedCommands = [
        "composer dumpautoload",
        "php artisan config:cache",
        "php artisan view:clear",
        "php artisan storage:link",
    ];

    runCommands($arrAllowedCommands, $commandsOptions);
})->describe('Command for deploying the project');

Artisan::command('project:load {commandsOptions?}', function ($commandsOptions = "") {
    $arrAllowedCommands = [
        "php artisan config:cache",
        "php artisan view:clear",
        "php artisan serve",
    ];

    runCommands($arrAllowedCommands, $commandsOptions);
})->describe('Command for loading the project');

Artisan::command('project:storagelink {commandsOptions?}', function ($commandsOptions = "") {
    $arrAllowedCommands = [
        "mkdir storage\app\public",
        "ln -s storage\app\public public\storage",
    ];

    runCommands($arrAllowedCommands, $commandsOptions);
})->describe('Command for symlinking the project');

Artisan::command('project:update {commandsOptions?}', function ($commandsOptions = "") {
    $arrAllowedCommands = [
        "git pull",
        "composer dumpautoload",
        "php artisan config:cache",
        "php artisan view:clear",
    ];

    runCommands($arrAllowedCommands, $commandsOptions);
})->describe('Command for updating the project');

function runCommands($arrAllowedCommands, $commandsOptions) {
    $arrCommandsOptions = explode(";", $commandsOptions);

    foreach ($arrCommandsOptions as $commandOptions) {
        if ($commandOptions == "") {
            continue;
        }

        $arrCommandOptions = explode("$", $commandOptions);
        $command = "$arrCommandOptions[0]";
        $options = "$arrCommandOptions[1]";
        $arrOptions = explode(",", $options);

        if (in_array($command, $arrAllowedCommands)) {
            $fullCommand = $command;

            foreach ($arrOptions as $option) {
                if ($option == "") {
                    continue;
                }

                $fullCommand .= " " . $option;
            }

            $allowedCommandIndex = array_search($command, $arrAllowedCommands);
            $arrAllowedCommands[$allowedCommandIndex] = $fullCommand;
        }
    }

    foreach ($arrAllowedCommands as $allowedCommand) {
        exec($allowedCommand);
    }
}
