<?php

namespace YSquare\GiftCert\Cron;

use OxidEsales\Eshop\Application\Model\Article;
use OxidEsales\Eshop\Application\Model\Order;
use OxidEsales\Eshop\Core\Email;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface;
use YSquare\GiftCert\Core\GiftCertHandler;
use YSquare\GiftCert\Model\GiftCert;

class GiftCertJobs {

    /**
     * @var mixed
     */
    protected $queryBuilderFactory;

    /**
     * GiftCertHandler constructor.
     */
    public function __construct() {
        $container = ContainerFactory::getInstance()->getContainer();
        $this->queryBuilderFactory = $container->get(QueryBuilderFactoryInterface::class);
    }

    /**
     *
     */
    public function sendGiftcerts() {

        $queryBuilder = $this->queryBuilderFactory->create();
        $queryBuilder
            ->select('*')
            ->from("ysgiftcert")
            ->where('ysdelivered = 0')
            ->andWhere('ysdeliverydate <= :deliverydate')
            ->setParameter("deliverydate", date("Y-m-d"));

        $blocksData = $queryBuilder->execute();
        $blocksData = $blocksData->fetchAll();

        foreach($blocksData as $entry) {
            $giftcert = oxNew(GiftCert::class);
            if($giftcert->load($entry["OXID"])){
                $options = json_decode($entry["YSINDIVIDUALOPTIONS"], true);

                // If individual Options are empty this giftcert should be delivered to the order owner or not at all (because it already should have been delivered)? TODO: check for the behaviour
                if (trim($entry["YSINDIVIDUALOPTIONS"]) == "" || !isset($options["recipientMail"]) || trim($options["recipientMail"]) == "") {
                    $giftcert->markAsDelivered();
                    continue;
                }

                // If this is a business giftcert it should never be send by email
                if(isset($options["isBusinessGiftcert"]) && $options["isBusinessGiftcert"] = 1) {
                    $giftcert->markAsDelivered();
                    continue;
                }

                // load order info
                $order = oxNew(Order::class);
                if($order->load($giftcert->ysgiftcert__oxorderid->value)) {
                    $shopid = $order->oxorder__oxshopid->value;
                    $langid = $order->oxorder__oxlang->value;
                    $purchaserFirstName = $order->oxorder__oxbillfname->value;
                    $purchaserLastName = $order->oxorder__oxbilllname->value;
                }

                // Produce the PDF
                $giftcertHandler = oxNew(GiftCertHandler::class);

                $filesToAttach=[];
                $filesToAttach[] = $giftcertHandler->generateAttachmentPDF($giftcert);

                $giftcert->markAsDelivered();

                // Send eMail to Recipient
                $email = oxNew(Email::class);
                $email->sendGiftcertEmailToRecipient($giftcert, $filesToAttach, $langid, $shopid, $purchaserFirstName, $purchaserLastName);
                // Send eMail to order owner
                $email = oxNew(Email::class);
                $email->sendGiftcertEmailToPurchaser($giftcert, $filesToAttach, $langid, $shopid);
                // Send eMail to shop owner
                $email = oxNew(Email::class);
                $email->sendGiftcertEmailToShopOwner($giftcert, $filesToAttach, $langid, $shopid);

                //delete files
                foreach($filesToAttach as $file){
                    unlink($file);
                }
            }
        }
    }
}
