<?php

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

if ( !class_exists( 'Reon' ) ) {

    return;
}

if ( !class_exists( 'Pricely_Admin_Cart_Cache_Settings' ) ) {

    class Pricely_Admin_Cart_Cache_Settings {

        public function __construct() {

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

        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;
        }

        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 & Checkout - Cache', 'pricely-dynamic-pricing-cart-discounts' ),
                'desc' => esc_html__( 'Use these settings to configure caching of cart and checkout data', 'pricely-dynamic-pricing-cart-discounts' ),
            );

            $in_fields[] = array(
                'id' => 'cart_cache',
                'type' => 'columns-field',
                'columns' => 4,
                'merge_fields' => true,
                'fields' => $this->get_fields(),
            );

            return $in_fields;
        }

        private function get_fields() {

            $in_fields = array();

            $in_fields[] = array(
                'id' => 'enable',
                'type' => 'select2',
                'column_size' => 1,
                'column_title' => esc_html__( 'Enable', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Choose whether or not the caching of cart and checkout data should be enabled', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => array( 'no' ),
                'options' => array(
                    'no' => esc_html__( 'No', 'pricely-dynamic-pricing-cart-discounts' ),
                    'yes' => esc_html__( 'Yes', 'pricely-dynamic-pricing-cart-discounts' ),
                ),
                'width' => '100%',
                'fold_id' => 'cart_cache',
            );

            $in_fields[] = array(
                'id' => 'cache_params',
                'type' => 'select2',
                'column_size' => 3,
                'column_title' => esc_html__( 'Cache Parameters', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Specify the conditions for caching cart and checkout data based on parameters', 'pricely-dynamic-pricing-cart-discounts' ),
                'multiple' => true,
                'default' => array( 'cart_items_hash' ),
                'options' => $this->get_parameters(),
                'placeholder' => esc_html__( 'Select parameters...', 'pricely-dynamic-pricing-cart-discounts' ),
                'width' => '100%',
                'fold' => array(
                    'target' => 'cart_cache',
                    'attribute' => 'value',
                    'value' => array( 'yes' ),
                    'oparator' => 'eq',
                    'clear' => false,
                ),
            );

            return $in_fields;
        }

        private function get_parameters() {

            $parameter_types = array(
                'cart_items_hash' => esc_html__( 'Cart items', 'pricely-dynamic-pricing-cart-discounts' ),
            );

            return apply_filters( 'pricely_admin_get-cart-hash-parameter-types', $parameter_types );
        }

    }

    new Pricely_Admin_Cart_Cache_Settings();
}
