<?php

class LivingTest extends \PHPUnit\Framework\TestCase
{
	/**
	 * @var null|Dalten_Export_Living
	 */
	private $export = null;

	/**
	 * @var Serenity_Config_Config
	 */
	private $entities = null;

	private $data = array(
		'id' => '1',
		'id_uzivatel' => 56943,
		'id_team' => 1,
		'id_firma' => 1285,
		'id_projekt' => 1,
		'cislo' => '1',
		'kod' => 'N00001',
		'id_klient' => '1',
		'zakazka_typ' => '1',
		'nemovitost_anuita' => '0',
		'nemovitost_anuita_mena' => '0',
		'nemovitost_balkon_plocha' => '0',
		'nemovitost_balkon' => '0',
		'nemovitost_bazen_plocha' => '0',
		'nemovitost_bazen' => '0',
		'nemovitost_bezbarierovy' => '-1',
		'nemovitost_bytu_celkem' => '0',
		'nemovitost_bytu_pocet' => '0',
		'nemovitost_cena_poznamka' => '',
		'nemovitost_cena_prodej' => '1.00',
		'nemovitost_cena_prodej_jednotka' => '1',
		'nemovitost_cena_pronajem' => '12000.00',
		'nemovitost_cena_pronajem_jednotka' => '2',
		'nemovitost_cena_datum_snizeni' => '',
		'nemovitost_charakter_zastavby' => '0',
		'nemovitost_dalsi_nebytove_prostory' => '',
		'nemovitost_datum_nastehovani' => '0000-00-00',
		'nemovitost_delka' => '0',
		'nemovitost_dispozice' => '1',
		'nemovitost_doprava' => '0001',
		'nemovitost_druh_bytu' => '0',
		'nemovitost_druh_objektu' => '3',
		'nemovitost_druh_pozemku' => '0',
		'nemovitost_druh_prostor' => '0',
		'nemovitost_druh_stavby' => '0',
		'nemovitost_elektrina' => '',
		'nemovitost_garaz_plocha' => '0',
		'nemovitost_garaz' => '-1',
		'nemovitost_parkovaci_stani' => '-1',
		'nemovitost_parkovaci_stani_plocha' => '0',
		'nemovitost_inzenyrske_site' => '',
		'nemovitost_komunikace' => '0',
		'nemovitost_konstrukcni_prvky' => '',
		'nemovitost_lodzie_plocha' => '0',
		'nemovitost_lodzie' => '1',
		'nemovitost_makler_poznamka' => '',
		'nemovitost_mena' => '1',
		'nemovitost_obcanska_vybavenost' => '',
		'nemovitost_odpad' => '',
		'nemovitost_ostatni' => '',
		'nemovitost_ostatni_nebytove_prosotry_popis' => '',
		'nemovitost_ostatni_rozvody' => '',
		'nemovitost_ostatni_vybavenost' => '',
		'nemovitost_plocha_celkova' => '80',
		'nemovitost_plocha_dalsi_nebytove_prostory' => '0',
		'nemovitost_plocha_dalsi_zelene' => '0',
		'nemovitost_plocha_dilna' => '0',
		'nemovitost_plocha_dilny' => '0',
		'nemovitost_plocha_jine' => '0',
		'nemovitost_plocha_jine_popis' => '',
		'nemovitost_plocha_kancelari' => '0',
		'nemovitost_plocha_nebytovych_prostor' => '0',
		'nemovitost_plocha_nejmensi' => '0',
		'nemovitost_plocha_nejvetsi' => '0',
		'nemovitost_plocha_obchodni' => '0',
		'nemovitost_plocha_obchodu' => '0',
		'nemovitost_plocha_obestavena' => '0',
		'nemovitost_plocha_parcely' => '0',
		'nemovitost_plocha_provozni' => '0',
		'nemovitost_plocha_skladu' => '0',
		'nemovitost_plocha_terasa' => '0',
		'nemovitost_plocha_uzitna' => '0',
		'nemovitost_plocha_uzitna_podkrovi' => '0',
		'nemovitost_plocha_uzitna_prizemi' => '0',
		'nemovitost_plocha_vnejsich_komunikaci' => '0',
		'nemovitost_plocha_vyrobni' => '0',
		'nemovitost_plocha_zahrada' => '0',
		'nemovitost_plocha_zastavena' => '0',
		'nemovitost_plochy_jine_popis' => '',
		'nemovitost_plyn' => '',
		'nemovitost_pocet_bytu_volnych' => '0',
		'nemovitost_pocet_kancelari' => '0',
		'nemovitost_pocet_luzek' => '0',
		'nemovitost_pocet_mist' => '0',
		'nemovitost_pocet_obchodu' => '0',
		'nemovitost_pocet_podlazi' => '10',
		'nemovitost_pocet_podlazi_pod_zemi' => '0',
		'nemovitost_pocet_volnych_kancelari' => '0',
		'nemovitost_podlazi_cislo' => '2',
		'nemovitost_poloha_objektu' => '0',
		'nemovitost_popis_detailni' => 'popis',
		'nemovitost_popis_zakladni' => 'zakldni popis',
		'nemovitost_prodej_vcelku' => '0',
		'nemovitost_pudni_vestavba' => '-1',
		'nemovitost_rok_kolaudace' => '0',
		'nemovitost_rok_rekonstrukce' => '0',
		'nemovitost_rok_vystavby' => '0',
		'nemovitost_schodiste' => '',
		'nemovitost_sirka' => '0',
		'nemovitost_sklep_plocha' => '0',
		'nemovitost_sklep' => '-1',
		'nemovitost_socialni_zarizeni' => '0',
		'nemovitost_socialni_zarizeni_popis' => '',
		'nemovitost_stavba_na_pozemku' => '',
		'nemovitost_stav_objektu' => '2',
		'nemovitost_stromy' => '0',
		'nemovitost_telekomunikace' => '',
		'nemovitost_terasa_plocha' => '0',
		'nemovitost_terasa' => '0',
		'nemovitost_titulek' => 'Pronájem útulné ropné plošiny v Přerově (testovací nabídka)',
		'nemovitost_topeni' => '',
		'nemovitost_typ' => '4',
		'nemovitost_dispozice_bytu' => '1',
		'nemovitost_typ_domu' => '0',
		'nemovitost_typ_zarizeni' => '0',
		'nemovitost_ucel_budovy' => '2',
		'nemovitost_umisteni_objektu' => '0',
		'nemovitost_umisteni_v_chranenych_lokalitach' => '',
		'nemovitost_velikost_bytu' => '',
		'nemovitost_vlastnictvi' => '1',
		'nemovitost_vlastni_pozemek' => '0',
		'nemovitost_voda' => '',
		'nemovitost_vybaveni_byt' => '0',
		'nemovitost_vybaveni_kancelari' => '0',
		'nemovitost_vybaveni_kancelari_popis' => '',
		'nemovitost_vybaveni_komercnich_prostor' => '0',
		'nemovitost_vybaveni_komercnich_prostor_popis' => '',
		'nemovitost_vybaveno' => '1',
		'nemovitost_vybaveno_popis' => '',
		'nemovitost_vyska' => '0',
		'nemovitost_vyska_stropu' => '0',
		'pocet_mist_k_parkovani' => '0',
		'podlazi' => '0',
		'zakazka_datum_editace' => '2010-04-13',
		'zakazka_datum_vlozeno' => '2010-04-13',
		'zakazka_makler' => '0',
		'zakazka_makler_upravil' => '1719',
		'zakazka_stav' => '10',
		'zakazka_typ' => '2',
		'zakazka_obchodovat' => '20',
		'zakazka_obchodovat_provize_castka' => '0.00',
		'zakazka_obchodovat_provize_procenta' => '0.000',
		'zakazka_provize_celkem' => '15000.00',
		'zakazka_provize_procenta' => '107.140',
		'zakazka_provize_typ' => '1',
		'nemovitost_uir_cobce_kod' => '414255',
		'nemovitost_uir_cobce_text' => 'Přerov I-Město',
		'nemovitost_uir_kraj_kod' => '124',
		'nemovitost_uir_kraj_text' => 'Olomoucký',
		'nemovitost_uir_mcast_kod' => '0',
		'nemovitost_uir_mcast_text' => '',
		'nemovitost_uir_obec_kod' => '101',
		'nemovitost_uir_obec_text' => 'Bratislava 1',
		'nemovitost_uir_okres_kod' => '3808',
		'nemovitost_uir_okres_text' => 'Bratislava 1',
		'nemovitost_uir_pobvod_kod' => '0',
		'nemovitost_uir_pobvod_text' => '',
		'nemovitost_uir_ulice_kod' => '522554',
		'nemovitost_uir_ulice_text' => 'Kosmíkova',
		'nemovitost_uir_cp' => '',
		'nemovitost_uir_co' => '',
		'nemovitost_uir_stat' => 'CZ',
		'nemovitost_objekt' => '',
		'zakazka_interni_cislo_developera' => '',
		'zakazka_pouzit_foto_projektu' => '0',
		'zakazka_typ_smlouvy' => '20',
		'zakazka_neverejna_poznamka' => '',
		'nemovitost_kauce' => '',
		'smazano' => '0',
		'nabidka_gps_x' => '',
		'nabidka_gps_y' => '',
		'nabidka_gps_presnost' => '-1',
		'zakazka_detail_url' => '',
		'interni_cislo' => 'N666',
		'zakazka_obchodovani_zpusob' => '0111',
		'nabidka_typ_nabidky' => '',
		'preklad_status' => '0',
		'nastaveni_exportu' => ''
	);

