<?php

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

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

if ( !class_exists( 'Pricely_Admin_Cart_Discounts_Panel_Discounts' ) ) {

    class Pricely_Admin_Cart_Discounts_Panel_Discounts {

        private $amount_type_id = 'discounts';
        private $template_id = 'discount_amount';

        public function __construct() {

            add_filter( 'pricely_admin_cart-discounts_get-panels', array( $this, 'get_panel' ), 20 );

            add_filter( 'reon_get-repeater-field-pricely-discounts-templates', array( $this, 'get_templates' ), 10, 2 );
            add_filter( 'reon_get-repeater-template-pricely-discounts-discount_amount-fields', array( $this, 'get_fields' ), 10, 2 );

            add_filter( 'pricely_admin_cart-discounts_process-options', array( $this, 'process_options' ), 10 );
        }

        public function get_panel( $in_fields ) {

            $in_fields[] = array(
                'id' => 'any_id0',
                'type' => 'panel',
                'full_width' => true,
                'center_head' => true,
                'white_panel' => false,
                'panel_size' => 'smaller',
                'width' => '100%',
                'merge_fields' => false,
                'last' => true,
                'field_css_class' => array( 'pricely_next_panel' ),
                'css_class' => array( 'pricely_option_list_panel' ),
                'fields' => $this->get_panel_fields( array() ),
            );

            return $in_fields;
        }

        public function get_panel_fields( $in_fields ) {

            $in_fields[] = array(
                'id' => 'any_id1',
                'type' => 'panel',
                'full_width' => true,
                'center_head' => true,
                'white_panel' => true,
                'panel_size' => 'smaller',
                'width' => '100%',
                'last' => true,
                'field_css_class' => array( 'pricely_option_list_title' ),
                'css_class' => array( 'rn-round-small-top' ),
                'fields' => array(
                    array(
                        'id' => 'any_id',
                        'type' => 'paneltitle',
                        'full_width' => true,
                        'center_head' => true,
                        'title' => esc_html__( 'Amounts', 'pricely-dynamic-pricing-cart-discounts' ),
                        'desc' => esc_html__( 'Use these settings to set up amounts for the cart discount', 'pricely-dynamic-pricing-cart-discounts' ),
                    )
                ),
            );


            $in_fields[] = array(
                'id' => $this->amount_type_id,
                'filter_id' => 'pricely-discounts',
                'type' => 'repeater',
                'white_repeater' => true,
                'repeater_size' => 'smaller',
                'collapsible' => false,
                'accordions' => false,
                'buttons_sep' => false,
                'delete_button' => true,
                'clone_button' => false,
                'field_css_class' => array( 'pricely_option_list_box' ),
                'css_class' => array( 'pricely_option_list_box_inner', 'rn-round-small-bottom' ),
                'default' => array( array( 'amount_type' => 'fixed_amount' ) ),
                'width' => '100%',
                'auto_expand' => array(
                    'all_section' => true,
                    'new_section' => true,
                    'default_section' => true,
                    'cloned_section' => true,
                ),
                'sortable' => array(
                    'enabled' => true,
                ),
                'template_adder' => array(
                    'position' => 'right',
                    'button_text' => esc_html__( 'Add Amount', 'pricely-dynamic-pricing-cart-discounts' ),
                ),
            );

            return $in_fields;
        }

        public function get_templates( $in_templates, $repeater_args ) {

            $in_templates[] = array(
                'id' => $this->template_id,
                'head' => array(
                    'title' => '',
                    'defaut_title' => esc_html__( 'Discount', 'pricely-dynamic-pricing-cart-discounts' ),
                    'title_field' => 'amount_type',
                )
            );

            return $in_templates;
        }

        public function get_fields( $in_fields, $repeater_args ) {

            $args = array(
                'module' => 'cart-discounts',
                'sub_module' => 'amounts',
                'fold_id' => 'amount_type',
                'format_text' => esc_html__( 'amount calculations', 'pricely-dynamic-pricing-cart-discounts' ),
            );

            $amount_types_obj = Pricely_Admin_Amount_Types::get_instance();

            $in_fields[] = array(
                'id' => 'id',
                'type' => 'autoid',
                'autoid' => 'pricely',
            );

            $amount_types = $amount_types_obj->get_types( $args );

            $in_fields[] = array(
                'id' => 'any_ids',
                'type' => 'columns-field',
                'columns' => 8,
                'merge_fields' => false,
                'first' => true,
                'fields' => array(
                    array(
                        'id' => 'amount_type',
                        'type' => 'select2',
                        'column_size' => 3,
                        'column_title' => esc_html__( 'Amount - Type', 'pricely-dynamic-pricing-cart-discounts' ),
                        'tooltip' => esc_html__( 'Specify how the amount is calculated', 'pricely-dynamic-pricing-cart-discounts' ),
                        'default' => 'fixed_amount',
                        'disabled_list_filter' => 'pricely_admin_get-disabled-grouped-list',
                        'options' => $amount_types,
                        'attributes' => array(
                            'data-repeater-title' => $this->amount_type_id,
                        ),
                        'width' => '100%',
                        'fold_id' => 'amount_type',
                    ),
                    array(
                        'id' => 'adjustment_type',
                        'type' => 'select2',
                        'column_size' => 3,
                        'column_title' => esc_html__( 'Amounts - Adjustment', 'pricely-dynamic-pricing-cart-discounts' ),
                        'tooltip' => esc_html__( 'Select how this amount is added to previous amounts', 'pricely-dynamic-pricing-cart-discounts' ),
                        'default' => 'add',
                        'disabled_list_filter' => 'pricely_admin_get-disabled-list',
                        'options' => $amount_types_obj->get_amount_add_methods( $args ),
                        'width' => '100%',
                    ),
                    array(
                        'id' => 'apply_conditions',
                        'type' => 'select2',
                        'column_size' => 2,
                        'column_title' => esc_html__( 'Apply Conditions', 'pricely-dynamic-pricing-cart-discounts' ),
                        'tooltip' => esc_html__( 'Choose whether to apply conditions when calculating the amount', 'pricely-dynamic-pricing-cart-discounts' ),
                        'default' => 'no',
                        'disabled_list_filter' => 'pricely_admin_get-disabled-list',
                        'options' => $this->get_apply_conditions_options(),
                        'width' => '100%',
                        'fold_id' => 'apply_conditions',
                    ),
                ),
            );

            $in_fields[] = array(
                'id' => 'any_ids',
                'type' => 'columns-field',
                'columns' => 12,
                'merge_fields' => false,
                'fields' => $this->get_amount_box_fields( array(), $args ),
            );


            foreach ( $amount_types_obj->get_type_fields( $args ) as $type_field ) {

                $in_fields[] = $type_field;
            }

            return $amount_types_obj->get_other_fields( $in_fields, $args );
        }

        public function process_options( $options ) {

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

                $options[ 'discounts' ] = array();

                return $options;
            }

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

                $options[ 'discounts' ] = array();

                return $options;
            }

            $amount_types_obj = Pricely_Admin_Amount_Types::get_instance();

            $args = array(
                'module' => 'cart-discounts',
                'is_taxable' => false,
            );

            do_action( 'pricely_admin_before-process-amounts-options', $args );

            foreach ( $options[ 'discounts' ] as $key => $discount_options ) {

                $options[ 'discounts' ][ $key ] = $amount_types_obj->process_options( $discount_options, $args );
            }

            return $options;
        }

        private function get_amount_box_fields( $in_fields, $args ) {

            $in_fields[] = array(
                'id' => 'amount',
                'type' => 'textbox',
                'input_type' => 'number',
                'tooltip' => esc_html__( 'Specify the amount based on the chosen "Amount - Type"', 'pricely-dynamic-pricing-cart-discounts' ),
                'column_size' => 2,
                'column_title' => esc_html__( 'Amount', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => '0.00',
                'placeholder' => esc_html__( '0.00', 'pricely-dynamic-pricing-cart-discounts' ),
                'width' => '100%',
                'attributes' => array(
                    'min' => '0',
                    'step' => '0.01',
                ),
            );

            return Pricely_Admin_Amount_Types::get_instance()->get_amount_box_fields( $in_fields, $args );
        }

        private function get_apply_conditions_options() {

            if ( class_exists( 'Pricely_Pro_Admin_Cart_Discounts_Page' ) ) {

                return array(
                    'no' => esc_html__( 'No, Do not apply conditions', 'pricely-dynamic-pricing-cart-discounts' ),
                    'yes' => esc_html__( 'Yes, Apply conditions', 'pricely-dynamic-pricing-cart-discounts' ),
                );
            }

            return array(
                'no' => esc_html__( 'No, Do not apply conditions', 'pricely-dynamic-pricing-cart-discounts' ),
            );
        }

    }

    new Pricely_Admin_Cart_Discounts_Panel_Discounts();
}
