<?php

namespace FiloBlu\ActiveCampaign\Block\Adminhtml\System\Config;

use FiloBlu\ActiveCampaign\Api\FieldsRepositoryInterface;
use FiloBlu\ActiveCampaign\Helper\ConsentsProfilingHelper;
use Magento\Backend\Block\Template\Context;
use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;
use Magento\Framework\View\Element\BlockInterface;
use Magento\Framework\View\Element\Html\Select;
use Magento\Framework\View\Helper\SecureHtmlRenderer;

class ConsentsCustomerMap extends AbstractFieldArray
{
    protected $fieldsRepository;
    protected $consentFieldsRenderer;
    /**
     * @var BlockInterface
     */
    protected $fieldRenderer;

    /**
     * @param FieldsRepositoryInterface $fieldsRepository
     * @param Context $context
     * @param array $data
     * @param SecureHtmlRenderer|null $secureRenderer
     */
    public function __construct(
        FieldsRepositoryInterface $fieldsRepository,
        Context                   $context,
        array                     $data = [],
        ?SecureHtmlRenderer       $secureRenderer = null
    ) {
        $this->fieldsRepository = $fieldsRepository;
        parent::__construct($context, $data, $secureRenderer);
    }

    protected function _prepareToRender()
    {
        $consentsField = $this->getLayout()->createBlock(Select::class)
            ->setId($this->_getCellInputElementId('<%- _id %>', 'consents_field'))
            ->setName($this->_getCellInputElementName('consents_field'))
            ->setClass('select')
            ->setOptions($this->getConsentsField());

        $activeCampaignFields = $this->getLayout()->createBlock(Select::class)
            ->setId($this->_getCellInputElementId('<%- _id %>', 'active_campaign_field'))
            ->setName($this->_getCellInputElementName('active_campaign_field'))
            ->setClass('select')
            ->setOptions($this->getActiveCampaignFields());

        $this->addColumn('consents_field', [
            'label' => __('Consents Field'),
            'renderer' => $consentsField
        ]);

        $this->addColumn('active_campaign_field', [
            'label' => __('Active Campaign Field'),
            'renderer' => $activeCampaignFields
        ]);

        $this->_addAfter = false;
        $this->_addButtonLabel = __('Add Consent Mapping');
    }

    public function getConsentsField()
    {
        return [
            ['value' => ConsentsProfilingHelper::MARKETING_PROFILE_SUBSCRIPTION_DATE, 'label' => __('Accepted At')],
            ['value' => ConsentsProfilingHelper::MARKETING_PROFILE_UNSUBSCRIPTION_DATE, 'label' => __('Revoked At')],
            ['value' => ConsentsProfilingHelper::MARKETING_PROFILE, 'label' => __('Is Revoked')],
            ['value' => ConsentsProfilingHelper::MARKETING_PROFILE_ONLY_PROFILE, 'label' => __('Is Profiled Only')],
        ];
    }

    public function getActiveCampaignFields()
    {
        $fields = $this->fieldsRepository->getAll();
        $options = [];
        foreach ($fields as $field) {
            $options[] = [
                'value' => $field['ac_id'],
                'label' => $field['name']
            ];
        }
        return $options;
    }
}
