<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class GenerateVShortCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'generatevshort
                            {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 $output => $view) {

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

            $text = str_replace("_TITLE_", $view[0], $template);
            $text = str_replace("_ACTION_", $view[1], $text);

            // fields
            $fields = [];

            foreach ($view[2] as $field) {
                $parts = explode(":", $field);
                $type = "text";
                if(count($parts) === 3) $type = $parts[2];
                $html = "<div class='form-group'>\n" .
                    "<label class='control-label'>{$parts[1]}</label>\n";
                switch($type) {
                    case "switch":
                        // todo
                        break;
                    default:
                        $html .= "<input type='{$type}' name='{$parts[1]}' 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/$output.blade.php"), $text);
            echo "Generated " . resource_path("views/$output.blade.php") . "\n";

            $routes[] = "/* SCAFVR */Route::get('$output', function () { return 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);
    }
}
