<?php

namespace KupShop\ReturnsBundle\Email;

use KupShop\ReturnsBundle\Util\ReturnsUtil;

class ReturnHandleEmail extends ReturnEmail
{
    protected static $name = 'Vyřízení vratky';
    protected static $type = 'RETURN_HANDLE_EMAIL';

    protected $subject = 'Vratka {KOD_VRATKY} byla vyřízena';
    protected $template = 'email/email_return_handle.tpl';

    public static function attachmentsAllowed()
    {
        return true;
    }

    public function replacePlaceholdersItem($placeholder)
    {
        $result = parent::replacePlaceholdersItem($placeholder);
        $dbcfg = \Settings::getDefault();
        $bankAccShop = "{$dbcfg['bank_account_number']}/{$dbcfg['bank_code']}";
        if (!empty($dbcfg['bank_account_prefix'])) {
            $bankAccShop = "{$dbcfg['bank_account_prefix']}-{$bankAccShop}";
        }

        if (!$result) {
            switch ($placeholder) {
                case 'ZPUSOB_VYRIZENI':
                    $order = $this->prepareOrder();
                    $remaining = $order->getRemainingPayment();
                    $isItem = false;

                    foreach ($order->fetchItems() as $item) {
                        if (!empty($item['id_product']) && $item['pieces'] > 0) {
                            $isItem = true;
                        }
                    }

                    if ($remaining < 0 && $isItem) {
                        // preplatek + item - vracime penize a posilame jinou polozku
                        return replacePlaceholders(
                            translate_shop('handled_message_overpayment_item', 'returns'),
                            [
                                'ORDER_URL' => $order->getUrl(),
                                'ORDER_NO' => $order->order_no,
                            ]
                        );
                    } elseif ($remaining > 0 && $isItem) {
                        // nedoplatek + item - posilame jiny produkt, je potreba ho doplatit
                        return replacePlaceholders(
                            translate_shop($order->getDeliveryType()->getPayment()->hasOnlinePayment() ? 'handled_message_underpayment_item' : 'handled_message_underpayment_item_cod', 'returns'),
                            [
                                'ORDER_URL' => $order->getUrl(),
                                'ORDER_NO' => $order->order_no,
                                'PRICE' => printPrice($remaining),
                                'BANK_ACCOUNT' => $bankAccShop,
                            ]
                        );
                    } elseif ($remaining < 0 && !$isItem) {
                        // preplatek - vracime vam penize
                        return translate_shop('handled_message_overpayment', 'returns');
                    } elseif ($remaining == 0 && $isItem) {
                        return replacePlaceholders(
                            translate_shop('handled_message_new_item', 'returns'),
                            [
                                'ORDER_URL' => $order->getUrl(),
                                'ORDER_NO' => $order->order_no,
                            ]
                        );
                    }

                    return '';
            }
        }

        return $result;
    }

    public function testEmail(): array
    {
        if (!isset($this->return)) {
            if ($returnId = returnSQLResult('SELECT MAX(id) FROM returns WHERE status = '.ReturnsUtil::STATUS_HANDLED)) {
                $return = $this->returnsUtil->getReturn($returnId);
                $this->setReturn($return);
            }
        }

        return parent::testEmail();
    }
}
