<?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
 */
declare(strict_types=1);

namespace PrestaShop\Module\AgileMultisellerPro\Form\Sellerinfo;

use PrestaShop\Module\AgileMultisellerPro\Form\Configuration\DataConfiguration;
use PrestaShop\Module\AgileMultisellerPro\Helper\AgileHelper;
use PrestaShop\Module\AgileMultisellerPro\Repository\SellerInfoRepository;
use PrestaShop\PrestaShop\Core\Form\IdentifiableObject\DataProvider\FormDataProviderInterface;
use Symfony\Component\HttpFoundation\File\File;

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

class DataProvider implements FormDataProviderInterface
{
    /**
     * @var SellerInfoRepository
     */
    private $repository;

    /**
     * @param SellerInfoRepository $repository
     */
    public function __construct(SellerInfoRepository $repository)
    {
        $this->repository = $repository;
    }

    /**
     * {@inheritdoc}
     */
    public function getData($sellerinfoId)
    {
        $sellerInfo = $this->repository->findOneBy(['idSellerinfo' => $sellerinfoId]);

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

        $data = [
            'id_sellerinfo' => $sellerInfo->getIdSellerinfo(),
            'email' => $customer->email,
            'active' => $sellerInfo->isActive(),
            'id_customer' => $sellerInfo->getIdCustomer(),
            'id_category_default' => $sellerInfo->getIdCategoryDefault(),
            'id_shop' => $sellerInfo->getIdShop(),
            'payment_collection' => (int) $sellerInfo->isPaymentCollection(),
            'id_sellertype1' => $sellerInfo->getIdSellertype1(),
            'id_sellertype2' => $sellerInfo->getIdSellertype2(),
            'logo' => new File(AgileHelper::getImageDirectory($sellerInfo->getIdSellerinfo(), 'logo'), false),
            'postcode' => $sellerInfo->getPostcode(),
            'id_country' => $sellerInfo->getIdCountry(),
            'phone' => $sellerInfo->getPhone(),
            'fax' => $sellerInfo->getFax(),
            'latitude' => $sellerInfo->getLatitude(),
            'longitude' => $sellerInfo->getLongitude(),
        ];

        for ($i = 1; $i <= DataConfiguration::AGILE_MS_SELLER_NUMBER_QUANTITY; ++$i) {
            $methodName = 'getAmsCustomNumber' . $i;
            $data['ams_custom_number' . $i] = $sellerInfo->$methodName();
        }

        for ($i = 1; $i <= DataConfiguration::AGILE_MS_SELLER_DATE_QUANTITY; ++$i) {
            $methodName = 'getAmsCustomDate' . $i;
            $data['ams_custom_date' . $i] = $sellerInfo->$methodName();
        }

        for ($i = 1; $i <= DataConfiguration::AGILE_MS_SELLER_STRING_QUANTITY; ++$i) {
            $methodName = 'getAmsCustomString' . $i;
            $data['ams_custom_string' . $i] = $sellerInfo->$methodName();
        }

        // Multilanguage fields
        foreach ($sellerInfo->getLangs() as $sellerInfoLang) {
            $idLang = $sellerInfoLang->getLang()->getId();

            $data['company'][$idLang] = $sellerInfoLang->getCompany();
            $data['description'][$idLang] = $sellerInfoLang->getDescription();
            $data['address1'][$idLang] = $sellerInfoLang->getAddress1();
            $data['address2'][$idLang] = $sellerInfoLang->getAddress2();
            $data['city'][$idLang] = $sellerInfoLang->getCity();

            for ($i = 1; $i <= DataConfiguration::AGILE_MS_SELLER_TEXT_QUANTITY; ++$i) {
                $methodName = 'getAmsCustomText' . $i;
                $data['ams_custom_text' . $i][$idLang] = $sellerInfoLang->$methodName();
            }

            for ($i = 1; $i <= DataConfiguration::AGILE_MS_SELLER_HTML_QUANTITY; ++$i) {
                $methodName = 'getAmsCustomHtml' . $i;
                $data['ams_custom_html' . $i][$idLang] = $sellerInfoLang->$methodName();
            }
        }

        return $data;
    }

    /**
     * {@inheritdoc}
     */
    public function getDefaultData()
    {
    }
}
