<?php

namespace App\Http\Controllers\Api\Condominios;

use Illuminate\Http\Request;
use App\Danoral\Entidades\Entidad;
use App\Danoral\Entidades\EDatoType;
use App\Danoral\Dolarizacion\TasaCambio;
use App\Danoral\Condominios\Condominio;
use App\Danoral\Condominios\CondominioItems;
use App\Http\Controllers\Controller;
use App\Http\Resources\Condominio as CondominioResources;

class CondominioController extends Controller
{
    protected $cond;

    public function __construct(Condominio $cond){
        $this->cond = $cond;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        
        $cond = new Condominio;
        if(isset($request->idCond)){
            $cond->idCond = $request->idCond;
            
        }else{
            if(!isset($request->idE)){
                return response()->json('idE: no suministrado' , 400);

            }else{
                if(isset($request->anoCond) && isset($request->mesCond)){
                    $cond->idE = $request->idE;
                    $cond->anoCond = $request->anoCond;
                    $cond->mesCond = $request->mesCond;
                }else{
                    return response()->json(
                        CondominioResources::Collection(Condominio::getCondominios( $request->idE )),
                        200
                    );
                }

            }
        }

        $cond = Condominio::getCondominio($cond);
            
        if($cond){
            return response()->json(new CondominioResources($cond), 200);
        }else{
            return response()->json(null, 200);
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $cond = Condominio::loadCond($request);
        return response()->json(new CondominioResources($cond));
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Danoral\Condominios\Condominio  $cond
     * @return \Illuminate\Http\Response
     */
    public function show(Condominio $cond)
    {
        return response()->json(new CondominioResources($cond));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Danoral\Condominios\Condominio  $cond
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Condominio $cond)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Danoral\Condominios\Condominio  $cond
     * @return \Illuminate\Http\Response
     */
    public function destroy(Condominio $cond)
    {
        //
    }

    public function getCondominioItems(Request $request){
        $idE                = $request->idE;
        $anoCond            = $request->anoCond;
        $mesCond            = $request->mesCond;
        $typeEmisionCond    = $request->typeEmisionCond;
        $tcEmision_date     = $request->tcEmision_date;
        $tcEmision_dateVen  = $request->tcEmision_dateVen;

        return response()->json($request, 200);
    }

    public function generarCondominio(Request $request){

        $idE                = $request->idE;
        $anoCond            = $request->anoCond;
        $mesCond            = $request->mesCond;
        $typeEmisionCond    = $request->typeEmisionCond;
        $tcEmision_date     = $request->tcEmisionDateSTR;
        $tcEmision_dateVen  = $request->tcEmisionDateVenSTR;
        $isDegenerar        = $request->isDegenerar;
        $saveItemsCond      = isset($request->saveItemsCond)?$request->saveItemsCond:true;
        $updateMontoOrigen  = isset($request->updateMontoOrigen)?$request->updateMontoOrigen:false;

        $edif = Entidad::find($idE);

        $edif->loadDatos();
        $dolarizado = $edif->getDato(EDatoType::$DATO_TYPE_EDIF_DOLARIZADO);

        switch($dolarizado){
            case 1: //CPD:si | Mix:no (tipo:Camurimar)
                $isCPD   = true;
                $isMixto = false;
                break;
            case 2: //CPD:si | Mix:si (tipo:Macaira)
                $isCPD   = true;
                $isMixto = true;
                break;
            case 3: //CPD:no | Mix:si (tipo:Altosol)
                $isCPD   = false;
                $isMixto = true;
                break;
        }

        $tc = TasaCambio::getTasaFecha($tcEmision_date);

        $res = 'INICIO | ';
        $resList = [];
        $resList_migrar = [];
        $resList_generar = [];
        if($isDegenerar){
            $dataCond = [
                'idE'               => $idE,
                'typeEmisionCond'   => $typeEmisionCond,
                'anoCond'           => $anoCond,
                'mesCond'           => $mesCond,
                'refCond'           => null,
            ];            
            $res .= 'DEGENERAR | ';
            $resList = Condominio::degenerarCondominio($dataCond);
        }else{
            $dataCond = [
                'idE'               => $idE,
                'typeEmisionCond'   => $typeEmisionCond,
                'anoCond'           => $anoCond,
                'mesCond'           => $mesCond,
                'refCond'           => null,
                'idTC'              => $tc->idTC, //Tasa Cambio
                'isCPD'             => $isCPD,    //genera la tras 230 para cancelar los Bs
                'isMixto'           => $isMixto,  //contiene dos cuotas ($ Bs)
                'tcEmision_dateVen' => $tcEmision_dateVen,
            ];
            
            $res .= 'MIGRAR | '. PHP_EOL;
            $resList_migrar =  Condominio::migrarGastosCond($dataCond, $saveItemsCond, $updateMontoOrigen);
            $res .= 'GENERAR | '. PHP_EOL;
            if($saveItemsCond) {
                $resList_generar = Condominio::generarCondominio($dataCond);
            }else{
                $res .= '.... No generado: modo NO GUARDAR activo';
            }
        }
        $res .= '| FIN';
        $resList = array_merge($resList, $resList_migrar, $resList_generar);

        return response()->json([
            'res'       => $res,
            'list'      => $resList,
        ],200);
    }

    public function getCondominiosDolarizados(Request $request){
        $res = Condominio::getCondominiosDolarizados($request->ano, $request->mes);
        return response()->json(CondominioResources::collection($res), 200);
    }

    public function getCondominioItems_jsys(Request $request){
        $cond = new Condominio;
        $cond->idE = $request->idE;
        $cond->anoCond = $request->anoCond;
        $cond->mesCond = $request->mesCond;

        //echo $cond->anoCond . " " . $cond->mesCond;

        //return response()->json($request, 200);

        $res = CondominioItems::getCondominioItems_jsys($cond);
        return response()->json($res, 200);
    }

}
