<?php

	use VLW\Database\Models\Messages\Message;
	use VLW\Database\Tables\Messages\Messages;

	require_once VV::root("src/Database/Models/Messages/Message.php");
	require_once VV::root("src/Database/Tables/Messages/Messages.php");

	$date = new class extends DateTimeImmutable {
		public function __construct() {
			// Set DateTime for configured timezone
			parent::__construct("now", new DateTimeZone($_ENV["config_time_available"]["time_zone"]));
		}

		// Return current hour in 24-hour format
		private function hour(): int {
			return (int) $this->format("G");
		}

		// Returns true if current time is between available from and to hours
		public function is_available(): bool {
			return $this->hour() >= $_ENV["config_time_available"]["available_from_hour"] && $this->hour() < $_ENV["config_time_available"]["available_to_hour"];
		}

		public function get_estimated_reply_hours(): int {
			// I'm available! Return the estimated reply time for that
			if ($this->is_available()) {
				return $_ENV["config_time_available"]["reply_average_hours"];
			}

			return  $this->hour() < $_ENV["config_time_available"]["available_from_hour"]
				// Return hours past midnight until I become available (clamped to estimated reply hours)
				? max($_ENV["config_time_available"]["available_from_hour"] - $this->hour(), $_ENV["config_time_available"]["reply_average_hours"])
				// Return hours before midnight until I become available (clamped to estimated reply hours)
				: max($_ENV["config_time_available"]["available_from_hour"] + (24 - $this->hour()), $_ENV["config_time_available"]["reply_average_hours"]);
		}
	}

?>
<style><?= VV::css("public/assets/css/pages/contact") ?></style>
<section>
	<h1>Let's chat!</h1>
	<p>The best way to get in touch is definitely by email, or through the form on this page. The time in Sweden is <i><?= $date->format("h:i a") ?></i> right now, I am currently <?= $date->is_available() ? "available" : "not available" ?> and will hopefully reply in about <?= $date->get_estimated_reply_hours() ?> hours.</p>
</section>
<section class="social">
	<a href="mailto:victor@vlw.se"><social>
		<?= VV::embed("public/assets/media/icons/email.svg") ?>
		<p data-hover>e-mail</p>
	</social></a>
	<a href="matrix:@vlw:vlw.se"><social>
		<?= VV::embed("public/assets/media/icons/matrix.svg") ?>
		<p data-hover>matrix</p>
	</social></a>
	<a href="https://web.libera.chat/#vlw.se"><social>
		<?= VV::embed("public/assets/media/icons/libera.svg") ?>
		<p data-hover>libera.chat</p>
	</social></a>
</section>
<?= VV::embed("public/assets/media/line.svg") ?>
<section class="pgp">
	<?= VV::embed("public/assets/media/icons/pin.svg") ?>
	<h3>a note about encryption..</h3>
	<p>Please don't let certain <a href="https://web.archive.org/web/20250514040615/https://www.patrick-breyer.de/en/posts/chat-control/">short-sighted politicians</a> fool you into thinking encrypted text messages are only for <a href="https://en.wikipedia.org/wiki/Nothing_to_hide_argument">"those with nothing to hide"</a>. I encourage you to encrypt your message with PGP so we can aid normalization of <i>platform-agnostic</i> encryption for everyone.</p>
	<p>PGP fingerprint for victor@vlw.se:<br><strong class="fingerprint">0B7665204AF0E77A2B75C8965DAF14C317AA7719</strong></p>
	<div class="buttons">
		<a href="https://keys.openpgp.org/vks/v1/by-fingerprint/0B7665204AF0E77A2B75C8965DAF14C317AA7719"><button class="inline solid download">
			<p>download ASC</p>
			<?= VV::embed("public/assets/media/icons/chevron.svg") ?>
		</button></a>
		<a href="https://emailselfdefense.fsf.org/en/" target="_blank" rel="noopener noreferer"><button class="inline">
			<p>email encryption guide</p>
			<?= VV::embed("public/assets/media/icons/chevron.svg") ?>
		</button></a>
	</div>
</section>
<section class="center fade">
	<blockquote>"Arguing that you don't care about the right to privacy because you have nothing to hide is no different than saying you don't care about free speech because you have nothing to say."</blockquote>
	<p>- Edward Snowden, <cite><a href="https://en.wikiquote.org/wiki/Edward_Snowden#2015">Reddit (May 21, 2015)</a></cite></p>
</section>
<?= VV::embed("public/assets/media/line.svg") ?>

<?php // Send message on POST request ?>
<?php if ($_SERVER["REQUEST_METHOD"] === "POST"): ?>
	<?php $message = Message::new($_POST[Messages::MESSAGE->name] ?? "", $_POST[Messages::EMAIL->name] ?? null); ?>

	<?php if ($message->date_created): ?>
		<section class="form-message sent">
			<h3>🙏 Message sent!</h3>
		</section>
	<?php else: ?>
		<?php // Show response body from endpoint as error if request failed ?>
		<section class="form-message error">
			<h3>😟 Oh no, something went wrong</h3>
		</section>
	<?php endif; ?>
<?php endif; ?>

<section class="form">
	<form method="POST">
		<input-group>
			<label>your email (optional)</label>
			<input type="email" name="<?= Messages::EMAIL->name ?>" placeholder="nissehult@example.com" autocomplete="off"></input>
		</input-group>
		<input-group>
			<label title="this field is required">your message (required)</label>
			<textarea name="<?= Messages::MESSAGE->name ?>" required placeholder="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed molestie dignissim mauris vel dignissim. Sed et aliquet odio, id egestas libero. Vestibulum ut dui a turpis aliquam hendrerit id et dui. Morbi eu tristique quam, sit amet dictum felis. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin ac nibh a ex accumsan ullamcorper non quis eros. Nam at suscipit lacus. Nullam placerat semper sapien, vitae aliquet nisl elementum a. Duis viverra quam eros, eu vestibulum quam egestas sit amet. Duis lobortis varius malesuada. Mauris in fringilla mi. "></textarea>
		</input-group>
		<button class="inline solid">
			<?= VV::embed("public/assets/media/icons/email.svg") ?>
			<p>send message</p>
			<?= VV::embed("public/assets/media/icons/chevron.svg") ?>
		</button>
	</form>
</section>
<script ><?= VV::js("public/assets/js/pages/contact") ?></script>
