<?php

use EventEspresso\core\exceptions\InvalidDataTypeException;
use EventEspresso\core\exceptions\InvalidInterfaceException;
use EventEspresso\core\services\database\TableAnalysis;
use EventEspresso\core\services\database\TableManager;
use EventEspresso\core\services\loaders\LoaderFactory;
use EventEspresso\core\services\payments\PaymentProcessorFees;
use EventEspresso\core\services\request\sanitizers\AllowedTags;

/**
 * Payments_Admin_Page
 * This contains the logic for setting up the Event Payments related admin pages.  Any methods without
 * phpdoc comments have inline docs with parent class.
 *
 * @package         Payments_Admin_Page
 * @subpackage      includes/core/admin/Payments_Admin_Page.core.php
 * @author          Darren Ethier
 */
class Payments_Admin_Page extends EE_Admin_Page
{
    /**
     * Variables used for when we're re-sorting the logs results,
     * in case we needed to do two queries, and we need to resort
     *
     * @var string
     */
    private $_sort_logs_again_direction;


    /**
     * @Constructor
     * @access public
     * @param bool $routing indicate whether we want to just load the object and handle routing or just load the object.
     * @throws EE_Error
     * @throws InvalidArgumentException
     * @throws InvalidDataTypeException
     * @throws InvalidInterfaceException
     * @throws ReflectionException
     */
    public function __construct($routing = true)
    {
        parent::__construct($routing);
    }


    protected function _init_page_props()
    {
        $this->page_slug        = EE_PAYMENTS_PG_SLUG;
        $this->page_label       = esc_html__('Payment Methods', 'event_espresso');
        $this->_admin_base_url  = EE_PAYMENTS_ADMIN_URL;
        $this->_admin_base_path = EE_PAYMENTS_ADMIN;
    }


    protected function _ajax_hooks()
    {
        // todo: all hooks for ajax goes here.
    }


    protected function _define_page_props()
    {
        $this->_admin_page_title = $this->page_label;
        $this->_labels           = [
            'publishbox' => esc_html__('Update Settings', 'event_espresso'),
        ];
    }


    /**
     * @param string $sort_logs_again_direction
     */
    public function setSortLogsAgainDirection(string $sort_logs_again_direction): void
    {
        $this->_sort_logs_again_direction = $sort_logs_again_direction;
    }


    protected function _set_page_routes()
    {
        /**
         * note that with payment method capabilities, although we've implemented
         * capability mapping which will be used for accessing payment methods owned by
         * other users.  This is not fully implemented yet in the payment method ui.
         * Currently, only the "plural" caps are in active use.
         * When cap mapping is implemented, some routes will need to use the singular form of
         * capability method and also include the $id of the payment method for the route.
         **/
        $this->_page_routes = [
            'default'                   => [
                'func'       => [$this, '_payment_methods_list'],
                'capability' => 'ee_edit_payment_methods',
            ],
            'payment_settings'          => [
                'func'       => [$this, '_payment_settings'],
                'capability' => 'ee_manage_gateways',
            ],
            'activate_payment_method'   => [
                'func'       => [$this, '_activate_payment_method'],
                'noheader'   => true,
                'capability' => 'ee_edit_payment_methods',
            ],
            'deactivate_payment_method' => [
                'func'       => [$this, '_deactivate_payment_method'],
                'noheader'   => true,
                'capability' => 'ee_delete_payment_methods',
            ],
            'update_payment_method'     => [
                'func'       => [$this, '_update_payment_method'],
                'noheader'           => true,
                'headers_sent_route' => 'default',
                'capability'         => 'ee_edit_payment_methods',
            ],
            'update_payment_settings'   => [
                'func'       => [$this, '_update_payment_settings'],
                'noheader'   => true,
                'capability' => 'ee_manage_gateways',
            ],
            'payment_log'               => [
                'func'       => [$this, '_payment_log_overview_list_table'],
                'capability' => 'ee_read_payment_methods',
            ],
            'payment_log_details'       => [
                'func'       => [$this, '_payment_log_details'],
                'capability' => 'ee_read_payment_methods',
            ],
        ];
    }


    /**
     * @throws EE_Error
     * @throws ReflectionException
     */
    protected function _set_page_config()
    {
        $payment_method_list_config = [
            'nav'           => [
                'label' => esc_html__('Payment Methods', 'event_espresso'),
                'icon'  => 'dashicons-bank',
                'order' => 10,
            ],
            'metaboxes'     => $this->_default_espresso_metaboxes,
            'help_tabs'     => array_merge(
                [
                    'payment_methods_overview_help_tab' => [
                        'title'    => esc_html__('Payment Methods Overview', 'event_espresso'),
                        'filename' => 'payment_methods_overview',
                    ],
                ],
                $this->_add_payment_method_help_tabs()
            ),
            'require_nonce' => false,
        ];
        $this->_page_config         = [
            'default'          => $payment_method_list_config,
            'payment_settings' => [
                'nav'           => [
                    'label' => esc_html__('Settings', 'event_espresso'),
                    'icon'  => 'dashicons-admin-generic',
                    'order' => 20,
                ],
                'help_tabs'     => [
                    'payment_methods_settings_help_tab' => [
                        'title'    => esc_html__('Payment Method Settings', 'event_espresso'),
                        'filename' => 'payment_methods_settings',
                    ],
                ],
                'metaboxes'     => array_merge($this->_default_espresso_metaboxes, ['_publish_post_box']),
                'require_nonce' => false,
            ],
            'payment_log'      => [
                'nav'           => [
                    'label' => esc_html__("Logs", 'event_espresso'),
                    'icon'  => 'dashicons-text-page',
                    'order' => 30,
                ],
                'list_table'    => 'Payment_Log_Admin_List_Table',
                'metaboxes'     => $this->_default_espresso_metaboxes,
                'require_nonce' => false,
            ],
        ];
    }


