<?php

namespace App\Classes\File;

use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Support\Facades\Storage;

use App\Models\Address;

final class Item extends Receive
{
	/** @var Address The address that sent us this item */
	private Address $ao;
	private string $recvas;
	private int $recvmtime;
	private int $recvsize;

	/**
	 * @throws \Exception
	 */
	public function __construct(Address $ao,string $recvas,int $mtime,int $size)
	{
		parent::__construct();

		$this->ao = $ao;
		$this->recvas = $recvas;
		$this->recvmtime = $mtime;
		$this->recvsize = $size;

		$this->ftype = self::IS_FILE;
	}

	public function __get($key) {
		switch ($key) {
			case 'exists':
				return Storage::disk(config('fido.local_disk'))->exists($this->rel_name);

			case 'pref_name':
				return sprintf('%04X-%d-%s',$this->ao->id,$this->recvmtime,$this->recvas);
			case 'rel_name':
				return sprintf('%s/%s',config('fido.dir'),$this->pref_name);
			case 'full_name':
				return Storage::disk(config('fido.local_disk'))->path($this->rel_name);

			case 'match_mtime':
				return $this->mtime === $this->recvmtime;
			case 'match_size':
				return $this->size === $this->recvsize;

			case 'nameas':
				return $this->recvas;
			case 'recvmtime':
				return $this->recvmtime;
			case 'recvsize':
				return $this->recvsize;

			case 'name_size_time':
				return sprintf('%s %lu %lu',$this->recvas,$this->recvsize,$this->recvmtime);

			case 'mtime':
				return Storage::disk(config('fido.local_disk'))->lastModified($this->rel_name);

			case 'size':
				return Storage::disk(config('fido.local_disk'))->size($this->rel_name);

			default:
				return parent::__get($key);
		}
	}
}