#!/usr/bin/env php
<?php

use kora\cli\cmd\MakeAppCommand;
use kora\cli\cmd\MakeControllerCommand;
use kora\cli\cmd\MakeEntityCommand;
use kora\cli\cmd\MakeInputCommand;
use kora\cli\cmd\MakeMiddlewareCommand;
use kora\cli\cmd\MakeRouteCommand;
use kora\cli\cmd\ORM;

require_once(__DIR__.'/vendor/autoload.php');

class Sound
{
    private $argv;
    private MakeEntityCommand $MakeEntity;
    private MakeInputCommand $MakeInput;
   // private MakeControllerCommand $MakeController;
    private MakeRouteCommand $MakeRoute;
    private MakeAppCommand $MakeApp;
    private MakeMiddlewareCommand $MakeMiddleware;

    private $cmd = [
        'build:app' => 'makeApp',
        //'orm:make:migration' => "makeMigration",
        //'orm:exec:migrations' => "execMigrations",
        //'orm:exec:migration' => "execMigrations",
        'make:controller' => 'makeController',
        'make:middleware' => 'makeMiddleware',
        'make:input' => 'makeInput',
        'orm:make:entity' => 'makeEntity',
        'app:make:route' => 'makeRoute',
    ];

    public function __construct()
    {
        $this->MakeEntity = new MakeEntityCommand(__DIR__);
      //  $this->MakeController = new MakeControllerCommand(__DIR__);
        $this->MakeRoute = new MakeRouteCommand(__DIR__);
        $this->MakeMiddleware = new MakeMiddlewareCommand(__DIR__);
        $this->MakeApp = new MakeAppCommand(__DIR__);
        $this->MakeInput = new MakeInputCommand(__DIR__);
 
        $this->argv = $_SERVER['argv'];
        $this->cmd();
    }

    private function cmd()
    {
    
        $cmd = isset($this->argv[1]) ? $this->argv[1] : null;
        
        if(!empty($cmd))
        {
            $command = str_ireplace(['make:','build:'],'',$this->argv[1]);

            if(array_key_exists($cmd,$this->cmd))
            {
                $m = $this->cmd[$cmd];
                
                unset($this->argv[0],$this->argv[1]);

                if(method_exists($this,$m))
                {
                    $this->{$m}($this->argv,str_ireplace([':make',':build'],[''],$command));
                }
            }
            else
            {
                $validsCommands = implode("\r\n",array_keys($this->cmd));
                $this->printColored("Invalid command {$this->argv[1]}, valids commands are:","\r\n{$validsCommands}",31,32);
            }
        }
        else
        {
            $this->showHelp();
        }
    }

    private function makeEntity($arg = [])
    {
        $this->MakeEntity->exec($arg);
    }


    private function makeMiddleware($arg = [])
    {
        $this->MakeMiddleware->exec($arg);
    }

    private function makeInput($arg = [])
    {
        $this->MakeInput->exec($arg);
    }

   /* private function makeEntity($arg = [])
    {
        $this->ORM->config($arg,'entity');
        $this->ORM->makeEntity();
    }*/

  /*  private function makeMigration($arg = [])
    {
        $this->ORM->config($arg,'entity');
        $this->ORM->makeMigration();
    }

    private function execMigrations($arg = [])
    {
        $this->ORM->config($arg,'entity');
        $this->ORM->execMigrations();
    }*/

    private function makeApp($arg = [])
    {
        $this->MakeApp->exec($arg);
    }

    private function makeController($arg,$cmd)
    {
        $this->MakeApp->exec($arg,$cmd);
    }

    private function makeRoute($arg = [])
    {

         $this->MakeRoute->config($arg,'route');
        // $this->MakeRoute->exec(); 
    }

    private function printColored($cmdText1, $cmdText2, $colorCode1, $colorCode2) {
        $resetCode = "\033[0m"; // Código para resetar cor
        print "\033[{$colorCode1}m{$cmdText1}{$resetCode} -> \033[{$colorCode2}m{$cmdText2}{$resetCode}";
        print(PHP_EOL);
    }

    private function showHelp()
    {
        print("------------------------------LIST OF ALL COMMANDS--------------------------------");
        print(PHP_EOL);
        $this->printColored("make:app -> Create a new App.", "Ex: [php sound build:app {nameMyApp} -c={nameController} --front={true|false}]", 32,31);
        $this->printColored("make:controller -> Create a new controller.", "Ex: [php sound make:controller {nameApp} --controller={nameController} --model={name Model (optional)}]", 32,31);
        $this->printColored("make:middleware -> Create a new middleware in route.", "Ex: [php sound make:middleware {nameMiddleware} --app={nameApp} --route={aliasRoute} --method={nameMethod} --order={after|before}]", 32,31);
        $this->printColored("make:input -> Create a new input class.", "Ex: [php sound make:input {nameInput} --app={nameApp}]", 32,31);
        $this->printColored("orm:make:entity -> Create a new entity table.", "Ex: [php sound orm:make:entity {nameEntity} --app={nameApp} --underline={true|false}]", 32,31);
    }
}

(new Sound());
