<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Danoral\Entidades\Entidad;
use App\Danoral\Entidades\EDatoType;

class MigrarEntidadesJSON extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'migrar:entidades-json';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->migrarJSON();
    }

    public function migrarJSON($idESuper = null){
        echo "<--- INICIO" . PHP_EOL;

        /*
        $entidades = Entidad::whereIn("idESuper", function ($query){
            $query->select('idE')
                ->from('admin.entidades_datos')
                ->where([
                    //['typeE', 2],
                    ['typeED', EDatoType::$DATO_TYPE_EDIF_COD],
                    ['valueED', 'like', '%3___'],
                ]);
            })
            ->where([
                ['typeE', 3],
            ])
            ->orderBy('idE', 'asc')
            ->get();
        */

        //aptos de un solo edificio
        if(!is_null($idESuper)) {
            $entidades = Entidad::where([
                ['idESuper', $idESuper],
                ['typeE', 3],
            ])
            ->orWhere([
                ['idE', $idESuper]
            ])
            ->orderBy('idE', 'asc')
            ->get();
        }else{
            //todos las entidades
            //$entidades = Entidad::get();


            //aptos de un solo edificio
            $entidades = Entidad::where([
                ['idESuper', 100100020000000662],
                ['typeE', 3],
            ])
            ->orderBy('idE', 'asc')
            ->get();


            /*
            //solo edidifios
            $entidades = Entidad::where([
                ['typeE', 2],
            ])
            ->orderBy('idE', 'asc')
            ->get();
            */
        }


        $iE = 1;
        $iED = 0;
        $countE = count($entidades);
        echo "entidades: $countE" . PHP_EOL;

        $countPorc = ($countE>20)?$countE/20:1;

        foreach($entidades as $e){

            if(($iE%$countPorc)==0){
                echo "--->" . $iE ." | ". round(($iE*100)/$countE) ."%". PHP_EOL;
            }

            $e->loadDatos();
            $datose = [];
            foreach($e->edatos as $ed){
                $dato = [];

                if($ed->typeED == 1041){
                    $ed->typeED = 1066;
                }

                $dato['type'] = $ed->typeED;

                //oreden apto
                //if($ed->typeED == 1066){
                //    $dato['value'] = str_pad($ed->valueED, 10, " ", STR_PAD_LEFT);
                //}else{

                $dato['value'] = $ed->valueED;

                //}

                if(isset($ed->labelED)) { $dato['label']    = $ed->labelED;  }
                if(isset($ed->dateED))  { $dato['date']     = $ed->dateED;   }
                if(isset($ed->statusED)){ $dato['status']   = $ed->statusED; }

                $edt = EDatoType::firstWhere('typeED', $ed->typeED);

                if(isset($edt->selectorED)){
                    $selector = $edt->selectorED;
                }else{
                    $selector = "s-$ed->typeED";
                }

                $datose[$selector] = $dato;

                $iED++;
            }

            
            $datose_json = json_encode($datose, JSON_UNESCAPED_UNICODE);

            //print_r($datose);
            //print_r($datose_json);

            $e->datose = $datose_json;
            $e->save();

            $iE++;
            //break;
        };

        echo "<--- FIN | $iE | $iED " . PHP_EOL;

    }
}
