<?php
/**
 * 2007-2023 PrestaShop
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@prestashop.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 * versions in the future. If you wish to customize PrestaShop for your
 * needs please refer to http://www.prestashop.com for more information.
 *
 *  @author    PrestaShop SA <contact@prestashop.com>
 *  @copyright 2007-2023 PrestaShop SA
 *  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 *  International Registered Trademark & Property of PrestaShop SA
 */

namespace PrestaShop\Module\AgileMultisellerPro\Helper\Email;

use PrestaShop\Module\AgileMultisellerPro\Entity\Sellerinfo;

if (!defined('_PS_VERSION_')) {
    exit;
}

class SendEmail
{
    private $sellerinfo;
    private $context;

    public function __construct(Sellerinfo $sellerinfo)
    {
        $this->sellerinfo = $sellerinfo;
        $this->context = \Context::getContext();

        if (!$this->context->language) {
            $this->context->language = new \Language((int) \Configuration::get('PS_LANG_DEFAULT'));
        }
    }

    public function sendEmailByType($type)
    {
        $methodName = 'send' . ucfirst($type) . 'Email';

        if (true === method_exists($this, $methodName)) {
            return $this->$methodName();
        }

        return null;
    }

    public function sendNoSubscriptionEmail()
    {
        $templateVars = [
            '{module_name}' => \Module::getInstanceByName('agilemultisellerpro')->displayName,
            '{seller_name}' => $this->sellerinfo->getIdSellerinfo()
                ? $this->sellerinfo->getLangByLangId($this->context->language->id)->getCompany()
                : '',
        ];

        $configuration = \Configuration::getMultiple(['PS_SHOP_EMAIL', 'PS_SHOP_NAME']);

        return AgileMailer::sendTranslateSubject(
            $this->context->language->id,
            'no_subscription',
            $templateVars,
            $configuration['PS_SHOP_EMAIL'],
            'Administrator',
            $configuration['PS_SHOP_EMAIL'],
            $configuration['PS_SHOP_NAME'],
            null,
            null,
            $this->getEmailsDir()
        );
    }

    protected function sendNewSellerAccountEmail()
    {
        if (!$this->sellerinfo || !$this->sellerinfo->getIdSellerinfo()) {
            return;
        }

        $customer = new \Customer($this->sellerinfo->getIdCustomer());

        if (!\Validate::isLoadedObject($customer)) {
            return;
        }

        $configuration = \Configuration::getMultiple(['PS_SHOP_EMAIL', 'PS_SHOP_NAME']);

        $id_lang = $this->context->language->id;
        $iso = $this->context->language->iso_code;

        $aprove_message = '';

        if (!$this->sellerinfo->isActive()) {
            $aprove_message = \Mail::l(
                'You will be automatically notified by email when your account has been approved.'
            );
        } else {
            $aprove_message = \Mail::l('You can access your seller account by: login to front office, ' .
                'go to my seller account then click link to your seller account at back office.');
        }

        $templateVars = [
            '{firstname}' => $customer->firstname,
            '{lastname}' => $customer->lastname,
            '{email}' => $customer->email,
            '{seller_id}' => $this->sellerinfo->getIdSellerinfo(),
            '{seller_company}' => $this->sellerinfo->getLangByLangId($id_lang)->getCompany(),
            '{seller_address}' => $this->getFullAddress($id_lang, $customer),
            '{shop_name}' => \Configuration::get('PS_SHOP_NAME'),
            '{message}' => $aprove_message,
            '{shop_url}' => \Tools::getShopDomainSsl(true, true) . __PS_BASE_URI__,
            '{shop_logo}' => \Tools::getShopDomainSsl(true, true) . __PS_BASE_URI__ . 'img/' . \Configuration::get('PS_LOGO'),
        ];

        $temp_folder = _PS_ROOT_DIR_ . '/modules/agilemultisellerpro/mails/';

        if (
            !file_exists($temp_folder . $iso . '/new_selleraccount.txt')
            || !file_exists($temp_folder . $iso . '/new_selleraccount.html')
        ) {
            $id_lang = (int) Configuration::get('PS_LANG_DEFAULT');
            $iso = Language::getIsoById($id_lang);
        }

        if (
            file_exists($temp_folder . $iso . '/new_selleraccount.txt')
            && file_exists($temp_folder . $iso . '/new_selleraccount.html')
        ) {
            AgileMailer::sendTranslateSubject(
                $id_lang,
                'new_selleraccount',
                $templateVars,
                $customer->email,
                $customer->firstname . ' ' . $customer->lastname,
                $configuration['PS_SHOP_EMAIL'],
                $configuration['PS_SHOP_NAME'],
                null,
                null,
                $temp_folder
            );
        }

        if (
            !file_exists($temp_folder . $iso . '/new_selleraccount_admin.txt')
            || !file_exists($temp_folder . $iso . '/new_selleraccount_admin.html')
        ) {
            $id_lang = (int) Configuration::get('PS_LANG_DEFAULT');
            $iso = Language::getIsoById($id_lang);
        }

        if (
            file_exists($temp_folder . $iso . '/new_selleraccount_admin.txt')
            && file_exists($temp_folder . $iso . '/new_selleraccount_admin.html')
        ) {
            AgileMailer::sendTranslateSubject(
                $id_lang,
                'new_selleraccount_admin',
                $templateVars,
                $configuration['PS_SHOP_EMAIL'],
                'Administrator',
                $configuration['PS_SHOP_EMAIL'],
                $configuration['PS_SHOP_NAME'],
                null,
                null,
                $temp_folder
            );
        }
    }

    protected function getFullAddress($id_lang, $customer)
    {
        $sellerInfoLang = $this->sellerinfo->getLangByLangId($id_lang);

        $address = new \Address();
        $address->id_customer = 0;
        $address->id_manufacturer = 0;
        $address->id_supplier = 0;
        $address->id_warehouse = 0;
        $address->id_country = $this->sellerinfo->getIdCountry();
        $address->id_state = $this->sellerinfo->getIdState();
        $address->company = $sellerInfoLang->getCompany();
        $address->lastname = $customer->lastname;
        $address->firstname = $customer->firstname;
        $address->address1 = $sellerInfoLang->getAddress1();
        $address->address2 = $sellerInfoLang->getAddress2();
        $address->postcode = $this->sellerinfo->getPostcode();
        $address->city = $sellerInfoLang->getCity();
        $address->phone = $this->sellerinfo->getPhone();
        $address->phone_mobile = $this->sellerinfo->getPhone();
        $address->dni = $this->sellerinfo->getDni();
        $address->date_add = $this->sellerinfo->getDateAdd();
        $address->date_upd = $this->sellerinfo->getDateUpd();

        return \AddressFormat::generateAddress($address);
    }

    private function getEmailsDir()
    {
        return _PS_ROOT_DIR_ . '/modules/agilemultisellerpro/mails/';
    }
}
