<?php
namespace Incoviba\Service;

use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Model;
use Incoviba\Repository;

class Search
{
    public function __construct(
        protected Proyecto $proyectoService,
        protected Venta $ventaService,
        protected Repository\Venta $ventaRepository,
        protected Repository\Venta\Unidad $unidadRepository,
        protected Repository\Proyecto\TipoUnidad $tipoUnidadRepository,
        protected Repository\Venta\Pie $pieRepository,
        protected Repository\Venta\Pago $pagoRepository,
        protected Repository\Venta\Cuota $cuotaRepository,
        protected Repository\Venta\Credito $creditoRepository,
        protected Repository\Venta\Escritura $escrituraRepository,
        protected Repository\Venta\Subsidio $subsidioRepository,
        protected Repository\Venta\BonoPie $bonoPieRepository
    ) {}

    public function query(string $query, string $tipo): array
    {
        if ($tipo === '*') {
            $results = $this->findCualquiera($query);
        } else {
            $results = $this->find($query, $tipo);
        }
        return $results;
        //return $this->sort($results);
    }

    protected function findCualquiera(string $query): array
    {
        $tipos = [
            'departamento',
            'estacionamiento',
            'bodega',
            'propietario',
            'precio_venta',
            'proyecto',
            'pago',
            'unidad'
        ];
        $results = [];
        foreach ($tipos as $t) {
            $this->add($results, $this->find($query, $t));
        }
        return $results;
    }
    protected function find(string $query, string $tipo): array
    {
        preg_match_all('/["\']([\s\w\-]+)["\']|([\w\-]+)/iu', $query, $matches, PREG_SET_ORDER | PREG_UNMATCHED_AS_NULL);
        $queries = array_map(function($match) {
            array_shift($match);
            $valid = array_filter($match, function($line) {return $line !== null and trim($line); });
            return implode(' ', $valid);
        }, $matches);
        $tiposUnidades = $this->getTiposUnidades();
        $results = [];
        foreach ($queries as $q) {
            $this->add($results, $this->findVentas($q, $tipo));
            if (in_array($tipo, $tiposUnidades)) {
                $disponibles =  $this->findUnidadesDisponibles($q, $tipo);
                if (count($disponibles) === 0) {
                    continue;
                }
                $this->add($results, $disponibles, false);
            }
        }
        return $results;
    }
    protected function findVentas(string $query, string $tipo): array
    {
        $tiposUnidades = $this->getTiposUnidades();
        if (in_array($tipo, $tiposUnidades)) {
            return $this->findUnidad($query, $tipo);
        }
        switch ($tipo) {
            case 'unidad':
                $results = [];
                foreach ($tiposUnidades as $t) {
                    $this->add($results, $this->findVentas($query, $t));
                }
                return $results;
            case 'propietario':
                return $this->findPropietario($query);
            case 'precio_venta':
                return $this->findPrecio($query);
            case 'proyecto':
                return $this->findProyecto($query);
            case 'pago':
                return $this->findPago($query);
        }
        return [];
    }
    protected function findUnidadesDisponibles(string $query, string $tipo): array
    {
        try {
            return $this->unidadRepository->fetchDisponiblesIdsByDescripcionAndTipo($query, $tipo);
        } catch (EmptyResult) {
            return [];
        }
    }
    protected function findUnidad(string $query, string $tipo): array
    {
        try {
            return $this->ventaRepository->fetchIdsByUnidad($query, $tipo);
        } catch (EmptyResult) {
            return [];
        }
    }
    protected function findPropietario(string $query): array
    {
        try {
            return $this->ventaRepository->fetchIdsByPropietario($query);
        } catch (EmptyResult) {
            return [];
        }
    }
    protected function findPrecio(string $query): array
    {
        try {
            $precio = str_replace(['$', '.', ','], ['', '', '.'], $query);
            return $this->ventaRepository->fetchIdsByPrecio($precio);
        } catch (EmptyResult) {
            return [];
        }
    }
    protected function findProyecto(string $query): array
    {
        try {
            $proyecto = $this->proyectoService->getByName($query);
            return $this->ventaRepository->fetchIdsByProyecto($proyecto->id);
        } catch (EmptyResult) {
            return [];
        }
    }
    protected function findPago(string $query): array
    {
        if ((int) $query === 0) {
            return [];
        }
        $methods = [
            'findPie',
            'findEscritura',
            'findSubsidio',
            'findCredito',
            'findPromociones',
        ];
        $valor = str_replace(['$', '.', ','], ['', '', '.'], $query);
        $pagos = [];
        try {
            $pagos = $this->pagoRepository->fetchByValue((int) $valor);
        } catch (EmptyResult) {}
        $results = [];
        foreach ($methods as $method) {
            $this->add($results, $this->{$method}($valor, $pagos));
        }
        return $results;
    }
    protected function findPie(string $query, array $pagos): array
    {
        $results = [];
        try {
            $pies = $this->pieRepository->fetchByValue((float) $query);
            foreach ($pies as $pie) {
                try {
                    $this->add($results, [$this->ventaRepository->fetchIdByPie($pie->id)]);
                } catch (EmptyResult) {}
            }
        } catch (EmptyResult) {}
        $this->add($results, $this->findReajuste($query, $pagos));
        $this->add($results, $this->findCuota($query, $pagos));
        return $results;
    }
    protected function findReajuste(string $query, array $pagos): array
    {
        $results = [];
        foreach ($pagos as $pago) {
            try {
                $pie = $this->pieRepository->fetchByReajuste((int) $pago->id);
                $this->add($results, [$this->ventaRepository->fetchIdByPie($pie->id)]);
            } catch (EmptyResult) {}
        }
        return $results;
    }
    protected function findCuota(string $query, array $pagos): array
    {
        $results = [];
        foreach ($pagos as $pago) {
            try {
                $cuota = $this->cuotaRepository->fetchByPago($pago->id);
                $this->add($results, [$this->ventaRepository->fetchIdByPie($cuota->pie->id)]);
            } catch (EmptyResult) {}
        }
        return $results;
    }
    protected function findEscritura(string $query, array $pagos): array
    {
        $results = [];
        try {
            $escrituras = $this->escrituraRepository->fetchByValue((int) $query);
            foreach ($escrituras as $escritura) {
                try {
                    $this->add($results, [$this->ventaRepository->fetchIdByEscritura($escritura->id)]);
                } catch (EmptyResult) {}
            }
        } catch (EmptyResult) {}
        foreach ($pagos as $pago) {
            try {
                $escritura = $this->escrituraRepository->fetchByPago($pago->id);
                $this->add($results, [$this->ventaRepository->fetchIdByEscritura($escritura->id)]);
            } catch (EmptyResult) {}
        }
        return $results;
    }
    protected function findSubsidio(string $query, array $pagos): array
    {
        $results = [];
        foreach ($pagos as $pago) {
            try {
                $subsidio = $this->subsidioRepository->fetchByPago($pago->id);
                $this->add($results, [$this->ventaRepository->fetchIdBySubsidio($subsidio->id)]);
            } catch (EmptyResult) {}
        }
        return $results;
    }
    protected function findCredito(string $query, array $pagos): array
    {
        $results = [];
        try {
            $creditos = $this->creditoRepository->fetchByValue((int) $query);
            foreach ($creditos as $credito) {
                try {
                    $this->add($results, [$this->ventaRepository->fetchIdByCredito($credito->id)]);
                } catch (EmptyResult) {}
            }
        } catch (EmptyResult) {}
        foreach ($pagos as $pago) {
            try {
                $credito = $this->creditoRepository->fetchByPago($pago->id);
                $this->add($results, [$this->ventaRepository->fetchIdByCredito($credito->id)]);
            } catch (EmptyResult) {}
        }
        return $results;
    }
    protected function findPromociones(string $query, array $pagos): array
    {
        $results = [];
        $this->add($results, $this->findBonoPie($query, $pagos));
        return $results;
    }
    protected function findBonoPie(string $query, array $pagos): array
    {
        $results = [];
        try {
            $bonos = $this->bonoPieRepository->fetchByValue((float) $query);
            foreach ($bonos as $bono) {
                try {
                    $this->add($results, [$this->ventaRepository->fetchIdByBono($bono->id)]);
                } catch (EmptyResult) {}
            }
        } catch (EmptyResult) {}
        foreach ($pagos as $pago) {
            try {
                $bono = $this->bonoPieRepository->fetchByPago($pago->id);
                $this->add($results, [$this->ventaRepository->fetchIdByBono($bono->id)]);
            } catch (EmptyResult) {}
        }
        return $results;
    }

