<?php

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

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

if ( !class_exists( 'Pricely_Admin_Logic_Types_Product_Filters' ) ) {

    class Pricely_Admin_Logic_Types_Product_Filters {

        private static $instance;
        private $option_name = PRICELY_OPTION_NAME;

        public static function get_instance(): self {

            if ( !self::$instance ) {

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

            return self::$instance;
        }

        public function __construct() {

            add_filter( 'reon_get-simple-repeater-field-pricely-product_args-templates', array( $this, 'get_templates' ), 10, 2 );
            add_filter( 'reon_get-simple-repeater-template-pricely-product_args-filter-fields', array( $this, 'get_template_fields' ), 10, 2 );
        }

        public function get_fields( $in_fields, $args, $fold_args = array() ) {

            $format_text = $args[ 'format_text' ];
            
            /* translators: 1:  module name */
            $desc = sprintf( esc_html__( 'Choose which products to include in the %s. Leave blank to include all products.', 'pricely-dynamic-pricing-cart-discounts' ), $format_text );

            if ( isset( $args[ 'desc_text' ] ) ) {

                $desc = $args[ 'desc_text' ];
            }

            $title_text = esc_html__( 'Products', 'pricely-dynamic-pricing-cart-discounts' );

            if ( isset( $args[ 'title_text' ] ) ) {

                $title_text = $args[ 'title_text' ];
            }

            $field = array(
                'id' => 'product_args',
                'type' => 'simple-repeater',
                'filter_id' => 'pricely-product_args',
                'new_field_args' => $args,
                'full_width' => true,
                'center_head' => true,
                'title' => $title_text,
                'desc' => $desc,
                '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 Product', 'pricely-dynamic-pricing-cart-discounts' ),
                ),
            );

            if ( count( $fold_args ) ) {

                $field[ 'fold' ] = $fold_args;
            }

            $in_fields[] = $field;

            return $in_fields;
        }

        public function get_templates( $in_templates, $repeater_args ) {

            if ( $repeater_args[ 'screen' ] == 'option-page' && $repeater_args[ 'option_name' ] == $this->option_name ) {
                $in_templates[] = array(
                    'id' => 'filter',
                );
            }

            return $in_templates;
        }

        public function get_template_fields( $in_fields, $repeater_args ) {

            $product_filter_types = Pricely_Admin_Product_Filter_Types::get_instance();

            $args = $repeater_args[ 'field_args' ];

            $list = array();

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

            foreach ( $groups as $group_id => $group_label ) {

                $list[ $group_id ][ 'label' ] = $group_label;

                $list[ $group_id ][ 'options' ] = $product_filter_types->get_product_filters( $group_id, $args );
            }


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


            $filters = array();

            foreach ( $list as $grp ) {

                if ( count( $grp[ 'options' ] ) > 0 ) {

                    $filters = array_merge( $filters, array_keys( $grp[ 'options' ] ) );
                }
            }


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


            foreach ( $filters as $filter ) {

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

                $in_fields[] = array(
                    'id' => 'filter_type_' . $filter,
                    'type' => 'group-field',
                    'dyn_switcher_target' => 'filter_type',
                    'dyn_switcher_target_value' => $filter,
                    'fluid-group' => true,
                    'width' => '67%',
                    'css_class' => array( 'rn-last' ),
                    'fields' => $product_filter_types->get_product_filter_fields( $filter, $args ),
                );
            }

            return $in_fields;
        }

    }

    Pricely_Admin_Logic_Types_Product_Filters::get_instance();
}

