<?php

declare(strict_types=1);

namespace App\CoreModule\Model;

use Nette;
use App\Model\DatabaseManager;
use Nette\Database\Table\ActiveRow;
use Nette\Database\Table\Selection;
use Nette\Utils\ArrayHash;

class AdressManager extends DatabaseManager {

	const
		TABLE_NAME = 'adress',
		COLUMN_NAME = 'name',
		COLUMN_ID = 'adress_id',
		TABLE_KEY = 'events',
		COLUMN_KEY_URL = 'url',
		TABLE_CONTACT = 'contacts',
		COLUMN_KEY_ID = 'event_id',
		COLUMN_KEY_ACTIVE = 'active',
		COLUMN_CONTACT_ID = 'contact_id';
	

	public function getAdresses() {

		return $this->database->table(self::TABLE_NAME)->order(self::COLUMN_ID . ' DESC');
	}

	
	public function getAdress($id) {

		return $this->database->table(self::TABLE_NAME)->where(self::COLUMN_ID, $id)->fetch();
	}

	public function getAdressKey($url) {

		$event_id = $this->urlToID($url);

		return $this->database->table(self::TABLE_NAME)->where(self::COLUMN_KEY_ID, $event_id)->fetch();
	}

	public function pocetContact($id) {
		return $this->database->table(self::TABLE_CONTACT)->where(self::COLUMN_ID, $id)->where(self::COLUMN_KEY_ACTIVE, 1)->count();
	}

	public function pocetContactAdmin($id) {
		return $this->database->table(self::TABLE_CONTACT)->where(self::COLUMN_ID, $id)->count();
	}
	
	public function removeAdress($id) {
		$this->database->table(self::TABLE_NAME)->where(self::COLUMN_ID, $id)->delete();
	}

	public function urlToID($url) {

		return $this->database->table(self::TABLE_KEY)->where(self::COLUMN_KEY_URL, $url)->fetch(self::COLUMN_KEY_ID);
	}

	public function idToURL($id) {

		return $this->database->table(self::TABLE_KEY)->where(self::COLUMN_KEY_ID, $id)->fetch(self::COLUMN_KEY_URL);
	}

	public function adresstoContact($adress_id) {

		return $this->database->table(self::TABLE_CONTACT)->where(self::COLUMN_ID, $adress_id)->fetchField(self::COLUMN_CONTACT_ID);
	}

	public function aktivniKontakt($adress_id) {

		$contact_id = $this->adresstoContact($adress_id);

		return $this->database->table(self::TABLE_CONTACT)->where(self::COLUMN_CONTACT_ID, $contact_id)->where(self::COLUMN_KEY_ACTIVE, 1)->fetch(self::COLUMN_KEY_URL);
	}

	#CSV Import
	public function saveCsvData($data, $id, $created_at) {

		$salt = rand();
		$token = sha1($data['mail'].$salt);

		$active = 1;
		$name = $data['name'];
		$mail = $data['mail'];

		$this->database->table(self::TABLE_CONTACT)->insert([
			'mail' => $mail,
			'name' => $name,
			'salt' => $salt,
			'token' => $token,
			'active' => $active,
			'adress_id' => $id,
		]);
		#smazani duplicit
		$this->database->query("DELETE t1 FROM contacts t1 INNER JOIN contacts t2 WHERE t1.contact_id < t2.contact_id AND t1.mail = t2.mail");
	}

	public function provedZapis($form, $event_url, $adress_name, $values) {

	if (empty($values[self::COLUMN_ID])) {
		unset($values[self::COLUMN_ID]);

		$this->database->table(self::TABLE_NAME)->insert([
			'event_id' => $this->urlToID($event_url),
			'name' => $adress_name,
		]);
	} else 
		$this->database->table(self::TABLE_NAME)->where(self::COLUMN_ID, $values->adress_id)->update($values);
	}


}
