<?php
namespace Incoviba\Service\Venta;

use Error;
use Incoviba\Exception\ServiceAction\Read;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Implement;
use Incoviba\Common\Ideal;
use Incoviba\Model;
use Incoviba\Repository;
use Incoviba\Service\Valor;

class FormaPago extends Ideal\Service
{
    public function __construct(LoggerInterface                      $logger,
                                protected Pie                        $pieService,
                                protected BonoPie                    $bonoPieService,
                                protected Credito                    $creditoService,
                                protected Repository\Venta\Escritura $escrituraRepository,
                                protected Subsidio                   $subsidioService,
                                protected Repository\Venta\Abono\Cuota $cuotaRepository,
                                protected Pago                       $pagoService,
                                protected Valor                      $valorService)
    {
        parent::__construct($logger);
    }

    public function getByVenta(int $venta_id): Model\Venta\FormaPago
    {
        $formaPago = new Model\Venta\FormaPago();
        try {
            $formaPago->pie = $this->pieService->getByVenta($venta_id);
        } catch (Read) {}
        try {
            $formaPago->bonoPie = $this->bonoPieService->getByVenta($venta_id);
        } catch (Read) {}
        try {
            $formaPago->credito = $this->creditoService->getByVenta($venta_id);
        } catch (Read) {}
        try {
            $formaPago->escritura = $this->escrituraRepository->fetchByVenta($venta_id);
        } catch (Implement\Exception\EmptyResult) {}
        try {
            $formaPago->subsidio = $this->subsidioService->getByVenta($venta_id);
        } catch (Read) {}
        try {
            $formaPago->devolucion = $this->pagoService->getDevolucionByVenta($venta_id);
        } catch (Read) {}
        try {
            $formaPago->cuotasAbono = $this->cuotaRepository->fetchByVenta($venta_id);
        } catch (Implement\Exception\EmptyResult) {}

        return $formaPago;
    }

    public function add(array $data): Model\Venta\FormaPago
    {
        $fields = [
            'pie',
            'subsidio',
            'credito',
            'bono_pie'
        ];
        $forma_pago = new Model\Venta\FormaPago();
        foreach ($fields as $name) {
            if (isset($data["has_{$name}"])) {
                try {
                    $method = 'add' . str_replace(' ', '', ucwords(str_replace('_', ' ', $name)));
                    $obj = $this->{$method}($data);
                    $forma_pago->{$name} = $obj;
                } catch (Error $error) {
                    $this->logger->critical($error);
                }
            }
        }
        return $forma_pago;
    }

    protected function addPie(array $data): Model\Venta\Pie
    {
        $fields = array_fill_keys([
            'fecha_venta',
            'pie',
            'cuotas',
            'uf'
        ], 0);
        $filtered_data = array_intersect_key($data, $fields);
        $mapped_data = array_combine([
            'fecha',
            'valor',
            'cuotas',
            'uf'
        ], $filtered_data);
        $mapped_data['valor'] = $this->valorService->clean($mapped_data['valor']);
        return $this->pieService->add($mapped_data);
    }
    protected function addSubsidio(array $data): Model\Venta\Subsidio
    {
        $fields = array_fill_keys([
            'fecha_venta',
            'ahorro',
            'subsidio',
            'uf'
        ], 0);
        $filtered_data = array_intersect_key($data, $fields);
        $mapped_data = array_combine([
            'fecha',
            'ahorro',
            'subsidio',
            'uf'
        ], $filtered_data);
        $mapped_data['ahorro'] = $this->valorService->clean($mapped_data['ahorro']);
        $mapped_data['subsidio'] = $this->valorService->clean($mapped_data['subsidio']);
        return $this->subsidioService->add($mapped_data);
    }
    protected function addCredito(array $data): Model\Venta\Credito
    {
        $fields = array_fill_keys([
            'fecha_venta',
            'credito',
            'uf'
        ], 0);
        $filtered_data = array_intersect_key($data, $fields);
        $mapped_data = array_combine([
            'fecha',
            'valor',
            'uf'
        ], $filtered_data);
        $mapped_data['valor'] = $this->valorService->clean($mapped_data['valor']);
        return $this->creditoService->add($mapped_data);
    }
    protected function addBonoPie(array $data): Model\Venta\BonoPie
    {
        $fields = array_fill_keys([
            'fecha_venta',
            'bono_pie'
        ], 0);
        $filtered_data = array_intersect_key($data, $fields);
        $mapped_data = array_combine([
            'fecha',
            'valor'
        ], $filtered_data);
        $mapped_data['valor'] = $this->valorService->clean($mapped_data['valor']);
        return $this->bonoPieService->add($mapped_data);
    }
}