    /**
     * @return array
     * @throws DomainException
     * @throws EE_Error
     * @throws InvalidArgumentException
     * @throws InvalidDataTypeException
     * @throws InvalidInterfaceException
     * @throws ReflectionException
     */
    protected function _add_payment_method_help_tabs(): array
    {
        EE_Registry::instance()->load_lib('Payment_Method_Manager');
        $payment_method_types     = EE_Payment_Method_Manager::instance()->payment_method_types();
        $all_pmt_help_tabs_config = [];
        foreach ($payment_method_types as $payment_method_type) {
            if (
                ! $this->capabilities->current_user_can(
                    $payment_method_type->cap_name(),
                    'specific_payment_method_type_access'
                )
            ) {
                continue;
            }
            foreach ($payment_method_type->help_tabs_config() as $help_tab_name => $config) {
                $template_args                              = $config['template_args'] ?? [];
                $template_args['admin_page_obj']            = $this;
                $all_pmt_help_tabs_config[ $help_tab_name ] = [
                    'title'   => $config['title'],
                    'content' => EEH_Template::display_template(
                        $payment_method_type->file_folder() . 'help_tabs/' . $config['filename'] . '.help_tab.php',
                        $template_args,
                        true
                    ),
                ];
            }
        }
        return $all_pmt_help_tabs_config;
    }


    // none of the below group are currently used for Gateway Settings
    protected function _add_screen_options()
    {
    }


    protected function _add_feature_pointers()
    {
    }


    public function admin_init()
    {
    }


    public function admin_notices()
    {
    }


    public function admin_footer_scripts()
    {
    }


    public function load_scripts_styles()
    {
        // styles
        wp_enqueue_style('espresso-ui-theme');
        wp_register_style(
            'espresso_payments',
            EE_PAYMENTS_ASSETS_URL . 'ee-payments.css',
            [],
            EVENT_ESPRESSO_VERSION
        );
        // scripts
        wp_enqueue_script('ee_admin_js');
        wp_enqueue_script('ee-text-links');
        wp_enqueue_script(
            'espresso_payments',
            EE_PAYMENTS_ASSETS_URL . 'espresso_payments_admin.js',
            ['ee-datepicker'],
            EVENT_ESPRESSO_VERSION,
            true
        );
    }


    public function load_scripts_styles_default()
    {
        wp_enqueue_style('espresso_payments');
        wp_enqueue_style('ee-text-links');
    }


    public function load_scripts_styles_payment_log_details()
    {
        wp_enqueue_style('espresso_payments');
    }


    /**
     * @throws EE_Error
     */
    private function veryifyTablesExist()
    {
        /** @var TableAnalysis $table_analysis */
        $table_analysis = LoaderFactory::getShared(TableAnalysis::class);
        /** @var TableManager $table_manager */
        $table_manager = LoaderFactory::getShared(TableManager::class);
        if (! $table_analysis->tableExists('esp_payment_method')) {
            $table_manager->createTable(
                'esp_payment_method',
                "PMD_ID int(11) NOT NULL AUTO_INCREMENT,
				PMD_type varchar(124) DEFAULT NULL,
				PMD_name varchar(255) DEFAULT NULL,
				PMD_desc text,
				PMD_admin_name varchar(255) DEFAULT NULL,
				PMD_admin_desc text,
				PMD_slug varchar(124) DEFAULT NULL,
				PMD_order int(11) DEFAULT NULL,
				PMD_debug_mode tinyint(1) NOT NULL DEFAULT '0',
				PMD_wp_user int(11) NOT NULL DEFAULT '0',
				PMD_open_by_default tinyint(1) NOT NULL DEFAULT '0',
				PMD_button_url varchar(1012) DEFAULT NULL,
				PMD_scope varchar(255) NULL DEFAULT 'frontend',
				PRIMARY KEY  (PMD_ID),
				UNIQUE KEY PMD_slug_UNIQUE (PMD_slug),
				KEY PMD_type (PMD_type)",
                'InnoDB'
            );
        }
        if (! $table_analysis->tableExists('esp_currency_payment_method')) {
            $table_manager->createTable(
                'esp_currency_payment_method',
                "CPM_ID int(11) NOT NULL AUTO_INCREMENT,
				CUR_code varchar(6) NOT NULL,
				PMD_ID int(11) NOT NULL,
				PRIMARY KEY  (CPM_ID),
				KEY PMD_ID (PMD_ID)",
                'InnoDB'
            );
        }
    }


