<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class GenerateVCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'generatev
                            {path: /path/to/views.json}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //

        global $argv;
        $views = json_decode(file_get_contents($argv[2]));

        $routes = ['/* __SCAFFOLD_VIEW_ROUTES__ */'];

        foreach ($views as $view) {

            $template = file_get_contents(base_path('generatecv/form.template.blade.php'));

            $text = str_replace("_TITLE_", $view->title, $template);
            $text = str_replace("_ACTION_", $view->action, $text);

            // fields
            /*
             <div class="form-group">
                <label class="control-label">cellNumber</label>
                <input type=cellNumber"text" class="form-control">
            </div>
            */
            $fields = [];

            foreach ($view->fields as $name => $field) {
                $html = "<div class='form-group'>\n" .
                    "<label class='control-label'>{$field->name}</label>\n";

                if(!isset($field->type)) {
                    $field->type = "text";
                }

                switch($field->type) {
                    case "switch":
                        // todo
                        break;
                    default:
                        $html .= "<input type='{$field->type}' name='$name' class='form-control'>\n";
                        break;
                }

                $html .= "</div>\n";
                $fields[] = $html;
            }
            $text = str_replace("_FIELDS_", implode("\n", $fields), $text);

            $this->file_force_contents(resource_path("views/{$view->output}.blade.php"), $text);
            echo "Generated " . resource_path("views/{$view->output}.blade.php") . "\n";

            $routes[] = "/* SCAFVR */Route::get('{$view->output}', function () { return view('{$view->output}'); });";

        }

        // write routes
        $text = [];
        $file = fopen(base_path("routes/web.php"), "r");
        while (!feof($file)) {
            $line = rtrim(fgets($file));
            if(strpos($line, "/* SCAFVR */") === FALSE) {
                $text[] = $line;
            }
        }
        $text = implode("\n", $text);
        fclose($file);
        $text = str_replace("/* __SCAFFOLD_VIEW_ROUTES__ */", implode("\n", $routes), $text);
        file_put_contents(base_path("routes/web.php"), $text);
        echo "Updated " . base_path("routes/web.php") . "\n";

    }

    private function file_force_contents($dir, $contents){
        $parts = explode('/', $dir);
        $file = array_pop($parts);
        $dir = '';
        foreach($parts as $part)
            if(!is_dir($dir .= "/$part")) mkdir($dir);
        file_put_contents("$dir/$file", $contents);
    }
}
