<?php

namespace Itgalaxy\Elementor\Form\Infusionsoft\Integration\Includes;

use Elementor\Controls_Manager;

class RenderFields
{
    /**
     * @var string
     */
    public $namePrefix;

    /**
     * @param string $prefix
     */
    public function __construct($prefix = '')
    {
        $this->namePrefix = $prefix;
    }

    /**
     * @psalm-suppress UndefinedDocblockClass
     *
     * @param \ElementorPro\Modules\Forms\Widgets\Form $widget
     * @param array                                    $list
     * @param string                                   $key
     * @param string                                   $title
     *
     * @return void
     */
    public function selectField($widget, $list, $key, $title)
    {
        if (in_array($key, ['send_type'], true)) {
            $widget->add_control(
                'el_pro_infusionsoft_' . $this->namePrefix . $key,
                [
                    'label' => $title,
                    'type' => Controls_Manager::SELECT,
                    'options' => $list,
                    'default' => 'create_update',
                    'label_block' => true,
                ]
            );
        } else {
            $widget->add_control(
                'el_pro_infusionsoft_' . $this->namePrefix . $key,
                [
                    'label' => $title,
                    'type' => Controls_Manager::TEXT,
                    'description' => esc_html__('Possible values: ', 'el-pro-form-infusionsoft-integration')
                        . implode(', ', $list),
                    'label_block' => true,
                ]
            );
        }
    }

    /**
     * @psalm-suppress UndefinedDocblockClass
     *
     * @param \ElementorPro\Modules\Forms\Widgets\Form $widget
     * @param string                                   $key
     * @param string                                   $title
     * @param array                                    $field
     *
     * @return void
     */
    public function inputTextField($widget, $key, $title, $field)
    {
        $widget->add_control(
            'el_pro_infusionsoft_' . $this->namePrefix . $key,
            [
                'label' => $title,
                'type' => Controls_Manager::TEXT,
                'label_block' => mb_strlen($title) >= 15,
                'separator' => isset($field['separator']) ? $field['separator'] : '',
                'description' => isset($field['description'])
                    ? $field['description']
                    : '',
            ]
        );
    }

    /**
     * @psalm-suppress UndefinedDocblockClass
     *
     * @param \ElementorPro\Modules\Forms\Widgets\Form $widget
     * @param string                                   $key
     * @param string                                   $title
     * @param array                                    $field
     *
     * @return void
     */
    public function inputCheckboxField($widget, $key, $title, $field)
    {
        $required = !empty($field['isRequired']);

        $widget->add_control(
            'el_pro_infusionsoft_' . $this->namePrefix . $key,
            [
                'label' => $title,
                'title' => 'key - ' . $key,
                'required' => $required,
                'type' => Controls_Manager::SWITCHER,
                'default' => '',
            ]
        );
    }

    /**
     * @psalm-suppress UndefinedDocblockClass
     *
     * @param \ElementorPro\Modules\Forms\Widgets\Form $widget
     * @param string                                   $key
     * @param string                                   $title
     * @param array                                    $field
     *
     * @return void
     */
    public function textareaField($widget, $key, $title, $field)
    {
        $required = !empty($field['isRequired']);

        $widget->add_control(
            'el_pro_infusionsoft_' . $this->namePrefix . $key,
            [
                'label' => $title . ($required ? '<span> * </span>' : ''),
                'title' => 'key - ' . $key,
                'required' => $required,
                'type' => Controls_Manager::TEXTAREA,
                'separator' => isset($field['separator']) ? $field['separator'] : '',
                'label_block' => isset($field['label_block']) ? $field['label_block'] : mb_strlen($title) > 15,
            ]
        );
    }

    /**
     * @psalm-suppress UndefinedDocblockClass
     *
     * @param \ElementorPro\Modules\Forms\Widgets\Form $widget
     * @param RenderFields                             $renderFields
     *
     * @return void
     */
    public function showCustomFields($widget, $renderFields)
    {
        $fields = \get_option(Bootstrap::CONTACT_CUSTOM_FIELDS_KEY, []);

        foreach ($fields as $field) {
            if (!is_array($field)) {
                continue;
            }

            $fieldData = [
                'isRequired' => false,
                'description' => esc_html__('field type', 'el-pro-form-infusionsoft-integration')
                    . ' - '
                    . $field['field_type'],
            ];

            if (in_array($field['field_type'], ['Radio', 'Dropdown'])) {
                $selectItems = [];

                foreach ($field['options'] as $item) {
                    $selectItems[$item['id']] = $item['label'];
                }

                $renderFields->selectField(
                    $widget,
                    $selectItems,
                    'custom_fields_' . $field['id'],
                    $field['label']
                );
            } elseif ($field['field_type'] == 'TextArea') {
                $fieldData['label_block'] = true;

                $renderFields->textareaField(
                    $widget,
                    'custom_fields_' . $field['id'],
                    $field['label'],
                    $fieldData
                );
            } elseif ($field['field_type'] == 'Checkbox') {
                $renderFields->inputCheckboxField(
                    $widget,
                    'custom_fields_' . $field['id'],
                    $field['label'],
                    $fieldData
                );
            } else {
                $renderFields->inputTextField(
                    $widget,
                    'custom_fields_' . $field['id'],
                    $field['label'],
                    $fieldData
                );
            }
        }
    }
}
