<?php

/* * ********************************************************************************** */
/*      This file is part of the Thelia package.                                     */
/*                                                                                   */
/*      Copyright (c) OpenStudio                                                     */
/*      email : dev@thelia.net                                                       */
/*      web : http://www.thelia.net                                                  */
/*                                                                                   */
/*      For the full copyright and license information, please view the LICENSE.txt  */
/*      file that was distributed with this source code.                             */
/* * ********************************************************************************** */

namespace ArtabanMarketplace;

use Thelia\Module\BaseModule;
use Propel\Runtime\Connection\ConnectionInterface;
use Thelia\Install\Database;
use Thelia\Model\Resource;
use Thelia\Model\ResourceQuery;
use Thelia\Model\ConfigQuery;
use Thelia\Core\Template\TemplateDefinition;
use Thelia\Model\Message;
use Thelia\Model\MessageQuery;
use Thelia\Model\MessageI18n;
use Thelia\Model\MessageI18nQuery;

//use Thelia\Module\BaseModuleInterface;

class ArtabanMarketplace extends BaseModule {

    /** @var string */
    const DOMAIN_NAME = 'artabanmarketplace';
    const MESSAGE_DOMAIN = 'artabanmarketplace';
    const RESOURCES_OWNER = "admin.artabanmarketplace.owner";
    const RESOURCES_ADMINISTRATOR = "admin.artabanmarketplace.administrator";

    //const RESOURCES_MODULE = "admin.artabanmarketplace.module";

    public function postActivation(ConnectionInterface $con = null) {

        $database = new Database($con);
        $database->insertSql(null, [__DIR__ . '/Config/thelia.sql']);

        $this->addResource(self::RESOURCES_OWNER);
        $this->addResource(self::RESOURCES_ADMINISTRATOR);
        //$this->addResource(self::RESOURCES_MODULE);
        //Initialize the module_config
        //self::setConfigValue(self::CONFIG_ALLOW_PROFILE_ID, '');
        ConfigQuery::write('pdf_invoice_general_file', 'invoice_general');
        ConfigQuery::write('pdf_delivery_general_file', 'delivery_general');
        $this->addMessageNotificafionOrderSeller();
//        $baseModuleInterface = new BaseModuleInterface();
//        $baseModuleInterface
    }

    protected function addResource($code) {
        if (null === ResourceQuery::create()->findOneByCode($code)) {
            $resource = new Resource();
            $resource->setCode($code);
            $resource->setTitle($code);
            $resource->save();
        }
    }

    protected function addMessageNotificafionOrderSeller() {

        $messageNotificafionSellerRegisteredTest = MessageQuery::create()->findOneByName('seller_order_notification');

        if (!$messageNotificafionSellerRegisteredTest) {
            $messageNotificafionSeller = new Message();
            $messageNotificafionSeller->setName('seller_order_notification');
            $messageNotificafionSeller->setHtmlTemplateFileName('seller_order_notification.html');
            $messageNotificafionSeller->setTextTemplateFileName('seller_order_notification.txt');
            $messageNotificafionSeller->save();

            $messageNotificafionSellerRegistered = MessageQuery::create()->findOneByName('seller_order_notification');
            $idMessage = $messageNotificafionSellerRegistered->getId();

            $messageNotificafionSellerI18nFr = new MessageI18n();
            $messageNotificafionSellerI18nFr->setId($idMessage);
            $messageNotificafionSellerI18nFr->setLocale("fr_FR");
            $messageNotificafionSellerI18nFr->setTitle("Message envoyé au marchand lors d'une nouvelle commande.");
            $messageNotificafionSellerI18nFr->setSubject('Nouvelle commande {$order_ref} reçue sur {config key="store_name"}');
            $messageNotificafionSellerI18nFr->save();

            $messageNotificafionSellerI18nEn = new MessageI18n();
            $messageNotificafionSellerI18nEn->setId($idMessage);
            $messageNotificafionSellerI18nEn->setLocale("en_US");
            $messageNotificafionSellerI18nEn->setTitle('Message sent to seller when a new order is placed');
            $messageNotificafionSellerI18nEn->setSubject('New order {$order_ref} placed on {config key="store_name"}');
            $messageNotificafionSellerI18nEn->save();
        }
    }

    public static function getAmpSellerImagesFolder() {
        return sprintf(
                "%s%s/amp_seller", THELIA_ROOT, ConfigQuery::read('images_library_path', 'local' . DS . 'media' . DS . 'images')
        );
    }

    public function getHooks() {
        return array(
            array(
                "type" => TemplateDefinition::BACK_OFFICE,
                "code" => "home.top-info-for-seller",
                "title" => array(
                    "fr_FR" => "Page d'accueil - en haut, exclusivement pour les marchands",
                    "en_US" => "Home - Top, exclusively for sellers",
                ),
                "block" => true,
                "active" => true
            )
        );
    }

}
