<?php

namespace App\Services;

use App\DAO\InternacaoDAO;
use App\Services\Service;
use App\Services\ServiceResponse\ServiceResponse;
use App\ViewModels\Anamnese\AnamneseAdicionalRegistrationViewModel\AnamneseAdicionalRegistrationViewModel;
use App\ViewModels\Orcamento\OrcamentoClienteItemDetalhe\OrcamentoClienteItemDetalhe;
use App\DAO\RequisicaoDAO;
use App\DAO\DAOResponse\DAOResponse;
use App\DAO\DAOResponse\DAOResponseInterface;
use App\DAO\OrcamentoClienteDAO;
use App\Services\ServiceResponse\ServiceResponseInterface;

class RequisicaoService extends Service {

	public function registerRequisicaoATDContinuado(AnamneseAdicionalRegistrationViewModel $objRegister): ServiceResponse {

		$service_response = new ServiceResponse();
		$dao = new RequisicaoDAO();
		$dao_response = $dao->registerRequisicaoATDContinuado( $objRegister );
		$service_response = $this->setServiceResponse( $dao_response );

		return $service_response;

	}

	public function registerRequisicaoEXContinuado(AnamneseAdicionalRegistrationViewModel $objRegister): ServiceResponse {

		$service_response = new ServiceResponse();
		$dao = new RequisicaoDAO();
		$dao_response = $dao->registerRequisicaoEXContinuado( $objRegister );
		$service_response = $this->setServiceResponse( $dao_response );

		return $service_response;

	}

	/* método para exame , medicação */
	public function registerRequisicaoExameFromOrcamento(OrcamentoClienteItemDetalhe $orcamentoDetalhe): ServiceResponse {

		$service_response = new ServiceResponse();
		$dao_orcamento = new OrcamentoClienteDAO();
		$dao_internacao = new InternacaoDAO();
		$orcamento_response = $dao_orcamento->getOrcamentoByID( $orcamentoDetalhe->getIdOrcamento() );
		if ($orcamento_response->getStatus() != DAOResponseInterface::SUCESS) {
			$service_response = $this->setServiceResponse( $orcamento_response );
		} else {
			$orcamento_dados = $orcamento_response->getData();
			$orcamento = $orcamento_dados ['orcamento'];
			$comandaId = $this->getComandaAtivaIdByPetByClienteByUnidade( intval( $orcamento ['id_pet'] ), intval( $orcamento ['id_cliente'] ),
				intval( $orcamento ['id_unidade'] ) );
			$comanda_service = new ComandaService();
			$comanda_response = new ServiceResponse();
			$comanda_response->setStatus( ServiceResponseInterface::UNKNOWN_ERROR );
			if ($comandaId == 0) {
				/* cria nova comanda */
				$comanda_response = $comanda_service->registerComandaFromOrcamento( $orcamento );
				if ($comanda_response->getStatus() != ServiceResponseInterface::SUCESS) {
					$service_response = $comanda_response;
				} else {
					$result_comanda = $comanda_response->getData();
					$comandaId = $result_comanda ['id_comanda'];
				}
			}

			if ($comandaId > 0) {
				/* inclui itens na comanda */
				$quantidade = $orcamentoDetalhe->getQuantidade();
				$listRequisicao = array ();
				$listComandaItem = array ();

				for($i = 0; $i < $quantidade; $i++) {
					$dao_req = new RequisicaoDAO();
					$req_response = $dao_req->registerRequisicaoFromOrcamento( $orcamentoDetalhe, $orcamento );
					if ($req_response->getStatus() == DAOResponseInterface::SUCESS) {
						$result_req = $req_response->getData();
						$requisicaoId = $result_req ['id_requisicao'];
                        $dao_internacao->registerInternacaoFromOrcamento( $result_req ['id_requisicao'], intval( $orcamento ['id_pet'] ), intval( $orcamento ['id_unidade'] ) );
						array_push( $listRequisicao, $requisicaoId );
						$comanda_item_response = $comanda_service->registerComandaItemFromOrcamento( $orcamentoDetalhe, $comandaId, $requisicaoId );
						if ($comanda_item_response->getStatus() == ServiceResponseInterface::SUCESS) {
							$resp_comanda_item = $comanda_item_response->getData();
							$comandaItemId = $resp_comanda_item ['id_comanda_item'];
							array_push( $listComandaItem, $comandaItemId );
						} else {
							array_push( $listComandaItem, 0 );
						}
					} else {
						array_push( $listRequisicao, 0 );
					}
				}
				$orcamentoDetalhe->setListIdComandaItem($listComandaItem);
				$orcamentoDetalhe->setListIdRequisicao($listRequisicao);
				
				
				if(in_array(0, $listComandaItem) || in_array(0, $listRequisicao)) {
					$service_response->setStatus(ServiceResponseInterface::HAVE_CONFLICTS);
					$service_response->setData(['item' => $orcamentoDetalhe]);
				} else {
					$service_response->setStatus(ServiceResponseInterface::SUCESS);
					$service_response->setData(['item' => $orcamentoDetalhe]);
				}
			}
		}

		return $service_response;

	}