    /**
     * @throws EE_Error
     * @throws ReflectionException
     */
    protected function _payment_methods_list()
    {
        $this->veryifyTablesExist();
        /**
         * first let's ensure payment methods have been set up.
         * We do this here because when people activate a payment method for the first time (as an addon),
         * it may not set up its capabilities or get registered correctly due to the loading process.
         * However, people MUST set up the details for the payment method,
         * so it's safe to do a recheck here.
         */
        EE_Registry::instance()->load_lib('Payment_Method_Manager');
        EEM_Payment_Method::instance()->verify_button_urls();
        // set up tabs, one for each payment method type
        $tabs            = [];
        $payment_methods = [];
        foreach (EE_Payment_Method_Manager::instance()->payment_method_types() as $pmt_obj) {
            // we don't want to show admin-only PMTs for now
            if ($pmt_obj instanceof EE_PMT_Admin_Only) {
                continue;
            }
            // check access
            if (
                ! $this->capabilities->current_user_can(
                    $pmt_obj->cap_name(),
                    'specific_payment_method_type_access'
                )
            ) {
                continue;
            }
            // check for any active pms of that type
            $payment_method = EEM_Payment_Method::instance()->get_one_of_type($pmt_obj->system_name());
            if (! $payment_method instanceof EE_Payment_Method) {
                $payment_method = EE_Payment_Method::new_instance(
                    [
                        'PMD_slug'       => sanitize_key($pmt_obj->system_name()),
                        'PMD_type'       => $pmt_obj->system_name(),
                        'PMD_name'       => $pmt_obj->pretty_name(),
                        'PMD_admin_name' => $pmt_obj->pretty_name(),
                    ]
                );
            }
            $payment_methods[ $payment_method->slug() ] = $payment_method;
        }
        $payment_methods = apply_filters(
            'FHEE__Payments_Admin_Page___payment_methods_list__payment_methods',
            $payment_methods
        );
        foreach ($payment_methods as $payment_method) {
            if ($payment_method instanceof EE_Payment_Method) {
                $this->addMetaBox(
                // html id
                    'espresso_' . $payment_method->slug() . '_payment_settings',
                    // title
                    sprintf(esc_html__('%s Settings', 'event_espresso'), $payment_method->admin_name()),
                    // callback
                    [$this, 'payment_method_settings_meta_box'],
                    // post type
                    null,
                    // context
                    'normal',
                    // priority
                    'default',
                    // callback args
                    ['payment_method' => $payment_method]
                );
                // setup for tabbed content
                $tabs[ $payment_method->slug() ] = [
                    'label' => $payment_method->admin_name(),
                    'class' => $payment_method->active()
                        ? 'gateway-active'
                        : '',
                    'href'  => 'espresso_' . $payment_method->slug() . '_payment_settings',
                    'title' => esc_html__('Modify this Payment Method', 'event_espresso'),
                    'slug'  => $payment_method->slug(),
                    'icon'  => $payment_method->active()
                        ? '<span class="dashicons dashicons-yes-alt"></span>'
                        : '<span class="dashicons dashicons-remove"></span>',
                ];
            }
        }
        $this->_template_args['admin_page_header'] = EEH_Tabbed_Content::tab_text_links(
            $tabs,
            'payment_method_links',
            '',
            $this->_get_active_payment_method_slug()
        );
        $this->display_admin_page_with_sidebar();
    }


    /**
     *   _get_active_payment_method_slug
     *
     * @return string
     * @throws EE_Error
     * @throws ReflectionException
     */
    protected function _get_active_payment_method_slug()
    {
        $payment_method_slug = false;
        // decide which payment method tab to open first, as dictated by the request's 'payment_method'
        if (isset($this->_req_data['payment_method'])) {
            // if they provided the current payment method, use it
            $payment_method_slug = sanitize_key($this->_req_data['payment_method']);
        }

        $payment_method = EEM_Payment_Method::instance()->get_one([['PMD_slug' => $payment_method_slug]]);
        // if that didn't work or wasn't provided, find another way to select the current pm
        if (! $this->_verify_payment_method($payment_method)) {
            // like, looking for an active one
            $payment_method = EEM_Payment_Method::instance()->get_one_active(EEM_Payment_Method::scope_cart);
            // test that one as well
            if ($this->_verify_payment_method($payment_method)) {
                $payment_method_slug = $payment_method->slug();
            } else {
                $payment_method_slug = 'paypal_standard';
            }
        }
        return $payment_method_slug;
    }


    /**
     *    payment_method_settings_meta_box
     *    returns TRUE if the passed payment method is properly constructed and the logged-in user has the correct
     *    capabilities to access it
     *
     * @param EE_Payment_Method|null $payment_method
     * @return boolean
     * @throws EE_Error
     */
    protected function _verify_payment_method(?EE_Payment_Method $payment_method): bool
    {
        return $payment_method instanceof EE_Payment_Method
               && $payment_method->type_obj() instanceof EE_PMT_Base
               && $this->capabilities->current_user_can(
                   $payment_method->type_obj()->cap_name(),
                   'specific_payment_method_type_access'
               );
    }


    /**
     *    payment_method_settings_meta_box
     *
     * @param NULL  $post_obj_which_is_null is an object containing the current post (as a $post object)
     * @param array $metabox                is an array with metabox id, title, callback, and args elements. the value
     *                                      at 'args' has key 'payment_method', as set within _payment_methods_list
     * @return void
     * @throws EE_Error
     * @throws ReflectionException
     */
    public function payment_method_settings_meta_box($post_obj_which_is_null, array $metabox)
    {
        $payment_method = isset($metabox['args'], $metabox['args']['payment_method'])
            ? $metabox['args']['payment_method']
            : null;
        if (! $payment_method instanceof EE_Payment_Method) {
            throw new EE_Error(
                esc_html__(
                    'Payment method metabox setup incorrectly. No Payment method object was supplied',
                    'event_espresso'
                )
            );
        }
        $payment_method_scopes = $payment_method->active();
        // if the payment method really exists show its form, otherwise the activation template
        if ($payment_method->ID() && ! empty($payment_method_scopes)) {
            $form = $this->_generate_payment_method_settings_form($payment_method);
            if ($form->form_data_present_in($this->_req_data)) {
                $form->receive_form_submission($this->_req_data);
            }
            echo wp_kses(
                $form->form_open() . $form->get_html_and_js() . $form->form_close(),
                AllowedTags::getWithFormTags()
            );
        } else {
            echo wp_kses(
                $this->_activate_payment_method_button($payment_method)->get_html_and_js(),
                AllowedTags::getWithFormTags()
            );
        }
    }


