<?php
/**
 * This file is part of Swoft.
 *
 * @link https://swoft.org
 * @document https://doc.swoft.org
 * @contact group@swoft.org
 * @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
 */

namespace App\Commands\Common;

use Swoft\Console\Bean\Annotation\Command;
use Swoft\Console\Bean\Annotation\Mapping;
use Swoft\Console\Input\Input;
use Swoft\Console\Output\Output;

/**
 * generate dao,data layer commands
 *
 * @Command(coroutine=false)
 */
class GenerateCommand
{
    /**
     * this generate command
     *
     * @Usage
     * generate:dao [options]
     *
     * @Options
     * -n,--n the dao name
     * -e,--e the entity name
     *
     * @Example
     * php swoft generate:dao -e entityName [-n daoName]
     *
     * @param Input  $input
     * @param Output $output
     *
     * @Mapping("dao")
     */
    public function Dao(Input $input, Output $output)
    {
        $className    = $input->getOpt('n');
        $entityName   = $input->getOpt('e');

        $this->generateDao($output, $className, $entityName);
    }

    /**
     * this generate command
     *
     * @Usage
     * generate:all [options]
     *
     * @Options
     * -n,--n the dao name
     * -e,--e the entity name
     *
     * @Example
     * php swoft generate:dao -e entityName [-n dataName] [--dao-name daoName]
     *
     * @param Input  $input
     * @param Output $output
     *
     * @Mapping("all")
     */
    public function all(Input $input, Output $output)
    {
        $dataName    = $input->getOpt('n');
        $entityName   = $input->getOpt('e');
        $daoName   = $input->getOpt('dao-name');

        $appPath = alias('@app');
        $dataName = empty($dataName)?$entityName.'Data':$dataName;

        //generateDao
        $daoName = empty($daoName)?$entityName.'Dao':$daoName;
        $this->generateDao($output, $daoName, $entityName);

        //generateData
        $dataPath = $appPath.'/Models/Data/'.$dataName.'.php';
        $templatePath = $appPath.'/Commands/Common/ClassTemplate/Data/ClassTemplate';
        $content = file_get_contents($templatePath);
        $content = str_replace('{dataName}', $dataName, $content);
        $content = str_replace("{daoName}", $daoName, $content);
        $content = str_replace("{varDaoName}", lcfirst($daoName), $content);

        file_put_contents($dataPath, $content);





    }

    private function generateDao($output,$daoName, $entityName)
    {
        if ( empty($entityName) ) {
            $output->writeln("the en option is required", true, true);
        }

        $daoName = empty($daoName)?$entityName.'Dao':$daoName;

        $daoPath =   alias("@app").'/Models/Dao/'.$daoName.'.php';

        $templatePath = alias("@app").'/Commands/Common/ClassTemplate/Dao/ClassTemplate';

        $content = file_get_contents($templatePath);

        $content = str_replace("{className}", $daoName,$content);

        $content = str_replace("{entityName}", $entityName, $content);
        $content = str_replace("{varEntityName}", lcfirst($entityName), $content);

        file_put_contents($daoPath,$content);

    }
}