<?php

namespace App\Notifications;

use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Log;

use App\Classes\{ANSI,Fonts\Thin,Page};
use App\Classes\Fonts\Thick;
use App\Classes\FTN\Message;
use App\Models\{Address,Netmail,Setup,User};

class NetmailTest extends Notification //implements ShouldQueue
{
	private const LOGKEY = 'NNT';

	use Queueable;

	private Address $ao;

	/**
	 * Create a new notification instance.
	 *
	 * @param Address $ao
	 * @param User $uo
	 */
	public function __construct(Address $ao)
	{
		$this->queue = 'netmail';
		$this->ao = $ao;
	}

	/**
	 * Get the notification's delivery channels.
	 *
	 * @param  mixed  $notifiable
	 * @return array
	 */
	public function via($notifiable)
	{
		return ['netmail'];
	}

	/**
	 * Get the mail representation of the notification.
	 *
	 * @param mixed $notifiable
	 * @return Netmail
	 * @throws \Exception
	 */
	public function toNetmail($notifiable): Netmail
	{
		Log::info(sprintf('%s:Creating test netmail to [%s]',self::LOGKEY,$this->ao->ftn));

		$so = Setup::findOrFail(config('app.id'))->system;

		$o = new Netmail;
		$o->to = $this->ao->system->sysop;
		$o->from = Setup::PRODUCT_NAME;
		$o->subject = 'Testing 1, 2, 3...';
		$o->datetime = Carbon::now();
		$o->tzoffset = $o->datetime->utcOffset();

		$o->fftn_id = $so->match($this->ao->zone)->first();
		$o->tftn_id = $this->ao->id;
		$o->flags = Message::FLAG_LOCAL;
		$o->cost = 0;

		$o->tagline = 'Testing, testing, 1 2 3.';
		$o->tearline = sprintf('%s (%04X)',Setup::PRODUCT_NAME,Setup::PRODUCT_ID);

		// Message
		$msg = new Page;
		$msg->addLogo(new ANSI('public/logo/netmail.bin'));

		$header = new Thick;
		$header->addText(ANSI::ansi_code([1,37]).'Clearing Houz');
		$msg->addHeader($header,'FTN Mailer and Tosser',TRUE,0xc4);

		$lbc = new Thin;
		$lbc->addText('Test');
		$msg->addLeftBoxContent($lbc);

		$msg->addText(
			"Hi there,\r\r".
			"This is just a test netmail to make sure we can send a message to your system.\r\r".
			"There is no need to reply, but if you do, it'll boost my spirits :)\r"
		);

		$o->msg = $msg->render();
		$o->save();

		return $o;
	}
}