<?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
     * -en,--en 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);

        preg_match_all('/(?<={)\w+(?=})/', $content, $vars);
        $vars = isset($vars[0])?$vars[0]:[];

        foreach ( $vars as $var ) {
            if ( strpos($var, 'var') !== false ) {
                $name = str_replace('var', '', $var);
                $name = lcfirst($name);
                $$var = lcfirst($$name);
                var_dump($var, $$var);
            }

            $content = str_replace("{{$var}}", $$var,$content);

        }

        $dataPath = file_exists($dataPath)?$dataPath.'.gen':$dataPath;

        file_put_contents($dataPath, $content);





    }

    private function generateDao($output,$daoName, $entityName)
    {
        if ( empty($entityName) ) {
            $output->writeln("the entity name 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);

        preg_match_all('/(?<={)\w+(?=})/', $content, $vars);
        $vars = isset($vars[0])?$vars[0]:[];

        foreach ( $vars as $var ) {
            if ( strpos($var, 'var') !== false ) {
                $name = str_replace('var', '', $var);
                $name = lcfirst($name);
                $$var = lcfirst($$name);
                var_dump($var, $$var);
            }

            $content = str_replace("{{$var}}", $$var,$content);

        }

        $daoPath = file_exists($daoPath)?$daoPath.'.gen':$daoPath;
        file_put_contents($daoPath,$content);

    }


    /**
     * 解析模板中变量
     *
     * @param string $content
     * @return string
     */
    private function parseVar(string $content):string
    {
        preg_match_all('/(?<={)\w+(?=})/', $content, $vars);
        $vars = isset($vars[0])?$vars[0]:[];

        foreach ( $vars as $var ) {
            if ( strpos($var, 'var') !== false ) {
                $name = str_replace('var', '', $var);
                $name = lcfirst($name);
                $$var = lcfirst($$name);
                var_dump($var, $$var);
            }

            $content = str_replace("{{$var}}", $$var,$content);

        }

        return $content;
    }
}