<?php

App::uses('AppShell', 'Console/Command');
App::uses('Folder', 'Utility');
App::uses('File', 'Utility');

/**
 * Use:
 * $ cd {SITE_FOLDER}/app
 * $ ./Console/cake bake_lookup model_database
 */
class BakeLookupShell extends AppShell {

    /**
     * Criação do arquivo lookup no padrao do bmcrm
     */
    public function main() {

        $this->out('Starting Lookup Process');
        $pathTemplate = ROOT . DS . APP_DIR . DS . 'Console' . DS . 'Templates';
        $pathElements = ROOT . DS . APP_DIR . DS . 'View' . DS . 'Elements' . DS . 'admin' . DS . 'lookup';
        $controller = $this->args['0'];

        $this->out('    Moving Template');
        $file = new File($pathTemplate . DS . 'lookup.ctp');
        if ($file->exists()) {
            $file->copy($pathElements . DS . "{$controller}_lookup.ctp");
        }

        $this->out('    Updated Content');
        $filer = new File($pathElements . DS . "{$controller}_lookup.ctp", true, 0644);
        $lookupContent = $filer->read();
        $lookupContent = str_replace('{label}', $this->__labelize($controller), $lookupContent);
        $lookupContent = str_replace('{reference}', $this->__singularize($controller), $lookupContent);
        $lookupContent = str_replace('{model}', $this->__modelize($controller), $lookupContent);
        $lookupContent = str_replace('{modelId}', $this->__modelId($controller), $lookupContent);
        $lookupContent = str_replace('{controller}', $controller, $lookupContent);

        $this->out('    Writing data');
        $filer->write($lookupContent);
        $file->close();

        $this->out('    Closing file');
        $this->out($pathElements . DS . "{$controller}_lookup.ctp Created");
        $this->out('Done');
    }

    /**
     * opportunity_stages to Opportunity Stage
     */
    protected function __labelize($value) {
        $singValue = $this->__singularize($value);
        $quebra = explode('_', $singValue);

        $newValue = '';
        foreach ($quebra as $item) {
            $newValue .= ucfirst($item) . ' ';
        }
        return trim($newValue);
    }

    /**
     * opportunity_stages to opportunity_stage_id
     */
    protected function __modelId($value) {
        return $this->__singularize($value) . '_id';
    }

    /**
     * opportunity_stages to OpportunityStage
     */
    protected function __modelize($value) {
        $singValue = $this->__singularize($value);
        $quebra = explode('_', $singValue);
        $newValue = '';
        foreach ($quebra as $item) {
            $newValue .= ucfirst($item);
        }
        return trim($newValue);
    }

    /**
     * Opportunity_Stages to opportunity_stages
     */
    protected function __lowerize($value) {
        return strtolower($value);
    }

    /**
     * opportunity_stages to opportunity_stage
     */
    protected function __singularize($value) {
        if (substr($value, -3) === 'ies') {
            return substr($value, 0, strlen($value) - 3) . 'y';
        } else if (substr($value, -1) === 's') {
            return substr($value, 0, strlen($value) - 1);
        }
        return $value;
    }

}
