<?php

if ( !defined( 'ABSPATH' ) ) {
    exit;
}

if ( !class_exists( 'Reon' ) ) {
    return;
}

if ( !class_exists( 'Pricely_Admin_Checkout_Prices_Page' ) ) {

    class Pricely_Admin_Checkout_Prices_Page {

        private $option_name = PRICELY_OPTION_NAME;

        public function __construct() {

            add_filter( 'reon_get-option-page-' . $this->option_name . 'section-checkout_prices-fields', array( $this, 'get_page_settings' ), 10, 2 );
            add_filter( 'reon_get-option-page-' . $this->option_name . 'section-checkout_prices-fields', array( $this, 'get_page_options' ), 20, 2 );

            add_filter( 'reon_get-repeater-field-checkout_pricings-templates', array( $this, 'get_templates' ), 10, 2 );
            add_filter( 'reon_get-repeater-template-checkout_pricings-checkout_pricing-fields', array( $this, 'get_fields' ), 10, 2 );
            add_filter( 'reon_get-repeater-template-checkout_pricings-checkout_pricing-head-fields', array( $this, 'get_head_fields' ), 10, 2 );
        }

        public function get_page_settings( $in_fields, $section_id ) {

            if ( class_exists( 'Pricely_Pro_Admin_Checkout_Prices_Page' ) ) {

                return $in_fields;
            }

            $currency = get_woocommerce_currency_symbol( get_woocommerce_currency() );

            $in_fields[] = array(
                'id' => 'checkout_pricings_settings',
                'type' => 'panel',
                'last' => true,
                'white_panel' => false,
                'panel_size' => 'smaller',
                'width' => '100%',
                'field_css_class' => array( 'pricely_apply_mode' ),
                'css_class' => array( 'rn-round-top' ),
                'fields' => array(
                    array(
                        'id' => 'checkout_prices_any_id',
                        'type' => 'columns-field',
                        'columns' => 5,
                        'merge_fields' => false,
                        'fields' => array(
                            array(
                                'id' => 'limit_type',
                                'type' => 'select2',
                                'column_size' => 2,
                                'column_title' => esc_html__( 'Pricing Limit', 'pricely-dynamic-pricing-cart-discounts' ),
                                'tooltip' => esc_html__( 'Choose whether pricing should be limited to a specific amount', 'pricely-dynamic-pricing-cart-discounts' ),
                                'default' => 'no',
                                'disabled_list_filter' => 'pricely_admin_get-disabled-grouped-list',
                                'options' => array(
                                    'no' => array(
                                        'label' => esc_html__( 'No Limit', 'pricely-dynamic-pricing-cart-discounts' ),
                                        'options' => array(
                                            'no' => esc_html__( 'No pricing limit', 'pricely-dynamic-pricing-cart-discounts' ),
                                        ),
                                    ),
                                ),
                                'width' => '100%',
                            ),
                            array(
                                'id' => 'mode',
                                'type' => 'select2',
                                'column_size' => 3,
                                'column_title' => esc_html__( 'Apply Method', 'pricely-dynamic-pricing-cart-discounts' ),
                                'tooltip' => esc_html__( 'Choose the circumstances for pricing settings activation', 'pricely-dynamic-pricing-cart-discounts' ),
                                'default' => 'all',
                                'disabled_list_filter' => 'pricely_admin_get-disabled-list',
                                'options' => $this->get_apply_method(),
                                'width' => '100%',
                            ),
                        ),
                    ),
                ),
            );

            return $in_fields;
        }

        public function get_page_options( $in_fields, $section_id ) {

            $in_fields[] = array(
                'id' => 'checkout_pricings',
                'type' => 'repeater',
                'white_repeater' => false,
                'repeater_size' => 'small',
                'accordions' => true,
                'buttons_sep' => false,
                'delete_button' => true,
                'clone_button' => true,
                'width' => '100%',
                'field_css_class' => array( 'pricely_options' ),
                'css_class' => array( 'pricely_extension_options', 'rn-round-bottom' ),
                'auto_expand' => array(
                    'new_section' => true,
                    'cloned_section' => true,
                ),
                'sortable' => array(
                    'enabled' => true,
                ),
                'template_adder' => array(
                    'position' => 'right',
                    'show_list' => false,
                    'button_text' => esc_html__( 'New Pricing At Checkout', 'pricely-dynamic-pricing-cart-discounts' ),
                ),
            );

            return $in_fields;
        }

        public function get_templates( $in_templates, $repeater_args ) {

            if ( $repeater_args[ 'screen' ] == 'option-page' && $repeater_args[ 'option_name' ] == $this->option_name ) {

                $in_templates[] = array(
                    'id' => 'checkout_pricing',
                    'head' => array(
                        'title' => '',
                        'defaut_title' => esc_html__( 'Pricing At Checkout', 'pricely-dynamic-pricing-cart-discounts' ),
                        'title_field' => 'admin_note',
                        'subtitle_field' => 'mode',
                    )
                );
            }

            return $in_templates;
        }

        public function get_fields( $in_fields, $repeater_args ) {

            return apply_filters( 'pricely_admin_checkout-pricing_get-panels', array() );
        }

        public function get_head_fields( $in_fields, $repeater_args ) {

            $in_fields[] = array(
                'id' => 'any_id',
                'type' => 'group-field',
                'position' => 'right',
                'width' => '100%',
                'merge_fields' => false,
                'fields' => array(
                    array(
                        'id' => 'apply_mode',
                        'type' => 'select2',
                        'default' => 'with_others',
                        'disabled_list_filter' => 'pricely_admin_get-disabled-list',
                        'options' => $this->get_apply_modes(),
                        'width' => '270px',
                    ),
                    array(
                        'id' => 'enable',
                        'type' => 'select2',
                        'default' => 'yes',
                        'options' => array(
                            'yes' => esc_html__( 'Enable', 'pricely-dynamic-pricing-cart-discounts' ),
                            'no' => esc_html__( 'Disable', 'pricely-dynamic-pricing-cart-discounts' ),
                        ),
                        'width' => '95px',
                    ),
                ),
            );

            return $in_fields;
        }

        public function process_options( $options ) {

            if ( !isset( $options[ 'checkout_pricings' ] ) ) {

                return $options;
            }

            if ( !is_array( $options[ 'checkout_pricings' ] ) ) {

                $options[ 'checkout_pricings' ] = array();
            }

            foreach ( $options[ 'checkout_pricings' ] as $key => $checkout_pricings_options ) {

                $options[ 'checkout_pricings' ][ $key ] = apply_filters( 'pricely_admin_checkout-pricing_process-options', $checkout_pricings_options );
            }

            return $options;
        }

        private function get_apply_method() {

            $apply_methods = array(
                'all' => esc_html__( 'Apply all valid pricing', 'pricely-dynamic-pricing-cart-discounts' ),
            );

            $apply_methods = apply_filters( 'pricely_admin_checkout-pricing_get-apply-methods', $apply_methods );

            $apply_methods[ 'no' ] = esc_html__( 'Do not apply any pricing', 'pricely-dynamic-pricing-cart-discounts' );

            return $apply_methods;
        }

        private function get_apply_modes() {

            $apply_modes = array(
                'with_others' => esc_html__( 'Apply this and other pricing', 'pricely-dynamic-pricing-cart-discounts' ),
            );

            return apply_filters( 'pricely_admin_checkout-pricing_get-apply-modes', $apply_modes );
        }

    }

}