    /**
     * Gets the form for all the settings related to this payment method type
     *
     * @access protected
     * @param EE_Payment_Method|null $payment_method
     * @return EE_Form_Section_Proper
     * @throws EE_Error
     * @throws ReflectionException
     */
    protected function _generate_payment_method_settings_form(
        ?EE_Payment_Method $payment_method
    ): EE_Form_Section_Proper {
        if (! $payment_method instanceof EE_Payment_Method) {
            return new EE_Form_Section_Proper();
        }
        $subsections = apply_filters(
            'FHEE__Payments_Admin_Page___generate_payment_method_settings_form__form_subsections',
            [
                'pci_dss_compliance'      => $this->_pci_dss_compliance($payment_method),
                'currency_support'        => $this->_currency_support($payment_method),
                'payment_method_settings' => $this->_payment_method_settings($payment_method),
                'update'                  => $this->_update_payment_method_button($payment_method),
                'deactivate'              => $this->_deactivate_payment_method_button($payment_method),
                'fine_print'              => $this->_fine_print(),
                'partner_fees_notice'     => $this->partnerFeesNotice($payment_method),
            ],
            $payment_method
        );
        return new EE_Form_Section_Proper(
            [
                'name'            => $payment_method->slug() . '_settings_form',
                'html_id'         => $payment_method->slug() . '_settings_form',
                'action'          => EE_Admin_Page::add_query_args_and_nonce(
                    [
                        'action'         => 'update_payment_method',
                        'payment_method' => $payment_method->slug(),
                    ],
                    EE_PAYMENTS_ADMIN_URL
                ),
                'layout_strategy' => new EE_Admin_Two_Column_Layout(),
                'subsections'     => array_filter($subsections),
            ]
        );
    }


    /**
     * _pci_dss_compliance
     *
     * @access protected
     * @param EE_Payment_Method $payment_method
     * @return EE_Form_Section_HTML|null
     * @throws EE_Error
     */
    protected function _pci_dss_compliance(EE_Payment_Method $payment_method): ?EE_Form_Section_HTML
    {
        if (! $payment_method->type_obj()->requires_https()) {
            return null;
        }
        return new EE_Form_Section_HTML(
            EEH_HTML::tr(
                EEH_HTML::th(
                    EEH_HTML::label(
                        EEH_HTML::strong(
                            esc_html__('IMPORTANT', 'event_espresso'),
                            '',
                            'important-notice'
                        )
                    )
                ) .
                EEH_HTML::td(
                    EEH_HTML::div(
                        EEH_HTML::strong(
                            esc_html__(
                                'You are responsible for your own website security and Payment Card Industry Data Security Standards (PCI DSS) compliance.',
                                'event_espresso'
                            )
                        ),
                        '',
                        'ee-status-outline ee-status-bg--warning'
                    )
                    . EEH_HTML::br()

                    . EEH_HTML::div(
                        esc_html__('Learn more about ', 'event_espresso')
                        . EEH_HTML::link(
                            'https://www.pcisecuritystandards.org/merchants/index.php',
                            esc_html__('PCI DSS compliance', 'event_espresso')
                        ),
                        '',
                        'ee-status-outline ee-status-bg--info'
                    )
                )
            )
        );
    }


    /**
     * _currency_support
     *
     * @access protected
     * @param EE_Payment_Method $payment_method
     * @return EE_Form_Section_HTML|null
     * @throws EE_Error
     */
    protected function _currency_support(EE_Payment_Method $payment_method): ?EE_Form_Section_HTML
    {
        if ($payment_method->usable_for_currency(EE_Config::instance()->currency->code)) {
            return null;
        }
        return new EE_Form_Section_HTML(
            EEH_HTML::tr(
                EEH_HTML::th(
                    EEH_HTML::label(
                        EEH_HTML::strong(
                            esc_html__('IMPORTANT', 'event_espresso'),
                            '',
                            'important-notice'
                        )
                    )
                ) .
                EEH_HTML::td(
                    EEH_HTML::div(
                        EEH_HTML::strong(
                            sprintf(
                                esc_html__(
                                    'This payment method does not support the currency set on your site (%1$s). Please activate a different payment method or change your site\'s country and associated currency.',
                                    'event_espresso'
                                ),
                                EE_Config::instance()->currency->code
                            )
                        ),
                        '',
                        'ee-status-outline ee-status-bg--warning'
                    )
                )
            )
        );
    }


    /**
     * _update_payment_method_button
     *
     * @access protected
     * @param EE_Payment_Method $payment_method
     * @return EE_Payment_Method_Form
     * @throws EE_Error
     * @throws ReflectionException
     */
    protected function _payment_method_settings(EE_Payment_Method $payment_method): EE_Payment_Method_Form
    {
        // modify the form, so we only have/show fields that will be implemented for this version
        return $this->_simplify_form($payment_method->type_obj()->settings_form(), $payment_method->name());
    }


    /**
     * Simplifies the form to merely reproduce 4.1's gateway settings functionality
     *
     * @param EE_Form_Section_Proper $form_section
     * @param string                 $payment_method_name
     * @return EE_Payment_Method_Form
     * @throws EE_Error
     */
    protected function _simplify_form(
        EE_Form_Section_Proper $form_section,
        string $payment_method_name = ''
    ): EE_Payment_Method_Form {
        if ($form_section instanceof EE_Payment_Method_Form) {
            $form_section->exclude(
                [
                    'PMD_type', // don't want them changing the type
                    'PMD_slug', // or the slug (probably never)
                    'PMD_wp_user', // or the user's ID
                    'Currency', // or the currency, until the rest of EE supports simultaneous currencies
                ]
            );
            return $form_section;
        }
        throw new EE_Error(
            sprintf(
                esc_html__(
                    'The EE_Payment_Method_Form for the "%1$s" payment method is missing or invalid.',
                    'event_espresso'
                ),
                $payment_method_name
            )
        );
    }


