<?php

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

if ( !class_exists( 'Reon' ) ) {

    return;
}

if ( !class_exists( 'Pricely_Admin_Product_Filter_Type_Product' ) ) {

    class Pricely_Admin_Product_Filter_Type_Product {

        private $group_id;
        private $terms_prefix;
        private $logic_types_taxonomy;
        private $taxonomies;

        public function __construct() {

            $this->group_id = 'product';
            $this->terms_prefix = 'product_';
        }

        public function get_groups( $args ) {

            return array(
                $this->group_id => esc_html__( 'Product', 'pricely-dynamic-pricing-cart-discounts' )
            );
        }

        public function get_filters( $group_id, $args ) {

            if ( $group_id !== $this->group_id ) {

                return false;
            }

            $filters = array(
                'spec_products' => esc_html__( 'Product - Specific Products', 'pricely-dynamic-pricing-cart-discounts' ),
            );

            foreach ( $this->get_taxonomies() as $taxonomy_id => $taxonomy_data ) {

                $filters[ $taxonomy_id ] = $taxonomy_data[ 'label' ];
            }

            return $filters;
        }

        public function get_fields( $filter_id, $args ) {

            if ( 'spec_products' == $filter_id ) {

                return $this->get_specific_products_fields( array() );
            }

            if ( in_array( $filter_id, $this->get_taxonomy_ids() ) ) {

                return $this->get_product_terms_fields( array(), $filter_id );
            }

            return false;
        }

        private function get_specific_products_fields( $in_fields ) {

            $in_fields[] = array(
                'id' => 'compare',
                'type' => 'select2',
                'default' => 'in_list',
                'options' => array(
                    'in_list' => esc_html__( 'Any in the list', 'pricely-dynamic-pricing-cart-discounts' ),
                    'none' => esc_html__( 'None in the list', 'pricely-dynamic-pricing-cart-discounts' ),
                ),
                'width' => '98%',
                'box_width' => '25%',
            );

            $in_fields[] = array(
                'id' => 'product_slugs',
                'type' => 'select2',
                'multiple' => true,
                'minimum_input_length' => 2,
                'placeholder' => esc_html__( 'Search products...', 'pricely-dynamic-pricing-cart-discounts' ),
                'allow_clear' => true,
                'minimum_results_forsearch' => 10,
                'data' => array(
                    'source' => 'wc:products',
                    'ajax' => true,
                    'value_col' => 'slug',
                ),
                'width' => '100%',
                'box_width' => '56%',
            );

            return $in_fields;
        }

        private function get_product_terms_fields( $in_fields, $filter_id ) {

            $taxonomy_data = $this->get_taxonomy_by_id( $filter_id );

            if ( false === $taxonomy_data ) {

                return $in_fields;
            }

            $in_fields[] = array(
                'id' => 'compare',
                'type' => 'select2',
                'default' => 'in_list',
                'options' => array(
                    'in_list' => esc_html__( 'Any in the list', 'pricely-dynamic-pricing-cart-discounts' ),
                    'in_all_list' => esc_html__( 'All in the list', 'pricely-dynamic-pricing-cart-discounts' ),
                    'in_list_only' => esc_html__( 'Only in the list', 'pricely-dynamic-pricing-cart-discounts' ),
                    'in_all_list_only' => esc_html__( 'Only all in the list', 'pricely-dynamic-pricing-cart-discounts' ),
                    'none' => esc_html__( 'None in the list', 'pricely-dynamic-pricing-cart-discounts' ),
                ),
                'width' => '98%',
                'box_width' => '25%',
            );

            $in_fields[] = array(
                'id' => 'term_slugs',
                'type' => 'select2',
                'multiple' => true,
                'minimum_input_length' => 1,
                'placeholder' => $taxonomy_data[ 'search_text' ],
                'allow_clear' => true,
                'minimum_results_forsearch' => 10,
                'data' => array(
                    'source' => 'categories:' . $taxonomy_data[ 'taxonomy' ],
                    'ajax' => true,
                    'value_col' => 'slug',
                ),
                'width' => '100%',
                'box_width' => '56%',
            );

            return $in_fields;
        }

        private function get_taxonomy_by_id( $taxonomy_id ) {

            $taxonomies = $this->get_taxonomies();

            if ( !isset( $taxonomies[ $taxonomy_id ] ) ) {

                return false;
            }

            return $taxonomies[ $taxonomy_id ];
        }

        private function get_taxonomy_ids() {

            return array_keys( $this->get_taxonomies() );
        }

        private function get_taxonomies() {

            if ( !is_null( $this->taxonomies ) ) {

                return $this->taxonomies;
            }

            if ( is_null( $this->logic_types_taxonomy ) ) {

                $this->logic_types_taxonomy = Pricely_Admin_Logic_Types_Taxonomy::get_instance();
            }


            foreach ( $this->logic_types_taxonomy->get_taxonomies() as $taxonomy => $taxonomy_data ) {

                $taxonomy_key = $this->terms_prefix . $taxonomy;

                $data = array(
                    'taxonomy' => $taxonomy,
                );

                $label = $taxonomy_data[ 'label_singular' ];

                /* translators: 1: taxonomy name  */
                $data[ 'label' ] = sprintf( esc_html__( 'Product - %s', 'pricely-dynamic-pricing-cart-discounts' ), $label );

                $search_text = strtolower( $taxonomy_data[ 'label_plural' ] );
                /* translators: 1: terms search text  */
                $data[ 'search_text' ] = sprintf( esc_html__( 'Search %s...', 'pricely-dynamic-pricing-cart-discounts' ), $search_text );

                $this->taxonomies[ $taxonomy_key ] = $data;
            }

            return $this->taxonomies;
        }

    }

}
