<?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
 */
use PrestaShop\Module\AgileMultisellerPro\Controller\Front\AgileModuleFrontController;
use PrestaShop\Module\AgileMultisellerPro\Entity\Sellerinfo;
use PrestaShop\Module\AgileMultisellerPro\Entity\SellerinfoOrder;
use PrestaShop\Module\AgileMultisellerPro\Entity\SellerinfoProduct;

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

class AgileMultiSellerProSellerSummaryModuleFrontController extends AgileModuleFrontController
{
    public function setMedia()
    {
        parent::setMedia();

        Media::addJsDef([
            'id_language_current' => $this->context->language->id,
        ]);

        $this->context->controller->addJQueryUI('ui.sortable');
        $this->context->controller->addJQueryUI('ui.draggable');
        $this->context->controller->addJQueryUI('effects.transfer');

        $this->registerJavascript(
            'agile_tinymce',
            '/modules/agilemultisellerpro/views/js/agile_tiny_mce.js',
            ['position' => 'bottom', 'priority' => 100]
        );

        $this->registerJavascript(
            'agile_statememneger',
            '/modules/agilemultisellerpro/views/js/AgileStatesManagement.js',
            ['position' => 'bottom', 'priority' => 100]
        );

        $this->registerJavascript(
            'agile_multilanguage',
            '/modules/agilemultisellerpro/views/js/multi-language.js',
            ['position' => 'bottom', 'priority' => 100]
        );

        $this->registerJavascript(
            'agile_sellerpage',
            '/modules/agilemultisellerpro/views/js/sellerpage.js',
            ['position' => 'bottom', 'priority' => 100]
        );

        $this->registerJavascript(
            'agile_sellersummary',
            '/modules/agilemultisellerpro/views/js/sellersummary.js',
            ['position' => 'bottom', 'priority' => 100]
        );
    }

    public function initContent()
    {
        parent::initContent();

        if (!Configuration::get('AGILE_MS_CUSTOMER_SELLER') && !$this->sellerinfo->getIdSellerinfo()) {
            Tools::redirect($this->context->link->getPageLink('my-account', true));
        }

        $cms = new CMS((int) Configuration::get('AGILE_MS_SELLER_TERMS'), (int) $this->context->language->id);
        $link_terms = $this->context->link->getCMSLink($cms, $cms->link_rewrite, true);

        if (!strpos($link_terms, '?')) {
            $link_terms .= '?content_only=1';
        } else {
            $link_terms .= '&content_only=1';
        }

        $this->context->smarty->assign([
            'link_terms' => $link_terms,
            'id_cms_seller_terms' => (int) Configuration::get('AGILE_MS_SELLER_TERMS'),
        ]);

        $account_balance = 0;
        $paycommission_url = '';

        // if (Module::isInstalled('agilesellercommission')) {
        //     include_once(_PS_ROOT_DIR_ . '/modules/agilesellercommission/agilesellercommission.php');
        //     include_once(_PS_ROOT_DIR_ . '/modules/agilesellercommission/SellerCommission.php');

        //     $account_balance = AgileSellerManager::getAccountBalance($this->sellerinfo->id_seller);
        //     $paydata = array(
        //         'record_type' => SellerCommission::RECORD_TYPE_SELLER_PAY_STORE,
        //         'id_seller' => $this->sellerinfo->id_seller,
        //         'amount_to_pay' => (-$account_balance),
        //         'retkey' => Tools::getValue('token'),
        //         'paykey' => Tools::encrypt($this->sellerinfo->id_seller),
        //         'isfront' => 1
        //     );
        //     $paycommission_url = AgileSellerCommission::getPayCommissionUrl($paydata);
        // }

        $token_balance = 0;
        $numOfProducts = 0;
        $numOfOrders = 0;

        $totals_sold = $this->entityManager
            ->getRepository(SellerinfoOrder::class)
            ->getTotalAmountSold($this->sellerinfo->getIdSellerinfo());

        if ($this->isSeller) {
            $numOfProducts = $this->entityManager
                    ->getRepository(SellerinfoProduct::class)
                    ->getCountProducts($this->sellerinfo);

            $numOfOrders = $this->entityManager
                ->getRepository(SellerinfoOrder::class)
                ->count(['sellerInfo' => $this->sellerinfo]);
        }

        $this->context->smarty->assign([
            'seller_tab_id' => 1,
            'num_products' => $numOfProducts,
            'num_orders' => $numOfOrders,
            'totals_sold' => $totals_sold,
            'comcurrency' => new Currency((int) Configuration::get('ASC_COMMISSION_CURRENCY')),
            'paycommission_url' => $paycommission_url,
            'account_balance' => $account_balance,
            'token_balance' => $token_balance,
            'id_language_current' => $this->context->language->id,
            'membership_module_integrated' => (Module::isInstalled('agilemembership') && (int) Configuration::get('AGILE_MEMBERSHIP_SELLER_INTE') > 0) ? 1 : 0,
            'request_T2B' => $this->trans('CHANGE TOKENS TO BALANCE', [], 'Modules.Agilemultisellerpro.Summary'),
            'request_B2T' => $this->trans('CHANGE BALANCE TO TOKENS', [], 'Modules.Agilemultisellerpro.Summary'),
            'request_MPR' => $this->trans('MAKE A PEYMENT REQUEST', [], 'Modules.Agilemultisellerpro.Summary'),
            'use_paypal_forsellerpayment' => (int) Configuration::get('ASC_USE_PAYPAL'),
        ]);

        $this->setTemplate('module:agilemultisellerpro/views/templates/front/sellersummary.tpl');
    }

    public function postProcess()
    {
        $this->context->smarty->assign([
            'cfmmsg_flag' => Tools::getValue('cfmmsg_flag'),
        ]);

        if (Tools::getValue('submitSellerAccount')) {
            $this->module->createSellerAccount(new Customer($this->context->customer->id), new Sellerinfo());

            Tools::redirect($this->context->link->getModuleLink(
                'agilemultisellerpro',
                'sellersummary',
                ['cfmmsg_flag' => 1],
                true
            ));
        }
    }
}