    /**
     * _update_payment_method_button
     *
     * @access protected
     * @param EE_Payment_Method $payment_method
     * @return EE_Form_Section_HTML
     * @throws EE_Error
     */
    protected function _update_payment_method_button(EE_Payment_Method $payment_method): EE_Form_Section_HTML
    {
        $update_button = new EE_Submit_Input(
            [
                'name'       => 'submit',
                'html_id'    => 'save_' . $payment_method->slug() . '_settings',
                'default'    => sprintf(
                    esc_html__('Update %s Payment Settings', 'event_espresso'),
                    $payment_method->admin_name()
                ),
                'html_label' => EEH_HTML::nbsp(),
            ]
        );
        return new EE_Form_Section_HTML(
            EEH_HTML::tr(
                EEH_HTML::th(
                // esc_html__('Update Settings', 'event_espresso'),
                    '&nbsp;',
                    '',
                    'ee-update-' . $payment_method->slug() . '-settings__label'
                ) .
                EEH_HTML::td(
                    $update_button->get_html_for_input(),
                    '',
                    'ee-update-' . $payment_method->slug() . '-settings__input'
                ),
                '',
                'ee-update-' . $payment_method->slug() . '-settings'
            )
        );
    }


    /**
     * _deactivate_payment_method_button
     *
     * @access protected
     * @param EE_Payment_Method $payment_method
     * @return EE_Form_Section_HTML
     */
    protected function _deactivate_payment_method_button(EE_Payment_Method $payment_method): EE_Form_Section_HTML
    {
        $link_text_and_title = sprintf(
            esc_html__('Deactivate %1$s Payments?', 'event_espresso'),
            $payment_method->admin_name()
        );
        return new EE_Form_Section_HTML(
            EEH_HTML::tr(
                EEH_HTML::th(
                // esc_html__('Deactivate Payment Method', 'event_espresso'),
                    '&nbsp;',
                    '',
                    'ee-deactivate-' . $payment_method->slug() . '-settings__label'
                ) .
                EEH_HTML::td(
                    EEH_HTML::link(
                        EE_Admin_Page::add_query_args_and_nonce(
                            [
                                'action'         => 'deactivate_payment_method',
                                'payment_method' => $payment_method->slug(),
                            ],
                            EE_PAYMENTS_ADMIN_URL
                        ),
                        $link_text_and_title,
                        $link_text_and_title,
                        'deactivate_' . $payment_method->slug(),
                        'button button--secondary'
                    ),
                    '',
                    'ee-deactivate-' . $payment_method->slug() . '-settings__input'
                ),
                '',
                'ee-deactivate-' . $payment_method->slug() . '-settings'
            )
        );
    }


    /**
     * _activate_payment_method_button
     *
     * @access protected
     * @param EE_Payment_Method $payment_method
     * @return EE_Form_Section_Proper
     * @throws EE_Error
     */
    protected function _activate_payment_method_button(EE_Payment_Method $payment_method): EE_Form_Section_Proper
    {
        $link_text_and_title = sprintf(
            esc_html__('Activate %1$s Payment Method?', 'event_espresso'),
            $payment_method->admin_name()
        );
        return new EE_Form_Section_Proper(
            [
                'name'            => 'activate_' . $payment_method->slug() . '_settings_form',
                'html_id'         => 'activate_' . $payment_method->slug() . '_settings_form',
                'action'          => '#',
                'layout_strategy' => new EE_Admin_Two_Column_Layout(),
                'subsections'     => apply_filters(
                    'FHEE__Payments_Admin_Page___activate_payment_method_button__form_subsections',
                    [
                        new EE_Form_Section_HTML(
                            EEH_HTML::table(
                                EEH_HTML::tr(
                                    EEH_HTML::td(
                                        $payment_method->type_obj()->introductory_html(),
                                        '',
                                        '',
                                        '',
                                        'colspan="2"'
                                    )
                                ) .
                                EEH_HTML::tr(
                                    EEH_HTML::th(
                                        EEH_HTML::label(esc_html__('Click to Activate ', 'event_espresso'))
                                    ) .
                                    EEH_HTML::td(
                                        EEH_HTML::link(
                                            EE_Admin_Page::add_query_args_and_nonce(
                                                [
                                                    'action'              => 'activate_payment_method',
                                                    'payment_method_type' => $payment_method->type(),
                                                ],
                                                EE_PAYMENTS_ADMIN_URL
                                            ),
                                            $link_text_and_title,
                                            $link_text_and_title,
                                            'activate_' . $payment_method->slug(),
                                            'button button--primary-alt'
                                        )
                                    )
                                )
                            )
                        ),
                    ],
                    $payment_method
                ),
            ]
        );
    }


    /**
     * _fine_print
     *
     * @access protected
     * @return EE_Form_Section_HTML
     */
    protected function _fine_print(): EE_Form_Section_HTML
    {
        return new EE_Form_Section_HTML(
            EEH_HTML::tr(
                EEH_HTML::th()
                . EEH_HTML::thx()
                . EEH_HTML::td(
                    EEH_HTML::p(
                        esc_html__('All fields marked with a * are required fields', 'event_espresso'),
                        '',
                        'grey-text'
                    )
                )
            )
        );
    }


    /**
     * partnerFeesNotice
     *
     * @access protected
     * @param  EE_Payment_Method $payment_method
     * @return ?EE_Form_Section_HTML
     */
    protected function partnerFeesNotice(EE_Payment_Method $payment_method): ?EE_Form_Section_HTML
    {
        /** @var PaymentProcessorFees $payment_proc_fees */
        $payment_proc_fees = LoaderFactory::getLoader()->getShared(
            'EventEspresso\core\services\payments\PaymentProcessorFees'
        );
        try {
            $pm_name = $payment_method->name();
        } catch (EE_Error|ReflectionException $e) {
            return null;
        }
        if (! $payment_proc_fees->isPartnerGateway($pm_name)) {
            return null;
        }
        return $payment_proc_fees->getFeesNotice($pm_name);
    }


