<?php
namespace common\components;


use yii\base\Object;
use common\models\TicketInstallmentRequest;
use common\components\giro\GiroDETSTATetel;
use common\models\Ugiro;
use common\components\giro\GiroDETSTA;
use common\components\giro\GiroDETSTAFej;
use common\components\giro\GiroDETSTALab;
use common\components\DetStaDBSave;


/**
 * This is the model class for table "ticket".
 *
 * @property common\components\giro\GiroDETSTA $detstatUzenet
 * @property common\models\UGiro  $koteg
 * 
 * */
class DetStatProcessor extends Object{
	
	
	/**aktuális koteg, config paraméterként kapju*/
	public $koteg;
	public $path;
	
	public $idKoteg;
	public $detstatUzenet;
	public $megbizasok;
	
	public $errors = [];
	
	public function run(){
// 		$this->readKoteg();
		$this->readKotegMegbizasok();
		$this->readDetstaUzenet();
		$this->saveMessageDetsta();
		$this->createMegbizasTetelHozzarendelesek();
		$this->processMegbizasok();
		$this->markKotegFinished();
	}
	
	public function saveMessageDetsta(){
		$saver =  new DetStaDBSave(
				[
						'giroDETSTA' => $this->detstatUzenet,
						'path' => $this->path,
						'koteg' => $this->koteg,
						'idUser' =>\Yii::$app->user->id
				]);
		 
		$saver->run();
	}
	
	public function markKotegFinished(){
		$this->koteg->status = Ugiro::$STATUS_FINISHED;
		$this->koteg->save();
	}
	
	public function processMegbizasok(){
		foreach ($this->megbizasok as $megbizas){
			$processor = new DetStatTetelProcessor(
				[
					'tetel' => $megbizas->detstaTetel,
					'megbizas' => $megbizas
				]
			);
			$processor->run();
// 			$this->errors = $this->errors + $processor->errors;
		}
	}
	
	public function readDetstaUzenet(){
		
		$filename = \Yii::getAlias("@webroot") ."/" .$this->path;
		$content = file_get_contents($filename);
		$this->detstatUzenet =  GiroDETSTA::parse($content);
		
		/*
		
		$this->detstatUzenet = new GiroDETSTA();
		$this->idKoteg = 42;
		
		$fej = new GiroDETSTAFej();
		
		$this->detstatUzenet->fej = $fej;
		
		$tetel = new GiroDETSTATetel();
		$tetel->tetelSorszam = 1;
		$tetel->visszajelzesInformacio = "00";
// 		$tetel->visszajelzesInformacio = "02";
		
		$this->detstatUzenet->tetelek[] = $tetel;
		
// 		$tetel = new GiroDETSTATetel();
// 		$tetel->tetelSorszam = 2;
// 		$tetel->visszajelzesInformacio = "00";
// 				$tetel->visszajelzesInformacio = "02";
		
// 		$this->detstatUzenet->tetelek[] = $tetel;
		$lab = new GiroDETSTALab();
		$this->detstatUzenet->lab = $lab;
		*/
		
	}
	
	public function createMegbizasTetelHozzarendelesek(){
		$mapTetel = [];
		foreach ($this->detstatUzenet->tetelek as $tetel ){
			$mapTetel[$tetel->tetelSorszam] = $tetel;
		}
		
		foreach ($this->megbizasok as $megbizas){
			if ( array_key_exists($megbizas->number, $mapTetel)){
				$megbizas->detstaTetel =  $mapTetel[$megbizas->number];
			}
		}
		
		
	}
	
	
	public function readKoteg(){
		$this->koteg =  Ugiro::findOne($this->idKoteg );
		if ( !isset($this->koteg) ){
			$this->errors [] = "Kötege nem található! (".$this->detstatUzenet->fej." )";
		}
	}
	public function readKotegMegbizasok(){
		$this->megbizasok  =  $this->koteg->requests;
	}
	
// 	public function applyNewMegbizasState(){
// 		$kod = $this->tetel->valaszHivatkozasiKod; 
		
// 		if ( $kod == GiroDETSTATetel::$INFORMACIO_TELJESITETT){
// 			$this->megbizas->status = TicketInstallmentRequest::$STATUS_ACCEPTED;
// 		}else{
// 			$this->megbizas->status = TicketInstallmentRequest::$STATUS_REJECTED;
// 		}
// 		$this->megbizas->save(false);
// 	}
	
// 	public function applyNewTicketState(){
// 		$this->ticket->applyTicketInstallmentRequest($this->megbizas);
// 		$this->ticket->save(false);
// 	}
	
	
	public function hasError(){
		return count($this->errors) > 0;
	}
	
	
}