<?php


namespace App\Console\Commands;


use Illuminate\Filesystem\Filesystem;
use Symfony\Component\Console\Input\InputArgument;

class MakeTestCommand extends \Illuminate\Foundation\Console\TestMakeCommand
{
    protected $signature = 'make:test {name : The name of the class}
    {--unit : Create a unit test}
    {--controller : Create a controller test}
    {--controllers : Create a controllers test}
    {--service : Create a service test}
    {--services : Create a services test}
    ';
    protected function getDefaultNamespace($rootNamespace)
    {
        if ($this->option('controller')
            ||$this->option('controllers')) {
            return $rootNamespace.'\Controllers';
        }
        if ($this->isForService()) {
            return $rootNamespace.'\Services\\'
                .$this->getServiceName()
                ;
        }
        if ($this->option('unit')) {
            return $rootNamespace.'\Unit';
        } else {
            return $rootNamespace.'\Feature';
        }
    }

    protected function isForService()
    {
        if ($this->option('services')
            ||$this->option('service')) {
            return true;
        }
        return false;
    }
    protected function getNameInput()
    {
        $input= trim($this->argument('name'));
        if(!$this->isForService()){
            return $input;
        }
        $inputs = explode(':', $input);
        return $inputs[1]
            ?$this->getMethodName()
            :$inputs[0]
            ;
    }
    protected function getServiceName()
    {
        $input= trim($this->argument('name'));
        if(!$this->isForService()){
            return $input;
        }
        return ucfirst(explode(':',$input)[0]);
    }
    protected function getModelName()
    {
        $input= trim($this->argument('name'));
        if(!$this->isForService()){
            return $input;
        }
        return str_replace('Service','',(explode(':',$input)[0]));
    }
    protected function getModelNamePlural()
    {
        $modelName=preg_replace('/(s|x|ch|sh)$/','$1es',lcfirst($this->getModelName()));
        $modelName=preg_replace('/y$/','ies',$modelName);
        $modelName=preg_replace('/[cC]hild$/','children',$modelName);
        if(preg_match('/[cC]hildren$/',$modelName)==0){
            $modelName=preg_replace('/([^s])$/','$1s',$modelName);
        }
        return $modelName;
    }
    protected function getMethodName()
    {
        $input= trim($this->argument('name'));
        if(!$this->isForService()){
            return $input;
        }
        return ucfirst(explode(':',$input)[1]).'Test';
    }

    protected function getStub()
    {
        if ($this->option('controller')
            ||$this->option('controllers')) {
            return __DIR__.('/stubs/test.controller.stub');
        }
        if ($this->option('services')
            ||$this->option('service')) {
            return __DIR__.('/stubs/test.service.stub');
        }
        return $this->option('unit')
            ? $this->resolveStubPath('/stubs/test.unit.stub')
            : $this->resolveStubPath('/stubs/test.stub');
    }

    protected function replaceClass($stub, $name)
    {
        $class = str_replace($this->getNamespace($name).'\\', '', $this->getMethodName());
        $class = str_replace('\\', '', $class);
        $stub = str_replace(['{{ serviceName }}', '{{serviceName}}'], $this->getServiceName(), $stub);
        $stub = str_replace(['{{ modelName }}', '{{modelName}}'], $this->getModelName(), $stub);
        $stub = str_replace(['{{ modelNameUc }}', '{{modelNameUc}}'], ucfirst($this->getModelName()), $stub);
        $stub = str_replace(['{{ modelNamePlural }}', '{{modelNamePlural}}'], lcfirst($this->getModelNamePlural()), $stub);

        return str_replace(['DummyClass', '{{ class }}', '{{class}}'], $class, $stub);
    }
}