    /**
     * Activates a payment method of that type. Mostly assuming there is only 1 of that type (or none so far)
     *
     * @throws EE_Error
     * @throws ReflectionException
     * @global WP_User $current_user
     */
    protected function _activate_payment_method()
    {
        if (isset($this->_req_data['payment_method_type'])) {
            $payment_method_type = sanitize_text_field($this->_req_data['payment_method_type']);
            // see if one exists
            EE_Registry::instance()->load_lib('Payment_Method_Manager');
            $payment_method = EE_Payment_Method_Manager::instance()
                                                       ->activate_a_payment_method_of_type($payment_method_type);
            $this->_redirect_after_action(
                1,
                'Payment Method',
                'activated',
                ['action' => 'default', 'payment_method' => $payment_method->slug()]
            );
        } else {
            $this->_redirect_after_action(false, 'Payment Method', 'activated', ['action' => 'default']);
        }
    }


    /**
     * @throws EE_Error
     * @throws ReflectionException
     */
    protected function _deactivate_payment_method()
    {
        if (isset($this->_req_data['payment_method'])) {
            $payment_method_slug = sanitize_key($this->_req_data['payment_method']);
            // deactivate it
            EE_Registry::instance()->load_lib('Payment_Method_Manager');
            $count_updated = EE_Payment_Method_Manager::instance()->deactivate_payment_method($payment_method_slug);
            $this->_redirect_after_action(
                $count_updated,
                'Payment Method',
                'deactivated',
                ['action' => 'default', 'payment_method' => $payment_method_slug]
            );
        } else {
            $this->_redirect_after_action(false, 'Payment Method', 'deactivated', ['action' => 'default']);
        }
    }


