<?php

namespace KupShop\KupShopBundle\Email;

use KupShop\KupShopBundle\Exception\OrderMessageException;

class OrderMessageEmail extends OrderEmail implements UserMessagesInterface
{
    use UserMessagesTrait;

    protected static $name = 'Zprávy uživatelům';
    protected static $type = 'ORDER_MESSAGE';
    protected static $priority = 0;

    protected $subject = 'Změna objednávky {KOD}';
    protected $template = 'email/email_order_change.tpl';

    private $order_messages_types = [];

    protected $order_message; // ORDER_MESSAGE name

    public function setOrderMessage($order_message)
    {
        $this->order_message = $order_message;

        return $this;
    }

    public function getOrderMessages($languageID = null): array
    {
        if (!$languageID && $this->order) {
            $languageID = $this->order->id_language;
        }

        return $this->fetchMessages($languageID);
    }

    public function getOrderStatusMessagesByStatus($status, $languageID = null)
    {
        $order_messages = $this->getOrderMessages($languageID);

        if (!$this->order_messages_types) {
            foreach ($order_messages as &$message) {
                if (!is_null($message['order_status'])) {
                    $this->order_messages_types[$message['order_status']][] = $message;
                }
            }
        }

        if (!empty($this->order_messages_types[$status])) {
            return $this->order_messages_types[$status];
        }

        return [];
    }

    public function getEmailTemplate($languageID = null): array
    {
        $messages = $this->getOrderMessages($languageID);

        if ($this->order_message) {
            foreach ($messages as &$message) {
                if ($message['name'] == $this->order_message) {
                    return $message;
                }
            }
        }

        throw new OrderMessageException(
            sprintf('Error during order message send! Order message "%s" not found!', $this->order_message ?: ''),
            [
                'order_message' => $this->order_message,
                'messages' => $messages,
                'language' => $this->languageContext->getActiveId(),
                'id_order' => $this->order?->id,
            ]
        );
    }

    public function renderEmail($template, $data = [], $base_template = 'order.tpl'): array
    {
        if (!is_null($this->comment)) {
            $template['body'] = $this->comment;
        }

        $message = parent::renderEmail($template, $data, $base_template);

        return $message;
    }

    public static function getPlaceholders()
    {
        if (findModule(\Modules::ORDER_PAYMENT)) {
            $placeholders['ZAPLATIT'] = [
                'text' => translate('RemainsPaid', 'emails'),
            ];
        }

        if (findModule(\Modules::DELIVERY_TYPES)) {
            $placeholders['ODKAZ_SLEDOVANI_BALIKU'] = [
                'text' => translate('LinkToWebpage', 'emails'),
            ];
        }

        $placeholders['BALIK_ID'] = [
            'text' => translate('PackageNumber', 'emails'),
        ];

        return [self::$type => $placeholders] + (parent::getPlaceholders() ?? []);
    }

    public function getMessages($languageID = null): array
    {
        return $this->getOrderMessages($languageID);
    }

    public function setUserMessage(string $message): void
    {
        $this->setOrderMessage($message);
    }

    public function getMessagesByStatus($status, $languageID = null): array
    {
        return $this->getOrderStatusMessagesByStatus($status, $languageID);
    }

    public function getObjectId(): ?int
    {
        return $this->order?->id;
    }
}