    protected array $tipos;
    protected function getTiposUnidades(): array
    {
        if (!isset($this->tipos)) {
            $this->tipos = array_map(function(Model\Proyecto\TipoUnidad $tipoUnidad) {
                return $tipoUnidad->descripcion;
            }, $this->tipoUnidadRepository->fetchAll());
        }
        return $this->tipos;
    }
    protected function add(array &$results, array $found, bool $is_venta = true): void
    {
        foreach ($found as $item) {
            if (!isset($item['tipo'])) {
                $item['tipo'] = ($is_venta) ? 'venta' : 'unidad';
            }
            if (!$this->inResults($item, $results)) {
                $results []= $item;
            }
        }
    }
    protected function inResults($item, array $results): bool
    {
        return in_array($item, $results, true);
    }
    protected function sort(&$results): array
    {
        usort($results, function($a, $b) {
            if (is_a($a, Model\Venta::class)) {
                $pa = $a->proyecto()->descripcion;
                $ta = $a->propiedad()->departamentos()[0]->proyectoTipoUnidad->tipoUnidad->descripcion;
                $ua = $a->propiedad()->departamentos()[0]->descripcion;
            } else {
                $pa = $a->proyectoTipoUnidad->proyecto->descripcion;
                $ta = $a->proyectoTipoUnidad->tipoUnidad->descripcion;
                $ua = $a->descripcion;
            }
            if (is_a($b, Model\Venta::class)) {
                $pb = $b->proyecto()->descripcion;
                $tb = $b->propiedad()->departamentos()[0]->proyectoTipoUnidad->tipoUnidad->descripcion;
                $ub = $b->propiedad()->departamentos()[0]->descripcion;
            } else {
                $pb = $b->proyectoTipoUnidad->proyecto->descripcion;
                $tb = $b->proyectoTipoUnidad->tipoUnidad->descripcion;
                $ub = $b->descripcion;
            }
            $p = strcmp($pa, $pb);
            if ($p !== 0) {
                return $p;
            }
            $t = strcmp($ta, $tb);
            if ($t !== 0) {
                return $t;
            }
            return strcmp($ua, $ub);
        });
        return $results;
    }
}