    /**
     * Processes the payment method form that was submitted. This is slightly trickier than usual form
     * processing because we first need to identify WHICH form was processed and which payment method
     * it corresponds to. Once we have done that, we see if the form is valid. If it is, the
     * form's data is saved, and we redirect to the default payment methods page, setting the updated payment method
     * as the currently-selected one. If it DOESN'T validate, we render the page with the form's errors (in the
     * subsequently called 'headers_sent_func' which is _payment_methods_list)
     *
     * @return void
     * @throws EE_Error
     * @throws ReflectionException
     */
    protected function _update_payment_method()
    {
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            // ok let's find which gateway form to use based on the form input
            EE_Registry::instance()->load_lib('Payment_Method_Manager');
            /** @var $correct_pmt_form_to_use EE_Payment_Method_Form */
            $correct_pmt_form_to_use = null;
            $payment_method          = null;
            foreach (EEM_Payment_Method::instance()->get_all() as $payment_method) {
                if ($payment_method instanceof EE_Payment_Method) {
                    // get the form and simplify it, like what we do when we display it
                    $pmt_form = $this->_generate_payment_method_settings_form($payment_method);
                    if ($pmt_form->form_data_present_in($this->_req_data)) {
                        $correct_pmt_form_to_use = $pmt_form;
                        break;
                    }
                }
            }
            // if we couldn't find the correct payment method type...
            if (! $correct_pmt_form_to_use) {
                EE_Error::add_error(
                    esc_html__(
                        "We could not find which payment method type your form submission related to. Please contact support",
                        'event_espresso'
                    ),
                    __FILE__,
                    __FUNCTION__,
                    __LINE__
                );
                $this->_redirect_after_action(false, 'Payment Method', 'activated', ['action' => 'default']);
            }
            $correct_pmt_form_to_use->receive_form_submission($this->_req_data);
            if ($correct_pmt_form_to_use->is_valid()) {
                $payment_settings_subform = $correct_pmt_form_to_use->get_subsection('payment_method_settings');
                if (! $payment_settings_subform instanceof EE_Payment_Method_Form) {
                    throw new EE_Error(
                        sprintf(
                            esc_html__(
                                'The payment method could not be saved because the form sections were misnamed. We expected to find %1$s, but did not.',
                                'event_espresso'
                            ),
                            'payment_method_settings'
                        )
                    );
                }
                $payment_settings_subform->save();
                /** @var $pm EE_Payment_Method */
                $this->_redirect_after_action(
                    true,
                    'Payment Method',
                    'updated',
                    ['action' => 'default', 'payment_method' => $payment_method->slug()]
                );
            } else {
                EE_Error::add_error(
                    sprintf(
                        esc_html__(
                            'Payment method of type %s was not saved because there were validation errors. They have been marked in the form',
                            'event_espresso'
                        ),
                        $payment_method instanceof EE_Payment_Method
                            ? $payment_method->type_obj()->pretty_name()
                            : esc_html__('"(unknown)"', 'event_espresso')
                    ),
                    __FILE__,
                    __FUNCTION__,
                    __LINE__
                );
            }
        }
    }


    /**
     * Displays payment settings (not payment METHOD settings, that's _payment_method_settings)
     *
     * @throws DomainException
     * @throws EE_Error
     * @throws InvalidArgumentException
     * @throws InvalidDataTypeException
     * @throws InvalidInterfaceException
     */
    protected function _payment_settings()
    {
        $form = $this->getPaymentSettingsForm();
        $this->_set_add_edit_form_tags('update_payment_settings');
        $this->_set_publish_post_box_vars();
        $this->_template_args['admin_page_content'] = EEH_HTML::div(
            $form->get_html_and_js(),
            '',
            'padding'
        );
        $this->display_admin_page_with_sidebar();
    }


    /**
     *        _update_payment_settings
     *
     * @access protected
     * @return void
     * @throws EE_Error
     * @throws InvalidArgumentException
     * @throws InvalidDataTypeException
     * @throws InvalidInterfaceException
     */
    protected function _update_payment_settings()
    {
        $form = $this->getPaymentSettingsForm();
        if ($form->was_submitted($this->_req_data)) {
            $form->receive_form_submission($this->_req_data);
            if ($form->is_valid()) {
                /**
                 * @var $reg_config EE_Registration_Config
                 */
                $loader                                   = LoaderFactory::getLoader();
                $reg_config                               = $loader->getShared('EE_Registration_Config');
                $valid_data                               = $form->valid_data();
                $show_pending_payment_options             = $valid_data['show_pending_payment_options'] ?? null;
                $reg_config->show_pending_payment_options = $show_pending_payment_options === 'ON';
                $reg_config->gateway_log_lifespan         = $valid_data['gateway_log_lifespan'];
            }
        }
        EE_Registry::instance()->CFG = apply_filters(
            'FHEE__Payments_Admin_Page___update_payment_settings__CFG',
            EE_Registry::instance()->CFG
        );

        $what    = esc_html__('Payment Settings', 'event_espresso');
        $success = $this->_update_espresso_configuration(
            $what,
            EE_Registry::instance()->CFG,
            __FILE__,
            __FUNCTION__,
            __LINE__
        );
        $this->_redirect_after_action(
            $success,
            $what,
            esc_html__('updated', 'event_espresso'),
            ['action' => 'payment_settings']
        );
    }


    /**
     * Gets the form used for updating payment settings
     *
     * @return EE_Form_Section_Proper
     * @throws EE_Error
     * @throws InvalidArgumentException
     * @throws InvalidDataTypeException
     * @throws InvalidInterfaceException
     */
    protected function getPaymentSettingsForm(): EE_Form_Section_Proper
    {
        /**
         * @var $reg_config EE_Registration_Config
         */
        $reg_config = LoaderFactory::getLoader()->getShared('EE_Registration_Config');
        return new EE_Form_Section_Proper(
            [
                'name'            => 'payment-settings',
                'layout_strategy' => new EE_Admin_Two_Column_Layout(),
                'subsections'     => [
                    'show_pending_payment_options' => new EE_Switch_Input(
                        [
                            'default'        => $reg_config->show_pending_payment_options
                                ? EE_Switch_Input::OPTION_ON
                                : EE_Switch_Input::OPTION_OFF,
                            'html_name'      => 'show_pending_payment_options',
                            'html_help_text' => esc_html__(
                                "If a payment is marked as 'Pending Payment', or if payment is deferred (ie, an offline gateway like Check, Bank, or Invoice is used), then give registrants the option to retry payment. ",
                                'event_espresso'
                            ),
                        ],
                        [
                            EE_Switch_Input::OPTION_OFF => esc_html__(
                                'pending payment options are NOT displayed',
                                'event_espresso'
                            ),
                            EE_Switch_Input::OPTION_ON  => esc_html__(
                                'pending payment options are displayed',
                                'event_espresso'
                            ),
                        ]
                    ),
                    'gateway_log_lifespan'         => new EE_Select_Input(
                        $reg_config->gatewayLogLifespanOptions(),
                        [
                            'html_label_text' => esc_html__('Gateway Logs Lifespan', 'event_espresso'),
                            'html_help_text'  => esc_html__(
                                'If issues arise with payments being made through a payment gateway, it\'s helpful to log non-sensitive communications with the payment gateway. But it\'s a security responsibility, so it\'s a good idea to not keep them for any longer than necessary.',
                                'event_espresso'
                            ),
                            'default'         => $reg_config->gateway_log_lifespan,
                        ]
                    ),
                ],
            ]
        );
    }


    /**
     * @throws EE_Error
     */
    protected function _payment_log_overview_list_table()
    {
        $this->display_admin_list_table_page_with_sidebar();
    }


    protected function _set_list_table_views_payment_log()
    {
        $this->_views = [
            'all' => [
                'slug'  => 'all',
                'label' => esc_html__('View All Logs', 'event_espresso'),
                'count' => 0,
            ],
        ];
    }


    /**
     * @param int  $per_page
     * @param int  $current_page
     * @param bool $count
     * @return array|int
     * @throws EE_Error
     * @throws ReflectionException
     */
    public function get_payment_logs($per_page = 50, $current_page = 0, $count = false)
    {
        EE_Registry::instance()->load_model('Change_Log');
        // we may need to do multiple queries (joining differently), so we actually want an array of query params
        $query_params = [['LOG_type' => EEM_Change_Log::type_gateway]];
        // check if they've selected a specific payment method
        if (isset($this->_req_data['_payment_method']) && $this->_req_data['_payment_method'] !== 'all') {
            $query_params[0]['OR*pm_or_pay_pm'] = [
                'Payment.Payment_Method.PMD_ID' => $this->_req_data['_payment_method'],
                'Payment_Method.PMD_ID'         => $this->_req_data['_payment_method'],
            ];
        }
        // take into account search
        if (isset($this->_req_data['s']) && $this->_req_data['s']) {
            $similarity_string                                                              =
                ['LIKE', '%' . str_replace("", "%", $this->_req_data['s']) . '%'];
            $query_params[0]['OR*s']['Payment.Transaction.Registration.Attendee.ATT_fname'] = $similarity_string;
            $query_params[0]['OR*s']['Payment.Transaction.Registration.Attendee.ATT_lname'] = $similarity_string;
            $query_params[0]['OR*s']['Payment.Transaction.Registration.Attendee.ATT_email'] = $similarity_string;
            $query_params[0]['OR*s']['Payment.Payment_Method.PMD_name']                     = $similarity_string;
            $query_params[0]['OR*s']['Payment.Payment_Method.PMD_admin_name']               = $similarity_string;
            $query_params[0]['OR*s']['Payment.Payment_Method.PMD_type']                     = $similarity_string;
            $query_params[0]['OR*s']['Payment_Method.PMD_name']                             = $similarity_string;
            $query_params[0]['OR*s']['Payment_Method.PMD_admin_name']                       = $similarity_string;
            $query_params[0]['OR*s']['Payment_Method.PMD_type']                             = $similarity_string;
            $query_params[0]['OR*s']['LOG_message']                                         = $similarity_string;
        }
        if (
            isset($this->_req_data['payment-filter-start-date'])
            && isset($this->_req_data['payment-filter-end-date'])
        ) {
            // add date
            $start_date = wp_strip_all_tags($this->_req_data['payment-filter-start-date']);
            $end_date   = wp_strip_all_tags($this->_req_data['payment-filter-end-date']);
            // make sure our timestamps start and end right at the boundaries for each day
            $start_date = date('Y-m-d', strtotime($start_date)) . ' 00:00:00';
            $end_date   = date('Y-m-d', strtotime($end_date)) . ' 23:59:59';
            // convert to timestamps
            $start_date = strtotime($start_date);
            $end_date   = strtotime($end_date);
            // makes sure start date is the lowest value and vice versa
            $start_date = min($start_date, $end_date);
            $end_date   = max($start_date, $end_date);
            // convert for query
            $start_date                  = EEM_Change_Log::instance()->convert_datetime_for_query(
                'LOG_time',
                date('Y-m-d H:i:s', $start_date),
                'Y-m-d H:i:s'
            );
            $end_date                    = EEM_Change_Log::instance()->convert_datetime_for_query(
                'LOG_time',
                date('Y-m-d H:i:s', $end_date),
                'Y-m-d H:i:s'
            );
            $query_params[0]['LOG_time'] = ['BETWEEN', [$start_date, $end_date]];
        }
        if ($count) {
            return EEM_Change_Log::instance()->count($query_params);
        }
        if (isset($this->_req_data['order'])) {
            $sort                     = (isset($this->_req_data['order']) && ! empty($this->_req_data['order']))
                ? $this->_req_data['order']
                : 'DESC';
            $query_params['order_by'] = ['LOG_time' => $sort];
        } else {
            $query_params['order_by'] = ['LOG_time' => 'DESC'];
        }
        $offset = ($current_page - 1) * $per_page;
        if (! isset($this->_req_data['download_results'])) {
            $query_params['limit'] = [$offset, $per_page];
        }
        // now they've requested to instead just download the file instead of viewing it.
        if (isset($this->_req_data['download_results'])) {
            $wpdb_results = EEM_Change_Log::instance()->get_all_efficiently($query_params);
            header('Content-Disposition: attachment');
            header("Content-Disposition: attachment; filename=ee_payment_logs_for_" . sanitize_key(site_url()));
            echo '<h1> '
                 . sprintf(
                     esc_html__('Payment Logs for %1$s', 'event_espresso'),
                     esc_url_raw(site_url())
                 )
                 . '</h1 >';
            echo '<h3>' . esc_html__('Query:', 'event_espresso') . '</h3>';
            echo esc_html(var_export($query_params, true));
            echo '<h3>' . esc_html__('Results:', 'event_espresso') . '</h3>';
            echo esc_html(var_export($wpdb_results, true));
            die;
        }
        return EEM_Change_Log::instance()->get_all($query_params);
    }


    /**
     * Used by usort to RE-sort log query results, because we lose the ordering
     * because we're possibly combining the results from two queries
     *
     * @param EE_Change_Log $logA
     * @param EE_Change_Log $logB
     * @return int
     * @throws EE_Error
     * @throws ReflectionException
     */
    protected function _sort_logs_again($logA, $logB)
    {
        $timeA = $logA->get_raw('LOG_time');
        $timeB = $logB->get_raw('LOG_time');
        if ($timeA == $timeB) {
            return 0;
        }
        $comparison = $timeA < $timeB
            ? -1
            : 1;
        if (strtoupper($this->_sort_logs_again_direction) == 'DESC') {
            return $comparison * -1;
        }
        return $comparison;
    }


    /**
     * @throws EE_Error
     * @throws ReflectionException
     */
    protected function _payment_log_details()
    {
        EE_Registry::instance()->load_model('Change_Log');
        /** @var $payment_log EE_Change_Log */
        $payment_log    = EEM_Change_Log::instance()->get_one_by_ID($this->_req_data['ID']);
        $payment_method = null;
        $transaction    = null;
        if ($payment_log instanceof EE_Change_Log) {
            if ($payment_log->object() instanceof EE_Payment) {
                $payment_method = $payment_log->object()->payment_method();
                $transaction    = $payment_log->object()->transaction();
            } elseif ($payment_log->object() instanceof EE_Payment_Method) {
                $payment_method = $payment_log->object();
            } elseif ($payment_log->object() instanceof EE_Transaction) {
                $transaction    = $payment_log->object();
                $payment_method = $transaction->payment_method();
            }
        }
        $this->_template_args['admin_page_content'] = EEH_Template::display_template(
            EE_PAYMENTS_TEMPLATE_PATH . 'payment_log_details.template.php',
            [
                'payment_log'    => $payment_log,
                'payment_method' => $payment_method,
                'transaction'    => $transaction,
            ],
            true
        );
        $this->display_admin_page_with_no_sidebar();
    }
}
