<?php

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

if ( !class_exists( 'Reon' ) ) {

    return;
}

if ( !class_exists( 'Pricely_Admin_Condition_Type_Customer' ) ) {

    class Pricely_Admin_Condition_Type_Customer {

        private $group_id;

        public function __construct() {

            $this->group_id = 'customer';
        }

        public function get_groups( $args ) {

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

        public function get_conditions( $group_id, $args ) {

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

                return false;
            }

            return array(
                'logged_in_user' => esc_html__( 'Customer - Is Logged In', 'pricely-dynamic-pricing-cart-discounts' ),
                'users' => esc_html__( 'Customer - Specific Customers', 'pricely-dynamic-pricing-cart-discounts' ),
                'user_roles' => esc_html__( 'Customer - User Roles', 'pricely-dynamic-pricing-cart-discounts' ),
            );
        }

        public function get_fields( $condition_id, $args ) {

            if ( 'logged_in_user' == $condition_id ) {

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

            if ( 'users' == $condition_id ) {

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

            if ( 'user_roles' == $condition_id ) {

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

            return false;
        }

        private function get_logged_in_user_fields( $in_fields ) {

            $in_fields[] = array(
                'id' => 'is_logged_in',
                'type' => 'select2',
                'default' => 'yes',
                'options' => array(
                    'no' => esc_html__( 'No', 'pricely-dynamic-pricing-cart-discounts' ),
                    'yes' => esc_html__( 'Yes', 'pricely-dynamic-pricing-cart-discounts' ),
                ),
                'width' => '100%',
                'box_width' => '100%',
            );

            return $in_fields;
        }

        private function get_users_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' => 'user_emails',
                'type' => 'select2',
                'multiple' => true,
                'minimum_input_length' => 2,
                'placeholder' => esc_html__( 'Search users...', 'pricely-dynamic-pricing-cart-discounts' ),
                'allow_clear' => true,
                'minimum_results_forsearch' => 10,
                'data' => array(
                    'source' => 'users',
                    'ajax' => true,
                    'value_col' => 'email',
                    'show_value' => true,
                ),
                'width' => '100%',
                'box_width' => '75%',
            );

            return $in_fields;
        }

        private function get_user_roles_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' ),
                    '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' => 'user_roles',
                'type' => 'select2',
                'multiple' => true,
                'minimum_input_length' => 1,
                'placeholder' => esc_html__( 'Search user roles...', 'pricely-dynamic-pricing-cart-discounts' ),
                'allow_clear' => true,
                'minimum_results_forsearch' => 10,
                'data' => array(
                    'source' => 'roles',
                    'ajax' => true,
                ),
                'width' => '100%',
                'box_width' => '75%',
            );

            return $in_fields;
        }

    }

}