<?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\Controller\Front\Traits;

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

trait SellerTrait
{
    private function getCountries()
    {
        if (\Configuration::get('PS_RESTRICT_DELIVERED_COUNTRIES')) {
            $countries = \Carrier::getDeliveredCountries($this->context->language->id, true, true);
        } else {
            $countries = \Country::getCountries($this->context->language->id, true);
        }

        return $countries;
    }

    private function getSelectedCountry()
    {
        if (
            \Tools::isSubmit('id_country')
            && !is_null(\Tools::getValue('id_country'))
            && is_numeric(\Tools::getValue('id_country'))
        ) {
            $selected_country = (int) \Tools::getValue('id_country');
        } elseif (
            $this->sellerinfo
            && !empty($this->sellerinfo->getIdCountry())
            && is_numeric($this->sellerinfo->getIdCountry())
        ) {
            $selected_country = (int) $this->sellerinfo->getIdCountry();
        } elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
            $array = preg_split('/,|-/', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
            if (!\Validate::isLanguageIsoCode($array[0]) || !($selected_country = \Country::getByIso($array[0]))) {
                $selected_country = (int) \Configuration::get('PS_COUNTRY_DEFAULT');
            }
        } else {
            $selected_country = (int) \Configuration::get('PS_COUNTRY_DEFAULT');
        }

        return $selected_country;
    }

    private function getSelectedState()
    {
        $selected_state = 0;
        if (
            \Tools::isSubmit('id_state')
            && !is_null(\Tools::getValue('id_state'))
            && is_numeric(\Tools::getValue('id_state'))
        ) {
            $selected_state = (int) \Tools::getValue('id_state');
        } elseif (
            $this->sellerinfo
            && !empty($this->sellerinfo->getIdState())
            && is_numeric($this->sellerinfo->getIdState())
        ) {
            $selected_state = (int) $this->sellerinfo->getIdState();
        }

        return (int) $selected_state;
    }

    protected function assignCountries()
    {
        $this->context->smarty->assign([
            'countries' => $this->getCountries(),
        ]);
    }

    protected function assignVatNumber()
    {
        $vat_number_exists = file_exists(_PS_MODULE_DIR_ . 'vatnumber/vatnumber.php');
        $vat_number_management = \Configuration::get('VATNUMBER_MANAGEMENT');

        if ($vat_number_management && $vat_number_exists) {
            include_once _PS_MODULE_DIR_ . 'vatnumber/vatnumber.php';
        }

        if (
            $vat_number_management
            && $vat_number_exists
            && \VatNumber::isApplicable(\Configuration::get('PS_COUNTRY_DEFAULT'))
        ) {
            $vat_display = 2;
        } elseif ($vat_number_management) {
            $vat_display = 1;
        } else {
            $vat_display = 0;
        }

        $this->context->smarty->assign([
            'vatnumber_ajax_call' => file_exists(_PS_MODULE_DIR_ . 'vatnumber/ajax.php'),
            'vat_display' => $vat_display,
        ]);
    }

    protected function assignAddressFormat()
    {
        $id_country = $this->sellerinfo ? 0 : $this->sellerinfo->getIdCountry();
        $dlv_adr_fields = \AddressFormat::getOrderedAddressFields($id_country, true, true);
        $this->context->smarty->assign('ordered_adr_fields', $dlv_adr_fields);
    }

    private function getAllTypes()
    {
        $id_lang = (int) $this->context->language->id;

        $result[] = [
            'id_sellertype' => 0,
            'name' => $this->context->getTranslator()->trans('Please choose seller type', [], 'Modules.Agilemultisellerpro.Seller'),
        ];

        $types = $this->entityManager->getRepository(\Sellertype::class);

        foreach ($types->findAll() as $sellerType) {
            $result[] = [
                'id_sellertype' => $sellerType->getIdSellerType(),
                'name' => $sellerType->getLangByLangId($id_lang)->getName(),
            ];
        }

        return $result;
    }
}
