<?php

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

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

if ( !class_exists( 'Pricely_Admin_Logic_Types_Conditions' ) ) {

    class Pricely_Admin_Logic_Types_Conditions {

        private static $instance;

        public static function get_instance(): self {

            if ( !self::$instance ) {

                self::$instance = new self();
            }

            return self::$instance;
        }

        public function __construct() {

            add_filter( 'pricely_admin_get-panel-conditions-fields', array( $this, 'get_fields' ), 20, 2 );

            add_filter( 'reon_get-simple-repeater-field-pricely-conditions-templates', array( $this, 'get_condition_templates' ), 10, 2 );
            add_filter( 'reon_get-simple-repeater-template-pricely-conditions-condition-fields', array( $this, 'get_condition_fields' ), 10, 2 );
        }

        public function get_fields( $in_fields, $args ) {

            $dec_allowed_html = array(
                'br' => array(),
            );

            if ( !class_exists( 'Pricely_Pro_Admin_Logic_Types_Conditions' ) ) {

                $in_fields[] = array(
                    'id' => 'match_mode',
                    'type' => 'select2',
                    'full_width' => true,
                    'center_head' => true,
                    'title' => esc_html__( 'Conditions', 'pricely-dynamic-pricing-cart-discounts' ),
                    /* translators: 1:  module name 2: module name */
                    'desc' => sprintf( wp_kses( __( 'Specify the conditions under which this %1$s should apply. If no conditions are specified, <br /> the %2$s will apply in all cases.', 'pricely-dynamic-pricing-cart-discounts' ), $dec_allowed_html ), $args[ 'format_text' ], $args[ 'format_text' ] ),
                    'default' => 'match_all',
                    'disabled_list_filter' => 'pricely_admin_get-disabled-list',
                    'options' => array(
                        'match_all' => esc_html__( 'All conditions should match', 'pricely-dynamic-pricing-cart-discounts' ),
                    ),
                    'width' => '320px',
                );
            }


            $in_fields[] = array(
                'id' => 'conditions',
                'filter_id' => 'pricely-conditions',
                'type' => 'simple-repeater',
                'new_field_args' => $args,
                'white_repeater' => false,
                'repeater_size' => 'smaller',
                'buttons_sep' => false,
                'buttons_box_width' => '65px',
                'css_class' => array( 'rn-round-small' ),
                'width' => '100%',
                'sortable' => array(
                    'enabled' => true,
                ),
                'template_adder' => array(
                    'position' => 'right',
                    'show_list' => false,
                    'button_text' => esc_html__( 'Add Condition', 'pricely-dynamic-pricing-cart-discounts' ),
                ),
            );

            return $in_fields;
        }

        public function get_condition_templates( $in_templates, $repeater_args ) {

            $in_templates[] = array(
                'id' => 'condition',
            );

            return $in_templates;
        }

        public function get_condition_fields( $in_fields, $repeater_args ) {

            $args = $repeater_args[ 'field_args' ];

            $list = array();

            $condition_types = Pricely_Admin_Condition_Types::get_instance();

            $groups = $condition_types->get_groups( $args );

            foreach ( $groups as $group_id => $group_label ) {
                $list[ $group_id ][ 'label' ] = $group_label;
                $list[ $group_id ][ 'options' ] = $condition_types->get_conditions( $group_id, $args );
            }


            $in_fields[] = array(
                'id' => 'condition_type',
                'type' => 'select2',
                'default' => '',
                'disabled_list_filter' => 'pricely_admin_get-disabled-grouped-list',
                'options' => $list,
                'width' => '98%',
                'box_width' => '33%',
                'dyn_switcher_id' => 'condition_type',
            );

            $conds = array();

            foreach ( $list as $grp ) {
                if ( count( $grp[ 'options' ] ) > 0 ) {
                    $conds = array_merge( $conds, array_keys( $grp[ 'options' ] ) );
                }
            }


            $disabled_list = Pricely_Admin_Page::get_instance()->get_grouped_disabled_list( array(), $conds );


            foreach ( $conds as $cond ) {

                if ( in_array( $cond, $disabled_list ) ) {
                    continue;
                }

                $in_fields[] = array(
                    'id' => 'condition_type_' . $cond,
                    'type' => 'group-field',
                    'dyn_switcher_target' => 'condition_type',
                    'dyn_switcher_target_value' => $cond,
                    'fluid-group' => true,
                    'width' => '67%',
                    'css_class' => array( 'rn-last' ),
                    'fields' => $condition_types->get_condition_fields( $cond, $args ),
                );
            }

            return $in_fields;
        }

    }

    Pricely_Admin_Logic_Types_Conditions::get_instance();
}
