<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;

use App\Classes\File;
use App\Classes\FTN\Packet;
use App\Models\{Address,Echomail};

class PacketInfo extends Command
{
	/**
	 * The name and signature of the console command.
	 *
	 * @var string
	 */
	protected $signature = 'packet:info'
		.' {file : Packet to process}'
		.' {ftn? : FTN the packet is from}';

	/**
	 * The console command description.
	 *
	 * @var string
	 */
	protected $description = 'Packet Information';

	/**
	 * Execute the console command.
	 *
	 * @return mixed
	 * @throws \App\Exceptions\InvalidPacketException
	 */
	public function handle()
	{
		$fs = Storage::disk(config('fido.local_disk'));
		$rel_name = sprintf('%s/%s',config('fido.dir'),$this->argument('file'));
		$a = NULL;

		$f = new File($fs->path($rel_name));

		$m = NULL;
		if ($this->argument('ftn')) {
			$a = Address::findFTN($this->argument('ftn'));

		} elseif (preg_match(sprintf('/^%s\.(.{3})$/',Packet::regex),$this->argument('file'),$m)) {
			$a = Address::findOrFail(hexdec($m[1]));
		}

		foreach ($f as $packet) {
			$pkt = Packet::process($packet,$x=$f->itemName(),$f->itemSize(),$a?->zone->domain);

			$this->alert(sprintf('File Name: %s',$x));

			$this->info(sprintf('Packet Type : %s (%s)',$pkt->type,get_class($pkt)));
			$this->info(sprintf('From        : %s to %s',$pkt->fftn->ftn,$pkt->tftn->ftn));
			$this->info(sprintf('Dated       : %s (%s) [%s]',$pkt->date,$pkt->date->timestamp,$pkt->date->tz->toOffsetName()));
			$this->info(sprintf('Password    : %s (%s)',$pkt->password ?: '-',$pkt->password ? 'SET' : 'NOT set'));
			$this->info(sprintf('Messages    : %d',$pkt->count()));
			$this->info(sprintf('Tosser      : %d (%s) version %s',$pkt->software->code,$pkt->software->name,$pkt->software_ver));
			$this->info(sprintf('Capabilities: %x',$pkt->capability));
			$this->info(sprintf('Has Errors  : %s',$pkt->errors->count() ? 'YES' : 'No'));
			$this->info(sprintf('Messages    : %d',$pkt->count()));

			foreach ($pkt as $msg) {
				echo "\n";

				try {
					$this->warn(sprintf('- Date     : %s (%s)',$msg->datetime,$msg->datetime->tz->toOffsetName()));
					$this->warn(sprintf('  - Errors : %s',$msg->errors->count() ? 'YES' : 'No'));
					$this->warn(sprintf('  - Flags  : %s',$msg->flags()->keys()->join(', ')));
					$this->warn(sprintf('  - Cost   : %d',$msg->cost));
					$this->warn(sprintf('  - From   : %s (%s)',$msg->from,$msg->fftn->ftn));
					if ($msg instanceof Echomail)
						$this->warn(sprintf('  - To     : %s',$msg->to));
					else
						$this->warn(sprintf('  - To     : %s (%s)',$msg->to,$msg->tftn->ftn));
					$this->warn(sprintf('  - Subject: %s',$msg->subject));
					if ($msg instanceof Echomail)
						$this->warn(sprintf('  - Area   : %s',$msg->echoarea->name));

					if ($msg->errors->count()) {
						echo "\n";
						$this->error("Errors:");
						foreach ($msg->errors->all() as $error)
							$this->error('  - '.$error);
					}

				} catch (\Exception $e) {
					$this->error('! ERROR: '.$e->getMessage());
					$this->info('Message dump:');
					echo hex_dump($msg->dump);
					exit(1);
				}
			}

			foreach ($pkt->errors as $msg) {
				$this->error(sprintf('- Date: %s',$msg->date));
				$this->error(sprintf('  - FLAGS: %s',$msg->flags()->filter()->keys()->join(', ')));
				$this->error(sprintf('  - From: %s (%s)',$msg->from,$msg->fftn));
				$this->error(sprintf('  - To: %s (%s)',$msg->to,$msg->tftn));
				$this->error(sprintf('  - Subject: %s',$msg->subject));

				foreach ($msg->errors->all() as $error)
					$this->line('  - '.$error);
			}

			$this->line("\n");
		}
	}
}