<?php

namespace app\Services;

use App\Services\Service;
use App\Services\ServiceResponse\ServiceResponse;
use App\DAO\ChameFacilDAO;
use App\ViewModels\ChameFacil\ChameFacilSenhaResgistrationViewModel\ChameFacilSenhaRegistrationViewModel;
use App\Services\JWT\UsuarioLogado;
use App\Utils\DateUtils;
use App\Mappers\ChameFacilSenhaMapper;
use App\DAO\DAOResponse\DAOResponseInterface;
use App\Services\ServiceResponse\ServiceResponseInterface;

class ChameFacilService extends Service {

	public function registerRecepcaoSenha(array $request, string $operacao, string $setor): ServiceResponse {

		$usr = UsuarioLogado::getUsuarioId();
		$dt = DateUtils::today();
		$hr = DateUtils::now_time();
		$data = $request;
		$data['usuarioId'] = $usr;
		$data['dt_cr'] = $dt;
		$data['hr_cr'] = $hr;
		$data['operacao'] = $operacao;
		$data['setor'] = $setor;
		$dao = new ChameFacilDAO();
		$obj_cf = ChameFacilSenhaMapper::getRequestRecepcaoChameFacilSenha($data);
		$dao_response = $dao->registerRecepcaoSenha( $obj_cf );
		$service_response = new ServiceResponse();
		switch ($dao_response->getStatus()) {
			case DAOResponseInterface::SUCESS:
				$service_response->setStatus(ServiceResponseInterface::SUCESS);
				$service_response->setData($dao_response->getData());
			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 registerSenhaChamar( string $setor): ServiceResponse {
		
		$usr = UsuarioLogado::getUsuarioId();
		$dt = DateUtils::today();
		$hr = DateUtils::now_time();
		$data = [];
		$data['usuarioId'] = $usr;
		$data['dt_cr'] = $dt;
		$data['hr_cr'] = $hr;
		$data['operacao'] = 'Chamar Senha';
		$data['setor'] = $setor;
		$dao = new ChameFacilDAO();
		$obj_cf = ChameFacilSenhaMapper::getRequestRecepcaoChameFacilSenha($data);
		$dao_response = $dao->registerRecepcaoSenha( $obj_cf );
		$service_response = new ServiceResponse();
		switch ($dao_response->getStatus()) {
			case DAOResponseInterface::SUCESS:
				$service_response->setStatus(ServiceResponseInterface::SUCESS);
				$service_response->setData($dao_response->getData());
				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 registerRespostaChameFacil(array $request): ServiceResponse {
		
		$usr = UsuarioLogado::getUsuarioId();
		$dt = DateUtils::today();
		$hr = DateUtils::now_time();
		$data = $request;
		$data['usuarioId'] = $usr;
		$data['dt_cr'] = $dt;
		$data['hr_cr'] = $hr;
		$data['operacao'] = $operacao;
		$data['setor'] = $setor;
		$dao = new ChameFacilDAO();
		$obj_cf = ChameFacilSenhaMapper::getRequestRecepcaoChameFacilSenha($data);
		$dao_response = $dao->registerRecepcaoSenha( $obj_cf );
		$service_response = new ServiceResponse();
		switch ($dao_response->getStatus()) {
			case DAOResponseInterface::SUCESS:
				$service_response->setStatus(ServiceResponseInterface::SUCESS);
				$service_response->setData($dao_response->getData());
				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 getRecepcaoSenha(int $id_senha, int $id_pet, int $id_unidade): ServiceResponse {
		$dao = new ChameFacilDAO();
		$dao_response = $dao->getRecepcaoSenha( $id_senha, $id_pet, $id_unidade);
		$service_response = new ServiceResponse();
		switch ($dao_response->getStatus()) {
			case DAOResponseInterface::SUCESS:
				$service_response->setStatus(ServiceResponseInterface::SUCESS);
				$service_response->setData($dao_response->getData());
				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 getMedicoSenha(int $id_senha, int $id_pet, int $id_unidade, int $id_filainterna): ServiceResponse {
		$dao = new ChameFacilDAO();
		$dao_response = $dao->getMedicoSenha( $id_senha, $id_pet, $id_unidade, $id_filainterna );
		$service_response = new ServiceResponse();
		switch ($dao_response->getStatus()) {
			case DAOResponseInterface::SUCESS:
				$service_response->setStatus(ServiceResponseInterface::SUCESS);
				$service_response->setData($dao_response->getData());
				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 validrequestRecepcaoEncaminhar($data): bool {

		if (!intval( $data ['petId'] )) {
			return false;
		}
		if (!intval( $data ['senhaId'] )) {
			return false;
		}
		if (!intval( $data ['tutorId'] )) {
			return false;
		}
		if (!intval( $data ['filaInternaId'] )) {
			return false;
		}
		if (!intval( $data ['requisicaoId'] ) && trim($data ['requisicaoId'])!= '0') {
			return false;
		}
		if (!intval( $data ['unidadeId'] )) {
			return false;
		}

		return true;

	}

	public function validrequestRecepcaoAguardar($data): bool {

		if (!intval( $data ['petId'] )) {
			return false;
		}
		if (!intval( $data ['senhaId'] )) {
			return false;
		}
		if (!intval( $data ['tutorId'] )) {
			return false;
		}
		if (!intval( $data ['unidadeId'] )) {
			return false;
		}

		return true;

	}
	
	public function validrequestSenhaChamar($data): bool {
		
		if (!intval( $data ['setor'] )) {
			return false;
		}
				
		return true;
		
	}

}

