<?php

namespace App\Repositories\Core\Mail;

use ZBateson\MailMimeParser\Header\HeaderConsts;
use ZBateson\MailMimeParser\MailMimeParser;

class MailParser
{
    public static function getParsed($mail)
    {
        $model = self::getModel($mail);

        return [
            'from' => self::getFromByModel($model),
            'subject' => self::getSubjectByModel($model),
            'content' => self::getHtmlByModel($model),
            'to_name' => self::getToNameByModel($model),
            'to_email' => self::getToEmailByModel($model),
        ];
    }

    public static function getSubject($mail)
    {
        return self::getSubjectByModel(self::getModel($mail));
    }

    public static function getText($mail)
    {
        return self::getTextByModel(self::getModel($mail));
    }

    public static function getHtml($mail)
    {
        return self::getHtmlByModel(self::getModel($mail));
    }

    public static function getFrom($mail)
    {
        return self::getFromByModel(self::getModel($mail));
    }

    public static function getToName($mail)
    {
        return self::getToNameByModel(self::getModel($mail));
    }

    public static function getToEmail($mail)
    {
        return self::getToEmailByModel(self::getModel($mail));
    }

    public static function getSubjectByModel($model)
    {
        return $model->getHeaderValue(HeaderConsts::SUBJECT);
    }

    public static function getTextByModel($model)
    {
        return $model->getTextContent();
    }

    public static function getFromByModel($model)
    {
        return $model->getHeader(HeaderConsts::FROM)->getPersonName();
    }

    public static function getToNameByModel($model)
    {
        return $model->getHeader(HeaderConsts::TO)->getAddresses()[0]->getName();
    }

    public static function getToEmailByModel($model)
    {
        return $model->getHeader(HeaderConsts::TO)->getAddresses()[0]->getEmail();
    }

    public static function getHtmlByModel($model)
    {
        return $model->getHtmlContent();
    }

    public static function getModel($mail)
    {
        $mailParser = new MailMimeParser();

        return $mailParser->parse($mail, false);
    }
}
