<?php

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

if ( !class_exists( 'Reon' ) ) {

    return;
}

if ( !class_exists( 'Pricely_Admin_Product_Promotions_Page' ) ) {

    class Pricely_Admin_Product_Promotions_Page {

        private $option_name = PRICELY_OPTION_NAME;

        public function __construct() {

            add_filter( 'reon_get-option-page-' . $this->option_name . 'section-product_promotions-fields', array( $this, 'get_page_fields' ), 10, 2 );
            add_filter( 'reon_get-repeater-field-product_promotions-templates', array( $this, 'get_templates' ), 10, 2 );
            add_filter( 'reon_get-repeater-template-product_promotions-product_promotion-fields', array( $this, 'get_fields' ), 10, 2 );
            add_filter( 'reon_get-repeater-template-product_promotions-product_promotion-head-fields', array( $this, 'get_head_fields' ), 10, 2 );
        }

        public function get_page_fields( $in_fields, $section_id ) {

            $in_fields[] = array(
                'id' => 'product_promotions_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' => 'product_promotions_any_id',
                        'type' => 'columns-field',
                        'columns' => 1,
                        'merge_fields' => false,
                        'fields' => array(
                            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 promotion settings activation', 'pricely-dynamic-pricing-cart-discounts' ),
                                'default' => 'all',
                                'disabled_list_filter' => 'pricely_admin_get-disabled-list',
                                'options' => $this->get_apply_method(),
                                'width' => '100%',
                            ),
                        ),
                    ),
                ),
            );

            $in_fields[] = array(
                'id' => 'product_promotions',
                '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 Promotion Settings', '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' => 'product_promotion',
                    'head' => array(
                        'title' => '',
                        'defaut_title' => esc_html__( 'Promotion Settings', 'pricely-dynamic-pricing-cart-discounts' ),
                        'title_field' => 'admin_note',
                    )
                );
            }

            return $in_templates;
        }

        public function get_fields( $in_fields, $repeater_args ) {

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

                return apply_filters( 'pricely_admin_product-promotions_get-panels', array() );
            }

            return $in_fields;
        }

        public function get_head_fields( $in_fields, $repeater_args ) {

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

                $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' => '290px',
                        ),
                        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;
        }

        private function get_apply_method() {


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

            $apply_methods = apply_filters( 'pricely_admin_product-promotions_get-apply-methods', $apply_methods );

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

            return $apply_methods;
        }

        private function get_apply_modes() {

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

            return apply_filters( 'pricely_admin_product-promotions_get-apply-modes', $apply_modes );
        }

    }

}

