<?php

namespace App\Services;

use App\Services\Service;
use App\Services\ServiceResponse\ServiceResponse;
use App\Services\ServiceResponse\ServiceResponseInterface;
use App\ViewModels\CreditoCliente\CreditoClienteRegistrationViewModel\CreditoClienteRegistrationViewModel;
use App\DAO\CreditoClienteDAO;
use App\DAO\DAOResponse\DAOResponseInterface;
use App\ViewModels\CreditoCliente\CreditoClienteUpdateViewModel\CreditoClienteUpdateViewModel;

class CreditoClienteService extends Service {

	public function registerFromExtorno(CreditoClienteRegistrationViewModel $creditoCliente_vm): ServiceResponse {

		$service_response = new ServiceResponse();
		$creditoCliente_dao = new CreditoClienteDAO();
		$dao_response = $creditoCliente_dao->regiterFromExtorno( $creditoCliente_vm );

		switch ($dao_response->getStatus()) {
			case DAOResponseInterface::SUCESS :
				$service_response->setStatus( ServiceResponseInterface::SUCESS );
				$service_response->setData( $dao_response->getData() );
				break;
			case DAOResponseInterface::RECORD_NOT_FOUND :
				$service_response->setStatus( ServiceResponseInterface::RECORD_NOT_FOUND );
				$service_response->setErrors( $dao_response->getErrors() );
				break;
			case DAOResponseInterface::DATABASE_CONNECTION_ERROR :
				$service_response->setStatus( ServiceResponseInterface::DATABASE_CONNECTION_ERROR );
				$service_response->setErrors( $dao_response->getErrors() );
				break;
			default :
				$service_response->setStatus( ServiceResponseInterface::UNKNOWN_ERROR );
				$service_response->setErrors( $dao_response->getErrors() );
				break;
		}

		return $service_response;

	}

	public function registerFromCredito(CreditoClienteRegistrationViewModel $creditoCliente_vm): ServiceResponse {

		$service_response = new ServiceResponse();
		$creditoCliente_dao = new CreditoClienteDAO();
		$dao_response = $creditoCliente_dao->regiterFromCredito( $creditoCliente_vm );

		switch ($dao_response->getStatus()) {
			case DAOResponseInterface::SUCESS :
				$service_response->setStatus( ServiceResponseInterface::SUCESS );
				$service_response->setData( $dao_response->getData() );
				break;
			case DAOResponseInterface::RECORD_NOT_FOUND :
				$service_response->setStatus( ServiceResponseInterface::RECORD_NOT_FOUND );
				$service_response->setErrors( $dao_response->getErrors() );
				break;
			case DAOResponseInterface::DATABASE_CONNECTION_ERROR :
				$service_response->setStatus( ServiceResponseInterface::DATABASE_CONNECTION_ERROR );
				$service_response->setErrors( $dao_response->getErrors() );
				break;
			default :
				$service_response->setStatus( ServiceResponseInterface::UNKNOWN_ERROR );
				$service_response->setErrors( $dao_response->getErrors() );
				break;
		}

		return $service_response;

	}

	public function update(CreditoClienteUpdateViewModel $creditoCliente_vm): ServiceResponse {

		$service_response = new ServiceResponse();
		$creditoCliente_dao = new CreditoClienteDAO();
		$dao_response = $creditoCliente_dao->regiterFromExtorno( $creditoCliente_vm );

		switch ($dao_response->getStatus()) {
			case DAOResponseInterface::SUCESS :
				$service_response->setStatus( ServiceResponseInterface::SUCESS );
				$service_response->setData( $dao_response->getData() );
				break;
			case DAOResponseInterface::RECORD_NOT_FOUND :
				$service_response->setStatus( ServiceResponseInterface::RECORD_NOT_FOUND );
				$service_response->setErrors( $dao_response->getErrors() );
				break;
			case DAOResponseInterface::DATABASE_CONNECTION_ERROR :
				$service_response->setStatus( ServiceResponseInterface::DATABASE_CONNECTION_ERROR );
				$service_response->setErrors( $dao_response->getErrors() );
				break;
			default :
				$service_response->setStatus( ServiceResponseInterface::UNKNOWN_ERROR );
				$service_response->setErrors( $dao_response->getErrors() );
				break;
		}

		return $service_response;

	}

	public function getCreditoByClienteIdByUnidadeId(int $clienteid, int $unidadeid, int $usuarioid): ServiceResponse {

		$service_response = new ServiceResponse();
		$unidade_service = new UnidadeService();
		$creditoCliente_dao = new CreditoClienteDAO();

		$unidade_service_response = $unidade_service->getUnidadesToUsuarioId( $usuarioid );

		if ($unidade_service_response->getStatus() == ServiceResponseInterface::SUCESS) {
			$dados = $unidade_service_response->getData();
			$unidades = $dados ['unidades'];
			if ($this->isPermitido( $unidadeid, $unidades )) {
				$dao_response_list = $creditoCliente_dao->getCreditoByClienteIdByUnidadeId( $clienteid, $unidadeid );
				$data_list = $dao_response_list->getData();
				if (count( $data_list ) > 0) {
					$dao_response_sum = $creditoCliente_dao->getSumCreditoByClienteIdByUnidadeId( $clienteid, $unidadeid );
					$dados = [ 
						'creditos' => $data_list,
						'total_creditos' => $dao_response_sum->getData()
					];
					$service_response->setStatus( ServiceResponseInterface::SUCESS );
					$service_response->setData( $dados );
				} else {
					$service_response->setStatus( ServiceResponseInterface::RECORD_NOT_FOUND );
					$service_response->setData( [ ] );
				}
			} else {
				$service_response->setStatus( ServiceResponseInterface::FORBIDDEN );
			}
		} else {
			$service_response->setStatus( $unidade_service_response->getStatus() );
		}

		return $service_response;

	}

	private function isPermitido(int $unidadeid, array $unidades): bool {

		for($i = 0; $i < count( $unidades ); $i++) {
			if ($unidadeid == intval( $unidades [$i] ['id'] )) {
				return TRUE;
			}
		}
		return FALSE;

	}

}

