<?php

defined( 'ABSPATH' ) or exit;

final class PWLPAR_Settings {

    public $settings;

    function __construct() {
        add_filter( 'woocommerce_settings_pages', array( $this, 'woocommerce_settings_pages' ) );
        add_filter( 'woocommerce_get_sections_advanced', array( $this, 'woocommerce_get_sections_advanced' ), 10, 1 );
        add_filter( 'woocommerce_get_settings_advanced', array( $this, 'woocommerce_get_settings_advanced' ), 10, 2 );
    }

    function get_settings() {
        if ( empty( $this->settings ) ) {
            $this->settings = array(
                array(
                    'title' => PWLPAR_PLUGIN_NAME,
                    'type'  => 'title',
                    'desc'  => '',
                    'id'    => 'pwlpar_options',
                ),
                array(
                    'title'    => __( 'Welcome wizard', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
                    'desc'     => __( 'Show the setup wizard when loading the plugin settings page.', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
                    'id'       => 'pwlpar_show_setup_wizard',
                    'default'  => 'yes',
                    'type'     => 'checkbox',
                    'desc_tip' => false,
                ),
                array(
                    'title'    => __( 'Rounding', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
                    'desc'     => __( 'Rounding method to use when calculating points earned for an order.', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
                    'id'       => 'pwlpar_rounding_method',
                    'default'  => 'round',
                    'type'     => 'radio',
                    'options'     => array(
                        'round'  => __( 'Round up at .5 and down at .4', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
                        'floor'  => __( 'Always round down', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
                        'ceil'  => __( 'Always round up', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
                    ),
                    'desc_tip' => false,
                ),
                array(
                    'title'    => __( 'Points on product page', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
                    'desc'     => __( 'Show the points earned for a product on the product page itself.', 'pw-loyalty-points-and-rewards-for-woocommerce' )
                                    . ' ' . __( 'Default: Checked', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
                    'id'       => 'pwlpar_show_points_on_product_page',
                    'default'  => 'yes',
                    'type'     => 'checkbox',
                    'desc_tip' => false,
                ),
                array(
                    'title'    => __( 'Hide Pimwick Plugins Menu', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
                    // translators: %s is the PW Rewards menu name.
                    'desc'     => sprintf( __( 'Do not show the Pimwick Plugins menu on the left. You can still access the dashboard via WooCommerce > %s', 'pw-loyalty-points-and-rewards-for-woocommerce' ), PWLPAR_PLUGIN_SHORT_NAME ),
                    'id'       => 'hide_pimwick_menu',
                    'default'  => 'no',
                    'type'     => 'checkbox',
                    'desc_tip' => false,
                ),
                array(
                    'type'  => 'sectionend',
                    'id'    => 'pwlpar_options',
                ),
            );
        }

        return $this->settings;
    }

    function woocommerce_settings_pages( $settings ) {

        $new_setting = array(
            'title'    => __( 'Rewards', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
            'desc'     => __( 'Endpoint for the "My account &rarr; Rewards" page.', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
            'id'       => 'woocommerce_myaccount_pwlpar_endpoint',
            'type'     => 'text',
            'default'  => 'rewards',
            'desc_tip' => true,
        );

        $insert_index = 0;
        foreach( $settings as $setting ) {
            if ( isset( $setting['type'] ) && $setting['type'] == 'sectionend' && isset( $setting['id'] ) && $setting['id'] == 'account_endpoint_options' ) {
                break;
            }
            $insert_index++;
        }

        $settings = array_slice( $settings, 0, $insert_index, true ) + array( $new_setting['id'] => $new_setting ) + array_slice( $settings, $insert_index, NULL, true );

        return $settings;
    }

    function woocommerce_get_sections_advanced( $sections ) {
        if ( ! isset( $sections['pwlpar'] ) ) {
            $sections['pwlpar'] = PWLPAR_PLUGIN_SHORT_NAME;
        }

        return $sections;
    }

    function woocommerce_get_settings_advanced( $settings, $current_section ): array {
        if ( 'pwlpar' !== $current_section ) {
            return $settings;
        }

        return $this->get_settings();
    }
}

return new PWLPAR_Settings();
