<?php

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

if ( !class_exists( 'Reon' ) ) {

    return;
}

if ( !class_exists( 'Pricely_Admin_Product_Promotion_Type_Volumetric_Price_Table' ) ) {

    class Pricely_Admin_Product_Promotion_Type_Volumetric_Price_Table {

        public function __construct() {

            add_filter( 'pricely_admin_product-promotions_get-main-promotion-types', array( $this, 'get_types' ), 40, 2 );
            add_filter( 'pricely_admin_product-promotions_get-promotion-type-volumetric_price_table-fields', array( $this, 'get_fields' ), 40, 2 );

            add_filter( 'pricely_admin_product-promotions_get-type-requires-ex-products', array( $this, 'get_requires_ex_products' ), 10, 2 );
        }

        public function get_types( $in_options, $args = array() ) {

            $in_options[ 'volumetric_price_table' ] = array(
                'title' => esc_html__( 'Volumetric Pricing Table', 'pricely-dynamic-pricing-cart-discounts' ),
            );

            return $in_options;
        }

        public function get_fields( $in_fields, $args ) {

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

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

            $in_fields[] = array(
                'id' => 'shop',
                'type' => 'columns-field',
                'columns' => 3,
                'full_width' => true,
                'center_head' => true,
                'merge_fields' => true,
                'title' => esc_html__( 'Shop Loops', 'pricely-dynamic-pricing-cart-discounts' ),
                'desc' => esc_html__( "Use these settings to configure the display options for the volume pricing table in shop loops", 'pricely-dynamic-pricing-cart-discounts' ),
                'field_css_class' => array( 'pricely_positions_title' ),
                'fields' => $this->get_shop_panel_fields(),
            );

            $in_fields[] = array(
                'id' => 'single_product',
                'type' => 'columns-field',
                'columns' => 3,
                'full_width' => true,
                'center_head' => true,
                'merge_fields' => true,
                'title' => esc_html__( 'Product Summary', 'pricely-dynamic-pricing-cart-discounts' ),
                'desc' => esc_html__( "Use these settings to configure the display options for the volume pricing table in product summary area", 'pricely-dynamic-pricing-cart-discounts' ),
                'field_css_class' => array( 'pricely_positions_title' ),
                'fields' => $this->get_product_panel_fields(),
            );

            $in_fields[] = array(
                'id' => 'variatio_arg_any_id',
                'type' => 'columns-field',
                'columns' => 1,
                'full_width' => true,
                'center_head' => true,
                'merge_fields' => false,
                'fields' => $this->get_variation_view_args_fields(),
                'fold' => array(
                    'target' => 'product_table_enable',
                    'attribute' => 'value',
                    'value' => array( 'yes' ),
                    'oparator' => 'eq',
                    'clear' => false,
                ),
            );

            return $in_fields;
        }

        public function get_requires_ex_products( $in_options, $args ) {

            $in_options[] = 'volumetric_price_table';

            return $in_options;
        }

        private function get_panel_fields( $args ) {

            $in_fields = array();

            $in_fields[] = array(
                'id' => 'show_prices',
                'type' => 'select2',
                'column_size' => 1,
                'column_title' => esc_html__( 'Display Price Values', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( "Specify whether or not price values should be displayed in the volume pricing table", 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => 'yes',
                'options' => array(
                    'yes' => esc_html__( 'Yes, Display price values', 'pricely-dynamic-pricing-cart-discounts' ),
                    'no' => esc_html__( 'No, Do not display price values', 'pricely-dynamic-pricing-cart-discounts' ),
                ),
                'width' => '100%',
            );

            $in_fields[] = array(
                'id' => 'show_discounts',
                'type' => 'select2',
                'column_size' => 1,
                'column_title' => esc_html__( 'Display Discount Values', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( "Specify whether or not discount values should be displayed in the volume pricing table", 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => 'yes',
                'options' => array(
                    'yes' => esc_html__( 'Yes, Display discount values', 'pricely-dynamic-pricing-cart-discounts' ),
                    'no' => esc_html__( 'No, Do not display discount values', 'pricely-dynamic-pricing-cart-discounts' ),
                ),
                'width' => '100%',
            );

            $in_fields[] = array(
                'id' => 'show_discounts_per',
                'type' => 'select2',
                'column_size' => 1,
                'column_title' => esc_html__( 'Display Percentage Values', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( "Specify whether or not percentage values should be displayed in the volume pricing table", 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => 'yes',
                'options' => array(
                    'yes' => esc_html__( 'Yes, Display percentage values', 'pricely-dynamic-pricing-cart-discounts' ),
                    'no' => esc_html__( 'No, Do not display percentage values', 'pricely-dynamic-pricing-cart-discounts' ),
                ),
                'width' => '100%',
            );

            return $in_fields;
        }

        private function get_panel_two_fields( $args ) {

            $in_fields = array();

            $promotions_obj = Pricely_Admin_Product_Promotion_Types::get_instance();

            $in_fields[] = array(
                'id' => 'range_quantity_text',
                'type' => 'textbox',
                'column_size' => 2,
                'column_title' => esc_html__( 'Range - Quantity Text', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( "Set the text format for the quantity ranges in the volume pricing table", 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => esc_html__( '{{from_quantity}} - {{to_quantity}}', 'pricely-dynamic-pricing-cart-discounts' ),
                'placeholder' => esc_html__( 'Type here...', 'pricely-dynamic-pricing-cart-discounts' ),
                'width' => '100%',
            );

            $in_fields[] = array(
                'id' => 'from_quantity_text',
                'type' => 'textbox',
                'column_size' => 2,
                'column_title' => esc_html__( 'From - Quantity Text', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Set the text format for the minimum quantity in the volume pricing table when there is only a "from" quantity', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => esc_html__( '{{from_quantity}}+', 'pricely-dynamic-pricing-cart-discounts' ),
                'placeholder' => esc_html__( 'Type here...', 'pricely-dynamic-pricing-cart-discounts' ),
                'width' => '100%',
            );

            $in_fields[] = array(
                'id' => 'discount_based_on',
                'type' => 'select2',
                'column_size' => 3,
                'column_title' => esc_html__( 'Displayed Discounts - Based On', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( "Choose the basis for calculating the displayed discounts in the volume pricing table", 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => 'regular_price',
                'options' => $this->get_display_discount_based_on( $args ),
                'width' => '100%',
            );

            $in_fields[] = array(
                'id' => 'show_excluded_ranges',
                'type' => 'select2',
                'column_size' => 3,
                'column_title' => esc_html__( 'Display Missing Ranges', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( "Specify whether or not missing range values should be displayed in the volume pricing table", 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => 'no',
                'options' => array(
                    'no' => esc_html__( 'No, Do not display missing ranges', 'pricely-dynamic-pricing-cart-discounts' ),
                ),
                'width' => '100%',
            );


            return $in_fields;
        }

        private function get_shop_panel_fields() {

            $in_fields = array();

            $in_fields[] = array(
                'id' => 'enable',
                'type' => 'select2',
                'column_size' => 1,
                'column_title' => esc_html__( 'Show In Shop Loops', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Specify whether the volume pricing table should be shown in shop loops', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => 'no',
                'options' => array(
                    'yes' => esc_html__( 'Yes', 'pricely-dynamic-pricing-cart-discounts' ),
                    'no' => esc_html__( 'No', 'pricely-dynamic-pricing-cart-discounts' ),
                ),
                'width' => '100%',
                'fold_id' => 'shop_table_enable',
            );

            $in_fields[] = array(
                'id' => 'position',
                'type' => 'select2',
                'column_size' => 1,
                'column_title' => esc_html__( 'Shop Loops - Position', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( "Choose the display position of the volume pricing table in the shop loops", 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => '',
                'data' => 'pricely:view_positions:shop',
                'width' => '100%',
                'fold' => array(
                    'target' => 'shop_table_enable',
                    'attribute' => 'value',
                    'value' => array( 'yes' ),
                    'oparator' => 'eq',
                    'clear' => false,
                ),
            );

            $in_fields[] = array(
                'id' => 'ui_id',
                'type' => 'select2',
                'column_size' => 1,
                'column_title' => esc_html__( 'Shop Loops - UI Design', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( "Select a UI design for the volume pricing table in the shop loops", 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => '2234343',
                'data' => 'pricely:price_table_designs',
                'width' => '100%',
                'fold' => array(
                    'target' => 'shop_table_enable',
                    'attribute' => 'value',
                    'value' => array( 'no' ),
                    'oparator' => 'neq',
                    'clear' => false,
                ),
            );

            return $in_fields;
        }

        private function get_product_panel_fields() {

            $in_fields = array();

            $in_fields[] = array(
                'id' => 'enable',
                'type' => 'select2',
                'column_size' => 1,
                'column_title' => esc_html__( 'Show In Product Summary', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Specify whether the volume pricing table should be shown in product summary area', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => 'yes',
                'options' => array(
                    'yes' => esc_html__( 'Yes', 'pricely-dynamic-pricing-cart-discounts' ),
                    'no' => esc_html__( 'No', 'pricely-dynamic-pricing-cart-discounts' ),
                ),
                'width' => '100%',
                'fold_id' => 'product_table_enable',
            );

            $in_fields[] = array(
                'id' => 'position',
                'type' => 'select2',
                'column_size' => 1,
                'column_title' => esc_html__( 'Product Summary - Position', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( "Choose the display position of the volume pricing table in the product summary area", 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => '',
                'data' => 'pricely:view_positions:single-product',
                'width' => '100%',
                'fold' => array(
                    'target' => 'product_table_enable',
                    'attribute' => 'value',
                    'value' => array( 'yes' ),
                    'oparator' => 'eq',
                    'clear' => false,
                ),
            );

            $in_fields[] = array(
                'id' => 'ui_id',
                'type' => 'select2',
                'column_size' => 1,
                'column_title' => esc_html__( 'Product Summary - UI Design', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( "Select a UI design for the volume pricing table in the product summary area", 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => '2234343',
                'data' => 'pricely:price_table_designs',
                'width' => '100%',
                'fold' => array(
                    'target' => 'product_table_enable',
                    'attribute' => 'value',
                    'value' => array( 'no' ),
                    'oparator' => 'neq',
                    'clear' => false,
                ),
            );

            return $in_fields;
        }

        private function get_variation_view_args_fields() {

            $in_fields = array();

            $in_fields[] = array(
                'id' => 'variation_view_mode',
                'type' => 'select2',
                'column_size' => 1,
                'column_title' => esc_html__( 'Variations - Table Display', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Specify how product variations contents (i.e prices and/or discounts) should shown in table', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => 'combined',
                'options' => array(
                    'combined' => esc_html__( 'Display combined table content', 'pricely-dynamic-pricing-cart-discounts' ),
                ),
                'width' => '100%',
            );

            return $in_fields;
        }

        private function get_display_discount_based_on( $args ) {

            $promotions_obj = Pricely_Admin_Product_Promotion_Types::get_instance();

            $based_on = $promotions_obj->get_based_on( $args );

            return $based_on;
        }

    }

    new Pricely_Admin_Product_Promotion_Type_Volumetric_Price_Table();
}