<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
define("ROOT_PATH",dirname(dirname(dirname(__DIR__))));
class createModule extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'module:create {name} {module?} {app?}';
    protected $root=ROOT_PATH;

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'create module or model or controller or lib ';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $proj=$this->argument('name');
        switch($proj){
            case "module":
                $this->cdir();                
                break;
            case "controller":
                $this->cController();
                break;
            case "model":
                $this->cModel();
                break;
            case "lib":
                $this->cLib();
                break;
            default:
                break;
        }
       
    }
    protected function cLib(){
        $app=$this->argument('module');
        $app=ucfirst($app);
        $module=$this->root."/module/";
        $path=$module."/Lib"."/";
        if(!File::exists($path)){
            File::makeDirectory($path);
        }          
        $path=$module."/Lib"."/".ucfirst($app).".php";
        $content="<?php ".chr(9).chr(13).
        "namespace Module\\Lib;".chr(9).chr(13).
        "class ".ucfirst($app)." {".chr(9).chr(13).                     
        "}";
        File::put($path,$content);
    }
    protected function cController()
    {
        $proj=$this->argument('module');
        $app=$this->argument('app');
        $proj=ucfirst($proj);
        $module=$this->root."/module/".$proj."/";       
        $path=$module."/Controller"."/".ucfirst($app).".php";
        $content="<?php ".chr(9).chr(13).
        "namespace Module\\$proj\Controller;".chr(9).chr(13).
        "class ".ucfirst($app)." extends BaseController{".chr(9).chr(13).   
            "  function _init(){".chr(9).chr(13).
            "  }".chr(9).chr(13).         
        "}";
        File::put($path,$content);
    }

    protected function cBaseController()
    {
        $proj=$this->argument('module');
        $proj=ucfirst($proj);
        $module=$this->root."/module/".$proj."/";       
        $path=$module."/Controller"."/BaseController.php";
        $content="<?php".chr(9).chr(13).
        "namespace Module\\$proj\Controller;".chr(9).chr(13).
        "class BaseController extends \\Module\\Controller{".chr(9).chr(13).     
        "  function _init(){".chr(9).chr(13).
        "  }".chr(9).chr(13).
        "  function __construct(){".chr(9).chr(13).
        "      \$this->_init();".chr(9).chr(13).
        "  }".chr(9).chr(13).
        "}";
        File::put($path,$content);
    }

    protected function cBaseModel()
    {
        $proj=$this->argument('module');
        $proj=ucfirst($proj);
        $module=$this->root."/module/".$proj."/";       
        $path=$module."/Model"."/BaseModel.php";
        $content="<?php".chr(9).chr(13).
        "namespace Module\\$proj\Model;".chr(9).chr(13).
        "class BaseModel extends \Illuminate\Database\Eloquent\Model{".chr(9).chr(13).            
        "}";
        File::put($path,$content);
    }
    protected function cModel()
    {
        $proj=$this->argument('module');
        $model=$this->argument('app');
        $proj=ucfirst($proj);
        $model=ucfirst($model);
        $module=$this->root."/module/".$proj."/";       
        $path=$module."/Model"."/$model.php";
        $content="<?php".chr(9).chr(13).
        "namespace Module\\$proj\Model;".chr(9).chr(13).
        "class $model extends BaseModel{".chr(9).chr(13).            
        "}";
        File::put($path,$content);
    }

    protected function cdir()
    {
        $proj=$this->argument('module');
        $proj=ucfirst($proj);
        $module=$this->root."/module/".$proj."/";
        File::makeDirectory($module);
        $path=$module."/Model"."/";
        File::makeDirectory($path);
        $path=$module."/Controller"."/";
        File::makeDirectory($path);
        $path=$this->root."/resources/views/".lcfirst($proj)."/";
        File::makeDirectory($path);
        $this->cBaseController();
        $this->cBaseModel();
    }

}
