<?php

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

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

if ( !class_exists( 'Pricely_Admin_Product_Prices_Types_Sale' ) ) {

    class Pricely_Admin_Product_Prices_Types_Sale {

        public function __construct() {

            add_filter( 'pricely_admin_product-pricing_get-mode-type-sale-fields', array( $this, 'get_fields' ), 10, 2 );

            add_filter( 'pricely_admin_product-pricing_get-mode-types', array( $this, 'get_modes' ), 20, 2 );

            add_filter( 'pricely_admin_product-pricing_get-adjustment-types', array( $this, 'get_adjustment_types' ), 10, 2 );

            add_filter( 'pricely_admin_product-pricing_get-adjustment-based-on-types', array( $this, 'get_adjustment_based_on_types' ), 20, 2 );
        }

        public function get_modes( $in_modes, $args ) {

            $in_modes[ 'sale' ] = esc_html__( 'Sale Price Adjustment', 'pricely-dynamic-pricing-cart-discounts' );

            return $in_modes;
        }

        public function get_fields( $in_fields, $args ) {

            $pricing_types = Pricely_Admin_Product_Prices_Types::get_instance();

            $in_fields[] = array(
                'id' => 'sale_adj',
                'type' => 'columns-field',
                'full_width' => true,
                'center_head' => true,
                'title' => esc_html__( 'Sale Price Adjustment', 'pricely-dynamic-pricing-cart-discounts' ),
                'desc' => esc_html__( 'Use these settings to set up sale price adjustment.', 'pricely-dynamic-pricing-cart-discounts' ),
                'columns' => 11,
                'fields' => $this->get_sale_field( $in_fields, $args ),
                'fold' => array(
                    'target' => 'product_prices_mode',
                    'attribute' => 'value',
                    'value' => array( 'reg_sale', 'sale' ),
                    'oparator' => 'eq',
                ),
            );

            return $in_fields;
        }

        public function get_adjustment_types( $adj_types, $args ) {

            $currency = get_woocommerce_currency_symbol( get_woocommerce_currency() );

            if ( !class_exists( 'Pricely_Admin_Product_Prices_Types_Regular' ) ) {

                /* translators: 1:  currency symbol */
                $adj_types[ 'fixed_price' ] = sprintf( esc_html__( 'Fixed price (%s)', 'pricely-dynamic-pricing-cart-discounts' ), $currency );

                $adj_types[ 'per_discount' ] = esc_html__( 'Percentage discount (%)', 'pricely-dynamic-pricing-cart-discounts' );
            }

            return $adj_types;
        }

        public function get_adjustment_based_on_types( $based_on_types, $args ) {

            if ( !class_exists( 'Pricely_Admin_Product_Prices_Types_Regular' ) ) {

                $based_on_types[ 'regular_price' ] = esc_html__( 'Regular price', 'pricely-dynamic-pricing-cart-discounts' );
            }

            return $based_on_types;
        }

        private function get_sale_field( $in_fields, $args ) {

            $pricing_types = Pricely_Admin_Product_Prices_Types::get_instance();

            $cal_from_visibility = $pricing_types->get_calculate_from_visibility( $args );

            $in_fields[] = array(
                'id' => 'adj_type',
                'type' => 'select2',
                'column_size' => 3,
                'column_title' => esc_html__( 'Adjustment Type', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Determines how sale price should be adjusted', 'pricely-dynamic-pricing-cart-discounts' ),
                'disabled_list_filter' => 'pricely_admin_get-disabled-list',
                'default' => $pricing_types->get_default_adjustment_types( $args ),
                'options' => $pricing_types->get_adjustment_types( $args ),
                'width' => '100%',
                'fold_id' => 'sale_adj_type',
            );

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

            $in_fields[] = array(
                'id' => 'discount_based_on',
                'type' => 'select2',
                'column_size' => 3,
                'column_title' => esc_html__( 'Discount - Based On', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Choose the basis for adjusting prices based on the selected discount adjustment type.', 'pricely-dynamic-pricing-cart-discounts' ),
                'disabled_list_filter' => 'pricely_admin_get-disabled-list',
                'default' => 'regular_price',
                'options' => $pricing_types->get_adjustment_based_on_types( $args ),
                'width' => '100%',
                'fold' => array(
                    'target' => 'sale_adj_type',
                    'attribute' => 'value',
                    'value' => $cal_from_visibility[ 'discount' ],
                    'oparator' => 'eq',
                ),
            );
            
            if ( class_exists( 'Pricely_Admin_Product_Prices_Types_Regular' ) ) {

                $in_fields[] = array(
                    'id' => 'fee_based_on',
                    'type' => 'select2',
                    'column_size' => 3,
                    'column_title' => esc_html__( 'Fee - Based On', 'pricely-dynamic-pricing-cart-discounts' ),
                    'tooltip' => esc_html__( 'Choose the basis for adjusting prices based on the selected fee adjustment type.', 'pricely-dynamic-pricing-cart-discounts' ),
                    'disabled_list_filter' => 'pricely_admin_get-disabled-list',
                    'default' => 'regular_price',
                    'options' => $pricing_types->get_adjustment_based_on_types( $args ),
                    'width' => '100%',
                    'fold' => array(
                        'target' => 'sale_adj_type',
                        'attribute' => 'value',
                        'value' => $cal_from_visibility[ 'fee' ],
                        'oparator' => 'eq',
                    ),
                );
            }

            $in_fields[] = array(
                'id' => 'per_based_on',
                'type' => 'select2',
                'column_size' => 3,
                'column_title' => esc_html__( 'Percentage - Based On', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Determines basis for percentage-based price adjustment.', 'pricely-dynamic-pricing-cart-discounts' ),
                'disabled_list_filter' => 'pricely_admin_get-disabled-list',
                'default' => 'regular_price',
                'options' => $pricing_types->get_adjustment_based_on_types( $args ),
                'width' => '100%',
                'fold' => array(
                    'target' => 'sale_adj_type',
                    'attribute' => 'value',
                    'value' => $pricing_types->get_adjustment_based_on_visibility( $args ),
                    'oparator' => 'eq',
                ),
            );

            return $in_fields;
        }

    }

    new Pricely_Admin_Product_Prices_Types_Sale();
}