	/* método para consulta, cirurgia, internação */
	public function registerRequisicaoConsultaFromOrcamento(OrcamentoClienteItemDetalhe $orcamentoDetalhe): ServiceResponse {
		
		$service_response = new ServiceResponse();

		$service_response = $this->registerRequisicaoExameFromOrcamento($orcamentoDetalhe);
		
		return $service_response;

	}

	private function getComandaAtivaIdByPetByClienteByUnidade(int $id_pet, int $id_cliente, int $id_unidade): int {

		$comandaId = 0;
		$comanda_service = new ComandaService();
		$comanda_response = $comanda_service->getComandaAtivaByPetByClienteByUnidade( $id_pet, $id_cliente, $id_unidade );
		if ($comanda_response->getStatus() != ServiceResponseInterface::SUCESS) {
			$comandaId = 0;
		}
		if ($comanda_response->getStatus() == ServiceResponseInterface::SUCESS) {
			$comanda_dados = $comanda_response->getData();
			$comandaId = $comanda_dados['comanda_id'];
		}

		return $comandaId;

	}

	public function updateRequisicaoATDContinuado(): ServiceResponse {

		$service_response = new ServiceResponse();
		$dao = new RequisicaoDAO();
		$dao_response = $dao->updateRequisicaoATDContinuado();
		$service_response = $this->setServiceResponse( $dao_response );

		return $service_response;

	}

	public function getStatusRequisicao(int $idRequisicao): ServiceResponse {

		$service_response = new ServiceResponse();
		$dao = new RequisicaoDAO();
		$dao_response = $dao->getStatusRequisicaoById( $idRequisicao );
		$service_response = $this->setServiceResponse( $dao_response );

		return $service_response;

	}

	public function getRequisicoesEmAbertoByPetIdByUnidadeToAtender(int $pet_id, int $unidade_id): ServiceResponse {

		$service_response = new ServiceResponse();
		$dao = new RequisicaoDAO();
		$dao_response = $dao->getRequisicoesEmAbertoByPetIdByUnidadeToAtender( $pet_id, $unidade_id );
		$service_response = $this->setServiceResponse( $dao_response );

		return $service_response;

	}

	public function getRequisicoesExamesEmAbertoByPetIdUnidadeId(int $idPet, int $idUnidade): ServiceResponse {

		$service_response = new ServiceResponse();
		$dao = new RequisicaoDAO();
		$dao_response = $dao->getRequisicoesExamesEmAbertoByPetIdUnidadeId( $idPet, $idUnidade );
		$service_response = $this->setServiceResponse( $dao_response );

		return $service_response;

	}
	
