<?php

namespace app\foreignone\files;

use DateTime;
use engine\Model;

class MDFIssue
    extends Model
    implements FileInterface
{
    const DB_TABLE = 'mdf_collection';

    public int $id;
    public int $issue;
    public string $path;
    public string $date;
    public int $volume;
    public int $pageFrom;
    public int $pageTo;
    public int $pdfPages;
    public int $size;
    public string $type;

    public function getTitleHtml(): ?string {
        return null;
    }

    public function getTitle(): string {
        return "№{$this->issue}, {$this->getHumanFriendlyDate()}";
    }

    public function getHumanFriendlyDate(): string {
        $dt = new DateTime($this->date);
        return $dt->format('j M Y');
    }

    public function isTargetBlank(): bool {
        return true;
    }

    public function getId(): string {
        return $this->id;
    }

    public function getUrl(): string {
        global $config;
        return 'https://'.$config['files_domain'].'/Mercure-de-France-OCR/'.$this->path;
    }

    public function getMeta(?string $hl_matched = null): array {
        return [
            'inline' => true,
            'items' => [
                'Vol. '.$this->getRomanVolume(),
                'pp. '.$this->pageFrom.'-'.$this->pageTo,
                sizeString($this->size),
                'PDF'
            ]
        ];
    }

    public function getRomanVolume(): string {
        $number = $this->volume;
        $map = [
            1000 => 'M',
            900 => 'CM',
            500 => 'D',
            400 => 'CD',
            100 => 'C',
            90 => 'XC',
            50 => 'L',
            40 => 'XL',
            10 => 'X',
            9 => 'IX',
            5 => 'V',
            4 => 'IV',
            1 => 'I',
        ];
        $result = '';
        foreach ($map as $arabic => $roman) {
            while ($number >= $arabic) {
                $result .= $roman;
                $number -= $arabic;
            }
        }
        return $result;
    }

    public function getSubtitle(): ?string {
        return null;
        //return 'Vol. '.$this->getRomanVolume().', pp. '.$this->pageFrom.'-'.$this->pageTo;
    }

    public function getSize(): ?int {
        return $this->type == 'file' ? $this->size : null;
    }

    public function getIcon(): string {
        return $this->type;
    }

    public function getFullText(): ?string {
        if ($this->type != 'file')
            return null;
        $db = getDB();
        return $db->result($db->query("SELECT text FROM mdf_texts WHERE mdf_id=?", $this->id));
    }


    /**
     * Static methods
     */

    /**
     * @return MDFIssue[]
     */
    public static function getAll(): array {
        $db = getDB();
        $q = $db->query("SELECT * FROM mdf_collection ORDER BY `date`");
        return array_map(static::create_instance(...), $db->fetchAll($q));
    }

    /**
     * @param int[] $ids
     * @return MDFIssue[]
     */
    public static function getIssuesById(array $ids): array {
        $db = getDB();
        $q = $db->query("SELECT * FROM mdf_collection WHERE id IN (".implode(',', $ids).")");
        return array_map(static::create_instance(...), $db->fetchAll($q));
    }
}
