<?php

namespace Planaday\Database\SQLDatabaseControllers\Course;

use DateTime;
use DateTimeZone;
use Planaday\Entities\CostsData;
use Planaday\Entities\Course\DTO\Course;
use Planaday\Entities\Course\DTO\CourseUsersData;

class CourseDatabaseObject {
	public static function getDatabaseArrayFromCourse(Course $course): array{
		return array(
			CourseColumnEnum::ID => $course->getId(),
			CourseColumnEnum::CODE => $course->getCode(),
			CourseColumnEnum::NAME => $course->getName(),
			CourseColumnEnum::DESCRIPTION => $course->getDescription(),
			CourseColumnEnum::TYPE => $course->getType(),
			CourseColumnEnum::STATUS => $course->getType(),
			CourseColumnEnum::DAY_PART_AMOUNT => $course->getDayPartAmount(),
			CourseColumnEnum::USERS_MIN => $course->getUsersData()->getMin(),
			CourseColumnEnum::USERS_MAX => $course->getUsersData()->getMax(),
			CourseColumnEnum::USERS_SCHEDULED => $course->getUsersData()->getScheduled(),
			CourseColumnEnum::USERS_AVAILABLE => $course->getUsersData()->getAvailable(),
			CourseColumnEnum::USERS_OPTIONS => $course->getUsersData()->getOptions(),
			CourseColumnEnum::COST_COST => $course->getCostsData()->getCost(),
			CourseColumnEnum::COST_COURSE => $course->getCostsData()->getCourse(),
			CourseColumnEnum::COST_VAT => $course->getCostsData()->getVat(),
			CourseColumnEnum::COST_VAT_CODE => $course->getCostsData()->getVatCode(),
			CourseColumnEnum::COST_REMARK => $course->getCostsData()->getRemark(),
			CourseColumnEnum::TEMPLATE_ID => $course->getTemplateId(),
			CourseColumnEnum::LEVEL => $course->getLevel(),
			CourseColumnEnum::LEVEL_DESCRIPTION => $course->getLevelDescription(),
			CourseColumnEnum::START_GUARANTEED => $course->isStartGuaranteed(),
			CourseColumnEnum::MONEY_BACK_GUARANTEED => $course->isMoneyBackGuaranteed(),
			CourseColumnEnum::HAS_E_LEARNING => $course->isHasELearning(),
			CourseColumnEnum::HAS_CODE95 => $course->isHasCode95(),
			CourseColumnEnum::HAS_SOOB => $course->isHasSoob(),
			CourseColumnEnum::HAS_STAP => $course->isHasStap(),
			CourseColumnEnum::STAP_ONLY => $course->isStapOnly(),
			CourseColumnEnum::LABELS => '|' . implode('|', $course->getLabels()) . '|',
			CourseColumnEnum::LANGUAGE => $course->getLanguage(),
			CourseColumnEnum::LAST_UPDATE => (new DateTime())->setTimestamp(time() - $course->getAge())->format('Y-m-d H:i:s')
		);
	}

	public static function getCourseObject(array $data): Course{
		$lastUpdatedTime = DateTime::createFromFormat(
			"Y-m-d H:i:s",
			$data[CourseColumnEnum::LAST_UPDATE],
			new DateTimeZone('Europe/Amsterdam')
		);

		if ( $lastUpdatedTime === false )
			$lastUpdatedTime = (new DateTime())->setTimestamp(0);

		return (new Course($data[CourseColumnEnum::ID]))
			->setCode($data[CourseColumnEnum::CODE])
			->setName($data[CourseColumnEnum::NAME])
			->setDescription($data[CourseColumnEnum::DESCRIPTION])
			->setType($data[CourseColumnEnum::TYPE])
			->setStatus($data[CourseColumnEnum::STATUS])
			->setDaypartAmount($data[CourseColumnEnum::DAY_PART_AMOUNT])
			->setUsersData(
				(new CourseUsersData())
					->setMin($data[CourseColumnEnum::USERS_MIN])
					->setMax($data[CourseColumnEnum::USERS_MAX])
					->setScheduled($data[CourseColumnEnum::USERS_SCHEDULED])
					->setAvailable($data[CourseColumnEnum::USERS_AVAILABLE])
					->setOptions($data[CourseColumnEnum::USERS_OPTIONS])
			)
			->setCostsData(
				(new CostsData())
				->setCost($data[CourseColumnEnum::COST_COST])
				->setVat($data[CourseColumnEnum::COST_VAT])
				->setVatCode($data[CourseColumnEnum::COST_VAT_CODE])
				->setRemark($data[CourseColumnEnum::COST_REMARK])
				->setCourse($data[CourseColumnEnum::COST_COURSE])
			)
			->setTemplateId($data[CourseColumnEnum::TEMPLATE_ID])
			->setLevel($data[CourseColumnEnum::LEVEL])
			->setLevelDescription($data[CourseColumnEnum::LEVEL_DESCRIPTION])
			->setStartGuaranteed($data[CourseColumnEnum::START_GUARANTEED])
			->setMoneybackGuaranteed($data[CourseColumnEnum::MONEY_BACK_GUARANTEED])
			->setHasElearning($data[CourseColumnEnum::HAS_E_LEARNING])
			->setHasCode95($data[CourseColumnEnum::HAS_CODE95])
			->setHasSoob($data[CourseColumnEnum::HAS_SOOB])
			->setHasStap($data[CourseColumnEnum::HAS_STAP])
			->setStapOnly($data[CourseColumnEnum::STAP_ONLY])
			->setLabels(
				empty($data[CourseColumnEnum::LABELS]) || empty(substr($data[CourseColumnEnum::LABELS], 1, -1)) ?
					[] :
					explode('|', substr($data[CourseColumnEnum::LABELS], 1, -1))
			)
			->setLanguage($data[CourseColumnEnum::LANGUAGE])
			->setLastUpdate($lastUpdatedTime);
	}
}