<?php

namespace App\GO;

use App\Danoral\ModelBase;

use App\Danoral\Condominios\Condominio;
use App\Danoral\Condominios\Recibo;
use App\Danoral\Entidades\Entidad;

class Token extends ModelBase
{
    public static $TYPE_CUOTA_CONDOMINIO = 1001;

    protected $table = 'go.tokens';
    protected $primaryKey = 'token';
    public $incrementing = false;
    public $timestamps = false;

    protected $fillable = [
        'token',
        'typeToken',
        'idElemento',
        'idE',
        'ano',        
        'mes',
        'activo',
    ];

    public static function getGO($req) {

        $tokenString    = $req['token'];
        $ipAddress      = $req['ipAddress'];

        if(strlen($tokenString) != 6) {
            return "<h1> longitud invalida: $tokenString </h1>";
        }

        $tl = TokenLog::create([
            'dateTL'    => date("Y-m-d H:i:s"),
            'token'     => $tokenString,
            'ipTL'      => $ipAddress,
        ]);

        if($token = Token::find($tokenString)){

            switch($token->typeToken) {
                case Token::$TYPE_CUOTA_CONDOMINIO:
                    return (new Recibo($token->idE, null, $token->ano, $token->mes))->getRecibo();

                default:
                    return "<h1>ok: [ $token->token ] $token->idE | $token->typeToken</h1>";
            }

        }
        return "<h1>go: [ $tokenString ] $ipAddress </h1>";
    }

    /** 
     * 62 digitos
     * C(62,6) = 99795696 
     */
    public static function generateStringToken($strength = 6) {
        $permitted_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

        $input_length = strlen($permitted_chars);

        do {
            $random_string = '';
            for($i = 0; $i < $strength; $i++) {
                $random_character = $permitted_chars[mt_rand(0, $input_length - 1)];
                $random_string .= $random_character;
            }

            $token = Token::find($random_string);

        } while($token);

        return $random_string;
    }

    public static function generarTokensCondominios($ano, $mes){

        $condominios = Condominio::Where([
                ['anoCond', $ano],
                ['mesCond', $mes],
            ])
            ->orderBy('idE', 'asc')
            ->get();

        $iCond = 0;    
        $iApto = 0;
        foreach($condominios as $cond){

            $edif = Entidad::find($cond->idE);

            if($edif){
                $datosEdif = $edif->getDatosE();   

                echo ++$iCond . ' | ' . $datosEdif->cod->value . PHP_EOL;
                
                $edif->loadSub();
                
                foreach($edif->subE as $a) {
                    $datosApto = $a->getDatosE();

                    echo '... ' . ++$iApto . ' | ' . $datosEdif->cod->value . ' | ' . $datosApto->cod->value;

                    $token = Token::firstOrCreate([
                        'typeToken'     => Token::$TYPE_CUOTA_CONDOMINIO,
                        'idE'           => $a->idE,
                        'ano'           => $cond->anoCond,
                        'mes'           => $cond->mesCond,
                    ],[
                        'token'         => Token::generateStringToken(),
                        'idElemento'    => null,
                        'activo'        => true,
                    ]);

                    echo ' [ ' . $token->token . ' ]' . PHP_EOL;
                }
            }
        }
    }
}