<?php

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

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

if ( !class_exists( 'Pricely_Admin_Cart_Totals_Settings' ) ) {

    class Pricely_Admin_Cart_Totals_Settings {

        public function __construct() {

            add_filter( 'pricely_admin_get-settings-section-panels', array( $this, 'get_panel' ), 70 );

            add_filter( 'reon_get-repeater-field-pricely_cart_totals_calcs-templates', array( $this, 'get_templates' ), 10, 2 );

            add_filter( 'reon_get-repeater-template-pricely_cart_totals_calcs-calc_default-fields', array( $this, 'get_default_template_fields' ), 10, 2 );
            add_filter( 'reon_get-repeater-template-pricely_cart_totals_calcs-calc_option-fields', array( $this, 'get_other_template_fields' ), 10, 2 );
        }

        public function get_panel( $in_fields ) {

            $in_fields[] = array(
                'id' => 'any_id',
                'type' => 'panel',
                'last' => true,
                'white_panel' => false,
                'panel_size' => 'smaller',
                'width' => '100%',
                'field_css_class' => array( 'pricely_setting_panel' ),
                'css_class' => array( 'rn-round-small' ),
                'merge_fields' => false,
                'fields' => $this->get_panel_fields( array() ),
            );

            return $in_fields;
        }

        public function get_templates( $in_templates, $repeater_args ) {

            $in_templates[] = array(
                'id' => 'calc_default',
                'head' => array(
                    'title' => '',
                    'defaut_title' => esc_html__( 'Cart Totals Calculator', 'pricely-dynamic-pricing-cart-discounts' ),
                    'title_field' => 'admin_note',
                    'subtitle_field' => 'calc_for',
                ),
                'empy_button' => true,
            );

            $in_templates[] = array(
                'id' => 'calc_option',
                'head' => array(
                    'title' => '',
                    'defaut_title' => esc_html__( 'Cart Totals Calculator', 'pricely-dynamic-pricing-cart-discounts' ),
                    'title_field' => 'admin_note',
                    'subtitle_field' => 'calc_for',
                ),
            );

            return $in_templates;
        }

        public function get_default_template_fields( $in_fields, $repeater_args ) {

            return $this->get_fields( $in_fields, true );
        }

        public function get_other_template_fields( $in_fields, $repeater_args ) {

            return $this->get_fields( $in_fields, false );
        }

        private function get_panel_fields( $in_fields ) {

            $in_fields[] = array(
                'id' => 'any_id',
                'type' => 'paneltitle',
                'full_width' => true,
                'center_head' => true,
                'title' => esc_html__( 'Cart Totals Calculators', 'pricely-dynamic-pricing-cart-discounts' ),
                'desc' => esc_html__( 'Use these settings to configure how cart totals (e.g percentage-based totals) are calculated', 'pricely-dynamic-pricing-cart-discounts' ),
            );

            $in_fields[] = array(
                'id' => 'cart_totals_calcs',
                'filter_id' => 'pricely_cart_totals_calcs',
                'type' => 'repeater',
                'white_repeater' => true,
                'repeater_size' => 'smaller',
                'accordions' => true,
                'buttons_sep' => false,
                'delete_button' => true,
                'clone_button' => false,
                'width' => '100%',
                'default' => $this->get_default_calc(),
                'static_template' => 'calc_default',
                'section_type_id' => 'calc_option_type',
                'css_class' => array( 'pricely_calculator_options', 'rn-round-small' ),
                'auto_expand' => array(
                    'new_section' => true,
                    'cloned_section' => false,
                ),
                'sortable' => array(
                    'enabled' => false,
                ),
                'template_adder' => array(
                    'position' => 'right',
                    'show_list' => false,
                    'button_text' => esc_html__( 'Add Calculator', 'pricely-dynamic-pricing-cart-discounts' ),
                ),
            );

            return $in_fields;
        }

        private function get_fields( $in_fields, $is_default ) {

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

            $in_fields[] = array(
                'id' => 'any_ids',
                'type' => 'columns-field',
                'columns' => 4,
                'full_width' => true,
                'merge_fields' => false,
                'fields' => $this->get_main_fields( array(), $is_default ),
            );

            $in_fields[] = array(
                'id' => 'items_total',
                'type' => 'columns-field',
                'columns' => 3,
                'full_width' => true,
                'merge_fields' => true,
                'fields' => $this->get_items_total_fields( array() ),
                'fold' => array(
                    'target' => 'calc_for',
                    'attribute' => 'value',
                    'value' => array( 'cart-discounts' ),
                    'oparator' => 'eq',
                    'clear' => false,
                ),
            );

            return apply_filters( 'pricely_admin_get-cart-totals-fields', $in_fields, $is_default );
        }

        private function get_main_fields( $in_fields, $is_default ) {

            $in_fields[] = array(
                'id' => 'admin_note',
                'type' => 'textbox',
                'tooltip' => esc_html__( 'Adds a private note for reference purposes', 'pricely-dynamic-pricing-cart-discounts' ),
                'column_size' => 1,
                'column_title' => esc_html__( 'Admin Note', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => '',
                'placeholder' => esc_html__( 'Type here...', 'pricely-dynamic-pricing-cart-discounts' ),
                'width' => '100%',
            );

            $module_list = array(
                'cart-discounts' => esc_html__( 'Cart Discounts', 'pricely-dynamic-pricing-cart-discounts' ),
            );

            if ( !$is_default ) {

                $module_list = array(
                    'cart-discounts' => esc_html__( 'Cart Discounts', 'pricely-dynamic-pricing-cart-discounts' ),
                );
            }

            $in_fields[] = array(
                'id' => 'calc_for',
                'type' => 'select2',
                'column_size' => 1,
                'column_title' => esc_html__( 'Attach To', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Select the module to which the calculator belongs', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => 'cart-discounts',
                'options' => $module_list,
                'width' => '100%',
                'fold_id' => 'calc_for',
            );

            return $in_fields;
        }

        private function get_items_total_fields( $in_fields ) {

            $in_fields[] = array(
                'id' => 'mode',
                'type' => 'select2',
                'column_size' => 1,
                'column_title' => esc_html__( 'Items Total', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Choose whether items total should be included in the calculation', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => 'add_all',
                'options' => array(
                    'add_all' => esc_html__( 'Include all items total in the cart', 'pricely-dynamic-pricing-cart-discounts' ),
                ),
                'width' => '100%',
            );

            $in_fields[] = array(
                'id' => 'based_on',
                'type' => 'select2',
                'column_size' => 1,
                'column_title' => esc_html__( 'Item Totals - Based On', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Select the basis for calculating the price-based totals', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => 'sale_price',
                'options' => array(
                    'regular_price' => esc_html__( 'Product Prices - Regular price', 'pricely-dynamic-pricing-cart-discounts' ),
                    'sale_price' => esc_html__( 'Product Prices - Sale price', 'pricely-dynamic-pricing-cart-discounts' ),
                ),
                'width' => '100%',
            );

            $in_fields[] = array(
                'id' => 'tax_total',
                'type' => 'select2',
                'column_size' => 1,
                'column_title' => esc_html__( 'Items Total - Tax Total', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Choose whether items tax total should be included in the calculation', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => 'no',
                'options' => array(
                    'no' => esc_html__( 'Do not include tax total', 'pricely-dynamic-pricing-cart-discounts' ),
                    'yes' => esc_html__( 'Include tax total', 'pricely-dynamic-pricing-cart-discounts' ),
                ),
                'width' => '100%',
            );

            return $in_fields;
        }

        private function get_default_calc() {

            return array(
                array(
                    'calc_option_type' => 'calc_default',
                    'admin_note' => esc_html__( 'Subtotal Excluding Tax', 'pricely-dynamic-pricing-cart-discounts' ),
                    'items_total' => array(
                        'tax_total' => 'no'
                    ),
                    'option_id' => '2234343',
                ),
                array(
                    'calc_option_type' => 'calc_option',
                    'admin_note' => esc_html__( 'Subtotal Including Tax', 'pricely-dynamic-pricing-cart-discounts' ),
                    'items_total' => array(
                        'tax_total' => 'yes'
                    ),
                    'option_id' => '2234344',
                ),
            );
        }

    }

    new Pricely_Admin_Cart_Totals_Settings();
}