<?php
namespace App\Utils\BaseParser\Utils;

/**
 * Parser for the Hotel product code/multiplier/SIPPRM tag
 * @author Lucian Sirbu
 */
class HotelProductCodeRoomParser
{
	/**
	 * The input text
	 * @var string
	 */
	protected $text = null;

	/**
	 * Forgot where i got them originally, might still be listed in the main U line, but also check class references (@see's)
	 * @var array
	 */
	protected $roomCodes = [
		[ 'code1'=>'A',     'class'=> 'superior',    'type'=>'roomWithBath',       'hasBath'=>true,    'translation'=>'Room with bath', ],
		[ 'code1'=>'B',     'class'=> 'moderate',    'type'=>'roomWithBath',       'hasBath'=>true,    'translation'=>'Room with bath', ],
		[ 'code1'=>'C',     'class'=> 'standard',    'type'=>'roomWithBath',       'hasBath'=>true,    'translation'=>'Room with bath', ],
		[ 'code1'=>'D',     'class'=> 'minimum',     'type'=>'roomWithBath',       'hasBath'=>true,    'translation'=>'Room with bath', ],

		[ 'code1'=>'E',     'class'=> 'superior',    'type'=>'roomWithShower',     'hasBath'=>true,    'translation'=>'Room with shower', ],
		[ 'code1'=>'F',     'class'=> 'moderate',    'type'=>'roomWithShower',     'hasBath'=>true,    'translation'=>'Room with shower', ],
		[ 'code1'=>'G',     'class'=> 'standard',    'type'=>'roomWithShower',     'hasBath'=>true,    'translation'=>'Room with shower', ],
		[ 'code1'=>'H',     'class'=> 'minimum',     'type'=>'roomWithShower',     'hasBath'=>true,    'translation'=>'Room with shower', ],

		[ 'code1'=>'I',     'class'=> 'superior',    'type'=>'roomWithoutShower',  'hasBath'=>false,   'translation'=>'Room without bath/shower', ],
		[ 'code1'=>'J',     'class'=> 'moderate',    'type'=>'roomWithoutShower',  'hasBath'=>false,   'translation'=>'Room without bath/shower', ],
		[ 'code1'=>'K',     'class'=> 'minimum',     'type'=>'roomWithoutShower',  'hasBath'=>false,   'translation'=>'Room without bath/shower', ],

		[ 'code1'=>'N',     'class'=> null,          'type'=>'nonSmoking',         'hasBath'=>null,    'translation'=>'Non-smoking', ],

		[ 'code1'=>'P',     'class'=> 'superior',    'type'=>'executiveSuite',     'hasBath'=>true,    'translation'=>'Executive suite', ],

		[ 'code1'=>'S',     'class'=> 'moderate',    'type'=>'suite',              'hasBath'=>true,    'translation'=>'Suite', ],
		[ 'code1'=>'T',     'class'=> 'standard',    'type'=>'suite',              'hasBath'=>true,    'translation'=>'Suite', ],
		[ 'code1'=>'U',     'class'=> 'minimum',     'type'=>'suite',              'hasBath'=>true,    'translation'=>'Suite', ],

		[ 'code1'=>'W',     'class'=> null,          'type'=>'weekendRate',        'hasBath'=>null,    'translation'=>null, ],

		[ 'code1'=>'L',     'class'=> null,          'type'=>null,                 'hasBath'=>null,    'translation'=>null, ], // what is this? where/how did you meet this case?
		[ 'code1'=>'Q',     'class'=> 'superior',    'type'=>'roomWithBath',       'hasBath'=>true,    'translation'=>null, ], // what is this? where/how did you meet this case?

		[ 'code1'=>'*',     'class'=> null,          'type'=>null,                 'hasBath'=>null,    'translation'=>'Any', ],
		[ 'code1'=>'0',     'class'=> null,          'type'=>null,                 'hasBath'=>null,    'translation'=>null, ],
	];

	protected $bedCodes = [
		[ 'code1'=>'*',     'type'=>null, 			'translation'=>'Any', ],
		[ 'code1'=>'0',     'type'=>null, 			'translation'=>null, ],
		[ 'code1'=>'T',     'type'=>'twin', 		'translation'=>'Twin', ],
		[ 'code1'=>'S',     'type'=>'single', 		'translation'=>'Single', ],
		[ 'code1'=>'D',     'type'=>'double', 		'translation'=>'Double', ],
		[ 'code1'=>'K',     'type'=>'king', 		'translation'=>'King size', ],
		[ 'code1'=>'Q',     'type'=>'queen', 		'translation'=>'Queen size', ],
		[ 'code1'=>'W',     'type'=>'water', 		'translation'=>'Water bed', ],
		[ 'code1'=>'P',     'type'=>'pullOut', 		'translation'=>'Pull-out bed', ],
		[ 'code1'=>'X',     'type'=>'special', 		'translation'=>'Special', ],
		[ 'code1'=>'E',     'type'=>'budget', 		'translation'=>'Budget', ],
	];

	/**
	 * Internal class options
	 */
	protected $options = [
	];

	/**
	 * Set the text to parse
	 * @param string $text
	 * @return $this
	 */
	public function setText($text)
	{
		$this->text = $text;
		return $this;
	}

	/**
	 * Extract the passenger name from the input string
	 * @return array
	 */
	public function extract()
	{
		if ($this->text=='ROH') {
			return [
				'room' => 		[
					'code1'=> 	null,
					'code3'=> 	'ROH',
					'class'=> 	'any',
					'type'=> 	'any',
					'hasBath'=> null,
					'translation'=>'Run of the House - Room Type will be allocated on checkin',
				],
				'bed' => 		[
					'code1'=>		null,
					'type'=>		'any',
					'translation'=>	'Any',
				],
				'bedCount' => 	null,
			];
		}
		else {
			$xtr = new \App\Utils\BaseParser\Utils\Extractor\Single();
			if ($xt = $xtr->findAndStore("/^(?P<roomCode>.)(?P<bedCount>[0-9X\*])(?P<bedCode>.)$/", $this->text)) {
				$room = null;
				$bed = null;

				$room = array_filter($this->roomCodes, function($r) use($xt) {
					return $r['code1']==$xt->roomCode;
				});

				$bed = array_filter($this->bedCodes, function($b) use($xt) {
					return $b['code1']==$xt->bedCode;
				});

				return [
					'room' => 		($room?reset($room):null),
					'bed' => 		($bed?reset($bed):null),
					'bedCount' => 	($xt->bedCount?(is_numeric($xt->bedCount)?(int)$xt->bedCount:$xt->bedCount):null),
				];
			}
		}

		return null;
	}
}
