<?php
require_once('tracking.php');

class Correios extends correiosUtils{   

    // private $wsdl      = 'https://webservice.correios.com.br/service/rastro/Rastro.wsdl' ;
    private $tipo      = 'L'   ;
    private $resultado = 'T'   ;
    private $idioma    = '101' ;
	
	protected $statusCorreios = array(
		'_tracking_status_entregue'     => array('BDE' => 1, 'BDI' => 1, 'BDR' => 1),
		'_tracking_status_saiu_entrega' => array('OEC' => 0)
	);

	protected $statusCorreiosFinal = array(
		'_tracking_status_entregue' => array('BDE' => [1,23], 'BDI' => [1,23], 'BDR' => [1,23])
	);
     
    public function getObjectTracking($objects){
				$results = [];

        try{
					foreach ($objects as $index => $object) {
						$trakingData = $this->correios->getTrackingObj($object);

						if(isset($trakingData)){
							array_push($results, $trakingData);
						}
					}

          if(is_object($results) && isset($results->return->objeto->erro)){
            $error = $results->return->objeto->erro.': '.$results->return->objeto->numero;
            throw new Exception($error);
        	}

          $this->eventos = $results[0];
					return $results;               
        }catch (Exception $e){            
            throw $e;
        }

    }

  public function getEvents($data = array()){		
			$eventData	= array();

			if(is_object($this->eventos) && property_exists($this->eventos,'objetos')){
				$eventData = array_map(function($array) use (&$data){  				
					if(!isset($array->erro)){						
						return array(
							'order_tracking_id' => (in_array(trim($array->codObjeto), $data['tracking_code'])) ? array_search(trim($array->codObjeto), $data['tracking_code']) : false,
							'order_id' => (in_array(trim($array->codObjeto), $data['order_id'])) ? array_search(trim($array->codObjeto), $data['order_id']) : false,
							'evento' => array_map(function($event){
								$dataTimeCreated = strtotime($event->dtHrCriado);

								$date = date("Y-m-d", $dataTimeCreated);
								$hora  = date("H:i:s", $dataTimeCreated);

								return array(
									'tipo'      => $event->codigo,
									'status'    => '1',
									'data'      => $date,
									'hora'      => $hora,
									'descricao' => $event->descricao,
									'history'   => $this->getHistory(['tipo' => $event->codigo, 'status'=> $event->tipo]),
									'finally'	=> $this->getFinally(['tipo' => $event->codigo, 'status'=> $event->tipo])	
								);	
							}, $array->eventos)
						);					
					}else{
						return false;
					}
				}, $this->eventos->objetos);					
			}

		return $eventData;
	}
	
	public function getHistory($data = array()){
		
		foreach($this->statusCorreios as $key => $statusCorreios){
			if(array_key_exists($data['tipo'], $statusCorreios) && in_array($data['status'], $statusCorreios)){
				return true;
			}			
		}
		
		return false ;			
	}

	public function getFinally($data = array()){
		$retorno = false;
		foreach($this->statusCorreiosFinal as $key => $statusCorreioFinal){
			if(array_key_exists($data['tipo'], $statusCorreioFinal)){
				$retorno = array_map(function ($array) use (&$data){
					return in_array($data['status'], $array) ? true : false;
				}, $statusCorreioFinal);				
			}			
		}
	
		return $retorno ;			
	}	

   
}