<?php

namespace Filipveschool\Packagegenerator;

use Illuminate\Console\Command;
use Filipveschool\Packagegenerator\PackagerHelper;
use Illuminate\Support\Facades\Artisan;

use Illuminate\Support\Facades\File;

/**
 * Create a brand new package.
 *
 * @package Packager
 * @author JeroenG
 *
 **/
class PackagerOverviewChoiceMenuCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'packager:startmenu';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create a new startmenu.';

    /**
     * Packager helper class.
     * @var object
     */
    protected $helper;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct(PackagerHelper $helper)
    {
        parent::__construct();
        $this->helper = $helper;

    }

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

        //TODO de keuze aan de gebruiker laten door een input te vragen en dan overal instellen aan de hand van een config file.

        $path = getcwd() . '/packages/';

        // Create the package directory
        //$this->info('Creating packages directory...');
        $this->helper->makeDir($path);

        $this->makeOverviewMenu();

    }

    public function makeOverviewMenu()
    {
        $headfolder = $this->ask('what is the name of your head folder where the packages are stored in?');

        $list = File::directories($headfolder);

        echo "packages contains ", count($list), " directories.";

        /*
        foreach ($list as $directory => $value) {
            printf("directory : " . $directory . ' - vendor : ' . $value . PHP_EOL);
        }
        */

        foreach ($list as $directory) {
            //printf($directory . PHP_EOL);
            $pieces = explode('\\', $directory);
            //print_r($pieces);
            $folderarray[] = $pieces[1];

            //staat er tijdelijk in voor onderbreking
            //printf($pieces);
        }

        //$folderarray[count($list)] = "Choice this if you don't have a vendor folder yet";

        //print_r($folderarray);

        if (count($list) == 0) {
            $vendor = $this->ask('what is the name of the vendor?');
            $name = $this->ask('What is the name of the package');

            //$this->startMenu($vendor);
            $this->call('packager:new',
            ['vendor' => $vendor,
                'name' => $name,
            ]);

        } else {

            $choiceVendors = $this->choice('What is the name of your vendor?', $folderarray);

            switch ($choiceVendors) {
                case 0:
                    '';
                    break;

                default:
                    '';
                    break;
            }
        }

    }

    public function startMenu($vendor)
    {
        $choices = [
            'Create a new package',
            'Create a new view file',
            'Create a new Config file',
            'Create a new command file',
            'Create a new Migration file',
            'Create a new Seeder file',
            'Create a new Model file ',
            'Create a new controller',
            'Create a new Middleware',
            'Create a new request file',
            'Create a new routes file',
            'Create a new Provider',
            'Create a new repository',
            'Create a new Language file',
            'Create a new View',
            'Create a new Social class'
        ];

        $choiceValue = $this->choice('What action would you like to do?', $choices);

        //The value from the choices array hierboven is de index waarop je vergelijkt met de case value hieronder.
        //Maar de user moet wel de index als een nummer typen met de choice methode hierboven.

        switch ($choiceValue) {
            case 'Create a new package':
                $this->call('packager:startmenu');
                printf("keuze 0" . PHP_EOL);
                break;
            case 'Create a new view file':
                printf("keuze 1" . PHP_EOL);
                $vendor = $this->ask('What is the vendor name?', $this->argument('vendor'));
                $name = $this->ask('What is the package name?', $this->argument('name'));
                $nameView = $this->ask('What will be the name of the view?');

                $this->call('packager:view', [
                    'vendor' => $vendor,
                    'name' => $name,
                    'nameView' => $nameView
                ]);
                /*
                 * packager:view
                            {vendor : The name of the head folder.}
                            {name : The name of the module-folder}
                            {nameView : The name of the view}
                            {--plain : empty view or not}
                            {--parent=layouts.master : The parent Blade Template this view extends.}
                            {--i : make it interactive}';
                 */
                break;
            default:
                printf("default" . PHP_EOL);
                break;
        }

        $this->info($choiceValue);
    }


    protected function interactiveReplace($vendor, $name, $fullPath)
    {
        $author = $this->ask('Who is the author?');
        $authorEmail = $this->ask('What is the author\'s e-mail?');
        $authorSite = $this->ask('What is the author\'s website?');
        $description = $this->ask('How would you describe the package?');
        $license = $this->ask('Under which license will it be released?');
        $homepage = $this->ask('What is going to be the package website?', 'https://github.com/' . $vendor . '/' . $name);

        $search = [
            ':author_name',
            ':author_email',
            ':author_website',
            ':package_description',
            'MIT',
            'https://github.com/' . $vendor . '/' . $name,
        ];
        $replace = [
            $author,
            $authorEmail,
            $authorSite,
            $description,
            $license,
            $homepage,
        ];
        $this->helper->replaceAndSave($fullPath . '/composer.json', $search, $replace);
    }
}
