<?php

namespace App\Tools\Commands\Make;

use App\Tools\Commands\Make\GeneratorCommand;
use Illuminate\Support\Str;
use App\Tools\Support\Config\GenerateConfigReader;
use App\Tools\Support\Stub;
use App\Tools\Traits\ModuleCommandTrait;
use Symfony\Component\Console\Input\InputArgument;

class ObserverMakeCommand extends GeneratorCommand
{
    use ModuleCommandTrait;

    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'plugin:make-observer';

    /**
     * The name of argument name.
     *
     * @var string
     */
    protected $argumentName = 'name';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create a new observer for the specified plugin.';

    /**
     * Get the console command arguments.
     *
     * @return array
     */
    protected function getArguments()
    {
        return [
            ['name', InputArgument::REQUIRED, 'The observer name will be created.'],
            ['plugin', InputArgument::OPTIONAL, 'The name of plugin will be created.'],
        ];
    }

    /**
     * @return mixed
     */
    protected function getTemplateContents()
    {
        $module = $this->laravel['plugins']->findOrFail($this->getModuleName());

        return (new Stub('/observer.stub', [
                'NAMESPACE' => $this->getClassNamespace($module),
                'NAME' => $this->getModelName(),
                'MODEL_NAMESPACE' => $this->getModelNamespace(),
                'NAME_VARIABLE' => $this->getModelVariable(),
            ]))->render();
    }

    /**
     * Get model namespace.
     *
     * @return string
     */
    public function getModelNamespace(): string
    {
        $path = $this->laravel['plugins']->config('paths.generator.model.path', 'Entities');

        $path = str_replace('/', '\\', $path);

        return $this->laravel['plugins']->config('namespace') . '\\' . $this->laravel['plugins']->findOrFail($this->getModuleName()) . '\\' . $path;
    }

    /**
     * @return mixed|string
     */
    private function getModelName()
    {
        return Str::studly($this->argument('name'));
    }

    /**
     *  @return mixed|string
     */
    private function getModelVariable(): string
    {
        return '$' . Str::lower($this->argument('name'));
    }

    /**
     * @return mixed
     */
    protected function getDestinationFilePath()
    {
        $path = $this->laravel['plugins']->getModulePath($this->getModuleName());

        $observerPath = GenerateConfigReader::read('observer');

        return $path . $observerPath->getPath() . '/' . $this->getFileName();
    }

    /**
     * @return string
     */
    private function getFileName()
    {
        return Str::studly($this->argument('name')) . 'Observer.php';
    }

    public function handle(): int
    {
        $this->components->info('Creating observer...');

        parent::handle();

        return 0;
    }

    /**
     * Get default namespace.
     *
     * @return string
     */
    public function getDefaultNamespace(): string
    {
        return config('plugins.paths.generator.observer.namespace')
            ?? ltrim(config('plugins.paths.generator.observer.path', 'Observers'), config('plugins.paths.app_folder', ''));
    }
}
