<?php

namespace App\Services;

use App\DAO\DAOResponse\DAOResponseInterface;
use App\Services\Service;
use App\Services\ServiceResponse\ServiceResponse;
use App\Services\ServiceResponse\ServiceResponseInterface;
use App\Mappers\VoucherMapper;
use App\DAO\VoucherDAO;
use App\Utils\DateUtils;

class VoucherService extends Service {

	public function registerVoucher(array $dados, int $idcompra): ServiceResponse {

		$serviceReponse = new ServiceResponse();
		$voucherDao = new VoucherDAO();

		$vouchers = array ();

		$produtos = $dados ['produtos'];

		$produto_service = new ProdutoServicoService();

		for($i = 0; $i < count( $produtos ); $i++) {
			$produto = $produtos [$i];
			$produto_response = $produto_service->getProdutoFromlojaProdutoId( $idcompra, $produto ['id_loja_produto'] );
			if ($produto_response->getStatus() == ServiceResponseInterface::SUCESS) {
				$resp = $produto_response->getData();
				if ($resp ['voucher'] == "1") {
					array_push( $vouchers, $resp );
				}
			}
		}

		if (count( $vouchers ) > 0) {
			$resp_voucher = array ();
			for($i = 0; $i < count( $vouchers ); $i++) {
				$response_serial = $voucherDao->getNewUUID();

				if ($response_serial->getStatus() != DAOResponseInterface::SUCESS) {
					$serviceReponse = $this->setServiceResponse( $response_serial );
					return $serviceReponse;
				}

				$vencimento = '1900-01-01';
				if ($vouchers [$i] ['id_tipo_voucher'] == 2) {
					$hoje = DateUtils::today();
					$vencimento = date( 'Y-m-d', strtotime( $hoje . '+ 1 years' ) );
				}

				$dados ['serial'] = $response_serial->getData() ['seria_uuid'];
				$dados ['id_tipo'] = $vouchers [$i] ['id_tipo_voucher'];
				$dados ['id_compra'] = $idcompra;
				$dados ['id_compra_item'] = $vouchers [$i] ['id'];
				$dados ['data_vencimento'] = $vencimento;

				$voucherRegistration_vm = VoucherMapper::getVoucheRegistrationViewModelFromArray( $dados );
				$daoResponse = $voucherDao->registerVoucher( $voucherRegistration_vm );
				if ($daoResponse->getStatus() != DAOResponseInterface::SUCESS) {
					$serviceReponse = $this->setServiceResponse( $daoResponse );
					return $serviceReponse;
				}
				$r = $daoResponse->getData() ['voucher'];
				$r ['id_pet'] = $voucherRegistration_vm->getIdPet();
				array_push( $resp_voucher, $r );
			}
			$serviceReponse = $this->setServiceResponse( $daoResponse );
			$serviceReponse->setData( [ 
				'vouchers' => $resp_voucher
			] );
		}

		return $serviceReponse;

	}

	public function registerVoucherByCompraId(int $idcompra): ServiceResponse {

		$serviceReponse = new ServiceResponse();

		return $serviceReponse;

	}

	public function changeVoucherStatus(array $dados): ServiceResponse {

		$serviceReponse = new ServiceResponse();

		$lojaChangeStatus_vm = VoucherMapper::getVoucherChangeStatusViewModelFromArray( $dados );

		$lojaDao = new VoucherDAO();
		$daoResponse = $lojaDao->changeStatusLoja( $lojaChangeStatus_vm );
		$serviceReponse = $this->setServiceResponse( $daoResponse );

		return $serviceReponse;

	}

	public function getVoucherById(int $id_voucher): ServiceResponse {

		$serviceReponse = new ServiceResponse();

		$voucherDao = new VoucherDAO();

		$daoResponse = $voucherDao->getVoucherById( $id_voucher );
		$serviceReponse = $this->setServiceResponse( $daoResponse );

		return $serviceReponse;

	}

	public function getVoucherListByPetId(int $id_pet): ServiceResponse {

		$serviceReponse = new ServiceResponse();

		$voucherDao = new VoucherDAO();
		$daoResponse = $voucherDao->getVoucherListByPetId( $id_pet );
		$serviceReponse = $this->setServiceResponse( $daoResponse );

		return $serviceReponse;

	}

	public function getVoucherListByUnidadeId(int $id_unidade): ServiceResponse {

		$serviceReponse = new ServiceResponse();

		$voucherDao = new VoucherDAO();
		$daoResponse = $voucherDao->getVoucherListByUnidadeId( $id_unidade );
		$serviceReponse = $this->setServiceResponse( $daoResponse );

		return $serviceReponse;

	}

	public function getVoucherBySerial($serial): ServiceResponse {

		$serviceReponse = new ServiceResponse();

		$voucherDao = new VoucherDAO();
		$daoResponse = $voucherDao->getVoucherBySerial( $serial );
		$serviceReponse = $this->setServiceResponse( $daoResponse );

		return $serviceReponse;

	}

	public function getVoucherAtivosByPetId(int $id_pet): ServiceResponse {

		$serviceReponse = new ServiceResponse();

		$voucherDao = new VoucherDAO();
		$daoResponse = $voucherDao->getVoucherAtivosByPetId( $id_pet );
		$serviceReponse = $this->setServiceResponse( $daoResponse );

		return $serviceReponse;

	}
	
	public function setVoucherUnsed(int $id_voucher):ServiceResponse{
		$serviceReponse = new ServiceResponse();
		
		$voucherDao = new VoucherDAO();
		$daoResponse = $voucherDao->setVoucherUnsed( $id_voucher );
		$serviceReponse = $this->setServiceResponse( $daoResponse );
		
		return $serviceReponse;
		
	}

}