	private $user = array(
		'id' => 1,
		'id_firma' => 13,
		'jmeno' => 'frantisek',
		'uzivatel_os_jmeno' => 'Dalten',
		'uzivatel_os_prijmeni' => 'Matlák',
		'telefon' => 'Nokia N8',
		'email' => 'info@dalten.cz',
		'moto' => 'Kdo nekrade, je vůl.',
		'foto' => '/matlak.jpg'
	);

	private $images = array(
		'fikoid.jpg' => array(
			'id' => 1,
			'popis' => 'Foto 1',
			'soubor' => '/fikoid.jpeg'
		),

		'fikoid1.jpg' => array(
			'id' => 2,
			'popis' => 'Foto 2',
			'soubor' => '/fikoid.jpeg'
		)
	);

	private $_importUrl = 'http://test.living.sk/web_services/soap.php';
	private $_importUri = 'test.living.sk';
	private $_importUsername = 'remax';
	private $_importPassword = '9ZN0W9PcPsJ6';
	private $_importPasswordSalt = '7rHS8W9i';
	private $_additionalParams = array('result' => array(), 'listing' => array('portals' => array('sme')));

	private $_responseData = array();

	public function setUp(): void
	{
		$iniParser = new Serenity_Config_Parser_Ini();
		$codebooks = new Serenity_Config_Config(CONFIG_PATH . DIRECTORY_SEPARATOR . 'codebooks.ini', $iniParser);
		$this->entities = new Serenity_Config_Config(CONFIG_PATH . DIRECTORY_SEPARATOR . 'living.ini', $iniParser);

		$this->export = new Dalten_Export_Living($codebooks, $this->entities);
		$this->_additionalParams['advertiser_id'] = 1285;
	}