	public function getRequisicoesHigieneEmAbertoByPetIdUnidadeId(int $idPet, int $idUnidade, $comanda_status): ServiceResponse {
	    
	    $service_response = new ServiceResponse();
	    $dao = new RequisicaoDAO();
	    $dao_response = $dao->getRequisicoesHigieneEmAbertoByPetIdUnidadeId( $idPet, $idUnidade, $comanda_status );
	    $service_response = $this->setServiceResponse( $dao_response );
	    
	    return $service_response;
	    
	}

	public function setItemExtornadoToExcluirRequisicao($itemExtornado): ServiceResponse {

		$service_response = new ServiceResponse();
		$dao = new RequisicaoDAO();
		$dao_response = $dao->setItemExtornadoToExcluirRequisicao( $itemExtornado );
		$service_response = $this->setServiceResponse( $dao_response );

		return $service_response;

	}

	public function isExecutedItemList($extorno): ServiceResponse {

		$respList = [ ];
		$item_executado = [ ];
		$service_response = new ServiceResponse();
		$dao = new RequisicaoDAO();
		$dao_response = new DAOResponse();
		foreach ( $extorno->itens as $item ) {
			$response = $dao->isExecutedItemComanda( intval( $item->comanda_item_id ) );
			array_push( $respList, $response );
		}
		for($i = 0; $i < count( $respList ); $i++) {
			$dado = $respList [$i]->getData();
			if (intval( $dado [0] ['status_id'] ) == 2) {
				array_push( $item_executado, $dado [0] ['comada_item'] );
			}
		}
		if (count( $item_executado ) == 0) {
			$dao_response->setStatus( DAOResponseInterface::RECORD_NOT_FOUND );
		} else {
			$dao_response->setStatus( DAOResponseInterface::SUCESS );
			$dao_response->setData( $item_executado );
		}
		$service_response = $this->setServiceResponse( $dao_response );

		return $service_response;

	}

	public function adminExecExame(int $requisicao_id, int $assistente_id, string $data_exec, string $hora_exec): ServiceResponse {


	}

	public function adminExcluirExame(int $requisicao_id): ServiceResponse {

		$service_response = new ServiceResponse();
		$dao = new RequisicaoDAO();
		// int $requisicao_id, int $assistente_id, string $data_exec, string $hora_exec
		$daoResponse = $dao->adminExcluirExame( $requisicao_id );
		$service_response = $this->setServiceResponse( $daoResponse );
		return $service_response;

	}
	
	public function adminExcluirConsulta(int $requisicao_id): ServiceResponse {
		
		$service_response = new ServiceResponse();
		$dao = new RequisicaoDAO();
		// int $requisicao_id, int $assistente_id, string $data_exec, string $hora_exec
		$daoResponse = $dao->adminExcluirConsulta( $requisicao_id );
		$service_response = $this->setServiceResponse( $daoResponse );
		return $service_response;
		
	}

	public function adminNewReqExame(array $request): ServiceResponse {

		$service_response = new ServiceResponse();
		$dao = new RequisicaoDAO();
		$daoResponse = $dao->adminNewReqExame( $request );
		$service_response = $this->setServiceResponse( $daoResponse );
		return $service_response;

	}

	public function adminUpdateConsultaIdNewReqExame(int $requisicao_id, int $consulta_id): ServiceResponse {

		$service_response = new ServiceResponse();
		$dao = new RequisicaoDAO();
		$daoResponse = $dao->adminUpdateConsultaIdNewReqExame( $requisicao_id, $consulta_id );
		$service_response = $this->setServiceResponse( $daoResponse );
		return $service_response;

	}

	public function getDadosRequisicaoID(int $requisicao_id): ServiceResponse {

		$service_response = new ServiceResponse();
		$dao = new RequisicaoDAO();
		$daoResponse = $dao->getDadosRequisicaoID( $requisicao_id );
		$service_response = $this->setServiceResponse( $daoResponse );
		return $service_response;

	}

}

