<?php

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

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

if ( !class_exists( 'Pricely_Admin_Cart_Discounts_Page' ) ) {

    class Pricely_Admin_Cart_Discounts_Page {

        private $option_name = PRICELY_OPTION_NAME;

        public function __construct() {

            add_filter( 'reon_get-option-page-' . $this->option_name . 'section-cart_discounts-fields', array( $this, 'get_page_settings' ), 10, 2 );
            add_filter( 'reon_get-option-page-' . $this->option_name . 'section-cart_discounts-fields', array( $this, 'get_page_options' ), 20, 2 );
            
            add_filter( 'reon_get-repeater-field-cart_discounts-templates', array( $this, 'get_templates' ), 10, 2 );
            add_filter( 'reon_get-repeater-template-cart_discounts-cart_discount-fields', array( $this, 'get_fields' ), 10, 2 );
            add_filter( 'reon_get-repeater-template-cart_discounts-cart_discount-head-fields', array( $this, 'get_head_fields' ), 10, 2 );
        }

        public function get_page_settings( $in_fields, $section_id ) {
            
            if ( class_exists( 'Pricely_Pro_Admin_Cart_Discounts_Page' ) ) {
                
                return $in_fields;
            }

            $currency = get_woocommerce_currency_symbol( get_woocommerce_currency() );

            $in_fields[] = array(
                'id' => 'cart_discounts_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__( 'Discounts Limit', 'pricely-dynamic-pricing-cart-discounts' ),
                                'tooltip' => esc_html__( 'Determines if cart discounts 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 discount 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__( 'Defines circumstances for cart discounts 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' => 'cart_discounts',
                '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 Cart Discount', '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' => 'cart_discount',
                    'head' => array(
                        'title' => '',
                        'defaut_title' => esc_html__( 'Cart Discount', 'pricely-dynamic-pricing-cart-discounts' ),
                        'title_field' => 'title',
                        'subtitle_field' => 'admin_note',
                    )
                );
            }

            return $in_templates;
        }

        public function get_fields( $in_fields, $repeater_args ) {

            return apply_filters( 'pricely_admin_cart-discounts_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' => '280px',
                    ),
                    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[ 'cart_discounts' ] ) ) {

                return $options;
            }

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

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

            foreach ( $options[ 'cart_discounts' ] as $key => $cart_discount_options ) {

                $options[ 'cart_discounts' ][ $key ] = apply_filters( 'pricely_admin_cart-discounts_process-options', $this->process_discount_coupon_code( $cart_discount_options ) );
            }

            return $options;
        }

        private function process_discount_coupon_code( $cart_discount_options ) {

            $coupon_code = $cart_discount_options[ 'coupon_code' ];

            $cart_discount_options[ 'coupon_code' ] = $this->format_coupon_code( $coupon_code );

            return $cart_discount_options;
        }

        private function get_apply_method() {

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

            $apply_methods = apply_filters( 'pricely_admin_cart-discounts_get-apply-methods', $apply_methods );

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

            return $apply_methods;
        }

        private function get_apply_modes() {

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

            $apply_modes = apply_filters( 'pricely_admin_cart-discounts_get-apply-modes', $apply_modes );

            return $apply_modes;
        }

        private function format_coupon_code( $coupon_code ) {

            try {

                if ( function_exists( 'wc_format_coupon_code' ) ) {

                    return wc_format_coupon_code( $coupon_code );
                }

                if ( function_exists( 'wc_strtolower' ) ) {

                    return wc_strtolower( $coupon_code );
                }

                return $coupon_code;
            }
            catch ( Exception $exc ) {
                
            }

            return $coupon_code;
        }

    }

}

