<?php

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

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

if ( !class_exists( 'Pricely_Admin_Checkout_Prices_Modes_Simple' ) ) {

    class Pricely_Admin_Checkout_Prices_Modes_Simple {

        public function __construct() {

            add_filter( 'pricely_admin_checkout-pricing_get-simple-mode-types', array( $this, 'get_modes' ), 10, 2 );

            add_filter( 'pricely_admin_checkout-pricing_get-excl-quantity-ids', array( $this, 'get_excl_quantity_ids' ), 10, 2 );

            add_filter( 'pricely_admin_checkout-pricing_get-mode-type-simple-fields', array( $this, 'get_fields' ), 10, 2 );
            
            add_filter( 'pricely_admin_checkout-pricing_process-simple-prcing-options', array( $this, 'process_options' ), 10, 3 );
        }

        public function get_modes( $modes, $args ) {

            $modes[ 'simple' ] = esc_html__( 'Simple price adjustment', 'pricely-dynamic-pricing-cart-discounts' );

            return $modes;
        }

        public function get_excl_quantity_ids( $mode_ids, $args ) {

            $mode_ids[] = 'simple';

            return $mode_ids;
        }

        public function get_fields( $in_fields, $args ) {

            $pricing_types = Pricely_Admin_Checkout_Prices_Modes::get_instance();

            $in_fields[] = array(
                'id' => 'simple_pricing',
                'type' => 'columns-field',
                'columns' => 7,
                'merge_fields' => true,
                'fields' => array(
                    array(
                        'id' => 'pricing_type',
                        'type' => 'select2',
                        'column_size' => 2,
                        'column_title' => esc_html__( 'Pricing - Type', 'pricely-dynamic-pricing-cart-discounts' ),
                        'tooltip' => esc_html__( 'Choose how the price should be calculated', 'pricely-dynamic-pricing-cart-discounts' ),
                        'default' => 'fixed_price_item',
                        'disabled_list_filter' => 'pricely_admin_get-disabled-grouped-list',
                        'options' => $pricing_types->get_pricing_types( 'simple' ),
                        'width' => '100%',
                        'fold_id' => 'simple_pricing_type',
                    ),
                    array(
                        'id' => 'amount',
                        'type' => 'textbox',
                        'input_type' => 'number',
                        'column_size' => 1,
                        'tooltip' => esc_html__( 'Specify an amount for price calculation based on the chosen "Pricing - Type"', 'pricely-dynamic-pricing-cart-discounts' ),
                        'column_title' => esc_html__( 'Amount', 'pricely-dynamic-pricing-cart-discounts' ),
                        'default' => '0.00',
                        'placeholder' => esc_html__( '0.00', 'pricely-dynamic-pricing-cart-discounts' ),
                        'attributes' => array(
                            'min' => '0',
                            'step' => '0.01',
                        ),
                        'width' => '100%',
                    ),
                    array(
                        'id' => 'per_based_on',
                        'type' => 'select2',
                        'column_size' => 2,
                        'column_title' => esc_html__( 'Percentage - Based On', 'pricely-dynamic-pricing-cart-discounts' ),
                        'tooltip' => esc_html__( 'Choose the basis for the percentage-based price calculation', 'pricely-dynamic-pricing-cart-discounts' ),
                        'default' => 'regular_price',
                        'disabled_list_filter' => 'pricely_admin_get-disabled-list',
                        'options' => $pricing_types->get_pricing_based_on_types( $args ),
                        'width' => '100%',
                        'fold' => array(
                            'target' => 'simple_pricing_type',
                            'attribute' => 'value',
                            'value' => array( 'per_price' ),
                            'oparator' => 'eq',
                        ),
                    ),
                ),
            );

            return $in_fields;
        }

        public function process_options( $options, $raw_options, $args ) {

            if ( isset( $raw_options[ 'simple_pricing' ] ) ) {

                $options[ 'simple_pricing' ] = $raw_options[ 'simple_pricing' ];
            }

            return $options;
        }
    }

    new Pricely_Admin_Checkout_Prices_Modes_Simple();
}