<?php

namespace Itgalaxy\Elementor\Form\Infusionsoft\Integration\Admin\PageParts;

use Itgalaxy\Elementor\Form\Infusionsoft\Integration\Includes\Bootstrap;
use Itgalaxy\Elementor\Form\Infusionsoft\Integration\Includes\Helper;
use Itgalaxy\PluginCommon\ActionSchedulerHelper;
use Itgalaxy\PluginCommon\AdminGenerator\Themes\DefaultTheme;

class AuthorizePagePart
{
    /**
     * @return void
     */
    public static function render()
    {
        echo ' <form action="'
            . esc_url(admin_url() . 'admin.php?page=' . Bootstrap::OPTIONS_KEY)
            . '" method="post">';

        $trList = [];

        foreach (self::getFieldList() as $field) {
            $trList[] = [
                'type' => 'tr',
                'childes' => [
                    [
                        'type' => 'th',
                        'childes' => $field['th'],
                    ],
                    [
                        'type' => 'td',
                        'childes' => $field['td'],
                    ],
                ],
            ];
        }

        DefaultTheme::section(
            [
                'header' => [
                    'title' => esc_html__('Authorization', 'el-pro-form-infusionsoft-integration'),
                ],
                'childes' => [
                    [
                        'type' => 'table',
                        'childes' => $trList,
                    ],
                    [
                        'type' => 'button',
                        'classes' => ['btn-primary'],
                        'text' => Helper::isActive()
                            ? esc_attr__('Re-authorize', 'el-pro-form-infusionsoft-integration')
                            : esc_attr__('Authorize', 'el-pro-form-infusionsoft-integration'),
                        'attributes' => [
                            'type' => 'submit',
                            'name' => 'elementorInfusionsoftSubmit',
                        ],
                    ],
                ],
            ]
        );

        echo '</form>';
    }

    /**
     * @return array
     */
    private static function getFieldList()
    {
        $settings = get_option(Bootstrap::OPTIONS_KEY, []);

        return [
            [
                'th' => [
                    [
                        'type' => 'content',
                        'content' => esc_html__('API Key', 'el-pro-form-infusionsoft-integration'),
                    ],
                ],
                'td' => [
                    [
                        'type' => 'input',
                        'input_attributes' => [
                            'value' => isset($settings['api-key']) ? esc_attr($settings['api-key']) : '',
                            'name' => 'api-key',
                            'id' => 'api-key',
                            'aria-required' => 'true',
                        ],
                    ],
                ],
            ],
            [
                'th' => [
                    [
                        'type' => 'content',
                        'content' => esc_html__('API Secret', 'el-pro-form-infusionsoft-integration'),
                    ],
                ],
                'td' => [
                    [
                        'type' => 'input',
                        'input_attributes' => [
                            'value' => isset($settings['api-secret']) ? esc_attr($settings['api-secret']) : '',
                            'name' => 'api-secret',
                            'id' => 'api-secret',
                            'aria-required' => 'true',
                        ],
                    ],
                ],
            ],
            [
                'th' => [
                    [
                        'type' => 'content',
                        'content' => esc_html__('Send type', 'el-pro-form-infusionsoft-integration'),
                    ],
                ],
                'td' => [
                    [
                        'type' => 'select',
                        'select_attributes' => [
                            'name' => 'send_type',
                            'id' => 'send_type',
                            'aria-required' => 'true',
                        ],
                        'options' => [
                            [
                                'attributes' => [
                                    'value' => 'immediately',
                                    'selected' => !isset($settings['send_type']) || $settings['send_type'] === 'immediately',
                                ],
                                'label' => esc_html__('Immediately upon submitting the form', 'el-pro-form-infusionsoft-integration'),
                            ],
                            [
                                'attributes' => [
                                    'value' => 'wp_cron',
                                    'selected' => isset($settings['send_type']) && $settings['send_type'] === 'wp_cron',
                                ],
                                'label' => esc_html__('Action Scheduler', 'el-pro-form-infusionsoft-integration'),
                            ],
                        ],
                        'description' => [
                            'text' => Helper::isActive() && !empty($settings['send_type']) && $settings['send_type'] == 'wp_cron'
                                ? esc_html__('The number of registered form submit events pending', 'el-pro-form-infusionsoft-integration')
                                . ': <strong>' . ActionSchedulerHelper::getCountPendingActions(Bootstrap::SEND_CRON_TASK) . '</strong>'
                                : '',
                        ],
                    ],
                ],
            ],
        ];
    }
}