	public function testLogin()
	{
		$backend = new Dalten_Export_Api_Backend_Living($this->_importUrl, $this->_importUri);

		$api = new Dalten_Export_Api_Living($this->export, $this->entities, $backend);

		$response = $api->openConnection($this->_importUsername, $this->_importPassword, $this->_importPasswordSalt, $this->_additionalParams);
		$this->assertTrue($response->wasSuccessful(), $response->getMessage());
	}

	/**
	 * V testu se exportuje nabidka jako nova a pak se pokusi jeste jednou preexportovat.
	 */
	public function testExportListing()
	{
		$backend = new Dalten_Export_Api_Backend_Living($this->_importUrl, $this->_importUri);
		$api = new Dalten_Export_Api_Living($this->export, $this->entities, $backend);

		$response = $api->openConnection($this->_importUsername, $this->_importPassword, $this->_importPasswordSalt, $this->_additionalParams);
		$this->assertTrue($response->wasSuccessful(), $response->getMessage());

		$this->user['foto'] = realpath(ASSETS_PATH  . $this->user['foto']);

		foreach ($this->images as $key => $image) {
			$this->images[$key]['soubor'] = realpath(ASSETS_PATH . $image['soubor']);
		}

		// Pokazde chceme pridat noveho uzivatele a v dalsim kroku muze dojit k editaci stavajiciho.
		$this->user['id'] = uniqid();

		$response = $api->addListing($this->data, $this->user, $this->images, $this->_additionalParams);
		$this->assertTrue($response->wasSuccessful(), $response->getMessage());

		$data = $response->getData();
		$this->assertArrayHasKey('estate-id', $data);
		$this->assertArrayHasKey('ad-id', $data);
		$this->assertArrayHasKey('user-id', $data);
		$this->assertArrayHasKey('portals', $data);

		// Pridame do userdat vazbu na uzivatele.
		$userData = $this->user;
		$userData['livingsk_id'] = $data['user-id'];

		$response = $api->addListing($this->data, $userData, $this->images, array('result' => $data, 'listing' => array('portals' => array('sme'))));
		$this->assertTrue($response->wasSuccessful(), $response->getMessage());
	}

	public function testDeactivateAd()
	{
		$backend = new Dalten_Export_Api_Backend_Living($this->_importUrl, $this->_importUri);
		$api = new Dalten_Export_Api_Living($this->export, $this->entities, $backend);

		$response = $api->openConnection($this->_importUsername, $this->_importPassword, $this->_importPasswordSalt, $this->_additionalParams);
		$this->assertTrue($response->wasSuccessful(), $response->getMessage());

		// Pokazde chceme pridat noveho uzivatele at v dalsim kroku muze dojit k editaci stavajiciho.
		$this->user['id'] = uniqid();

		$response = $api->addListing($this->data, $this->user, $this->images, $this->_additionalParams);
		$this->assertTrue($response->wasSuccessful(), $response->getMessage());

		$data = $response->getData();
		$this->assertArrayHasKey('estate-id', $data);
		$this->assertArrayHasKey('ad-id', $data);
		$this->assertArrayHasKey('user-id', $data);
		$this->assertArrayHasKey('portals', $data);

		$response = $api->deleteListing($this->data, array('result' => $data, 'listing' => array('portals' => array('sme'))));
		$this->assertTrue($response->wasSuccessful(), $response->getMessage());
	}

	public function testGetServices()
	{
		$backend = new Dalten_Export_Api_Backend_Living($this->_importUrl, $this->_importUri);
		$api = new Dalten_Export_Api_Living($this->export, $this->entities, $backend);

		$response = $api->openConnection($this->_importUsername, $this->_importPassword, $this->_importPasswordSalt, $this->_additionalParams);
		$this->assertTrue($response->wasSuccessful(), $response->getMessage());

		$this->assertNotEmpty($backend->getPortals()->getData());
	}
}

