<?php

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

if ( !class_exists( 'Reon' ) ) {

    return;
}

if ( !class_exists( 'Pricely_Admin_Product_Promotions_Panel_Options' ) ) {

    class Pricely_Admin_Product_Promotions_Panel_Options {

        private $template_added;
        private $option_name = PRICELY_OPTION_NAME;
        private $admin_page;

        public function __construct() {

            $this->template_added = array();

            add_filter( 'pricely_admin_product-promotions_get-panels', array( $this, 'get_panel' ), 20 );

            add_filter( 'reon_get-repeater-field-pricely-product_promotion_list-templates', array( $this, 'get_option_templates' ), 10, 2 );
            add_filter( 'reon_get-repeater-field-pricely-product_promotion_list-template-groups', array( $this, 'get_option_template_groups' ), 10, 2 );

            add_filter( 'reon_get-repeater-field-pricely-product_promotion_list-disabled-templates', array( $this, 'get_disabled_option_list' ), 10, 2 );
        }

        public function get_panel( $in_fields ) {

            $in_fields[] = array(
                'id' => 'any_id0',
                'type' => 'panel',
                'full_width' => true,
                'center_head' => true,
                'white_panel' => false,
                'panel_size' => 'smaller',
                'width' => '100%',
                'merge_fields' => false,
                'last' => true,
                'field_css_class' => array( 'pricely_next_panel' ),
                'css_class' => array( 'pricely_option_list_panel' ),
                'fields' => $this->get_panel_fields( array() ),
            );

            return $in_fields;
        }

        public function get_panel_fields( $in_fields ) {

            $in_fields[] = array(
                'id' => 'any_id1',
                'type' => 'panel',
                'full_width' => true,
                'center_head' => true,
                'white_panel' => true,
                'panel_size' => 'smaller',
                'width' => '100%',
                'last' => true,
                'field_css_class' => array( 'pricely_option_list_title' ),
                'css_class' => array( 'rn-round-small-top' ),
                'fields' => array(
                    array(
                        'id' => 'any_id',
                        'type' => 'paneltitle',
                        'full_width' => true,
                        'center_head' => true,
                        'title' => esc_html__( 'Settings', 'pricely-dynamic-pricing-cart-discounts' ),
                        'desc' => esc_html__( 'Use this section to configure the promotion settings to apply to eligible products.', 'pricely-dynamic-pricing-cart-discounts' ),
                    )
                ),
            );

            $option_adder_with = '250px';
            
            if ( class_exists( 'Pricely_Pro_Admin_Product_Promotion_Types' ) ) {

                $option_adder_with = '240px';
            }

            $in_fields[] = array(
                'id' => 'promotions',
                'filter_id' => 'pricely-product_promotion_list',
                'type' => 'repeater',
                'white_repeater' => true,
                'repeater_size' => 'smaller',
                'section_type_id' => 'promotion_type',
                'collapsible' => false,
                'accordions' => false,
                'buttons_sep' => false,
                'delete_button' => true,
                'clone_button' => false,
                'field_css_class' => array( 'pricely_option_list_box' ),
                'css_class' => array( 'pricely_option_list_box_inner', 'rn-round-small-bottom' ),
                'width' => '100%',
                'auto_expand' => array(
                    'all_section' => true,
                    'new_section' => true,
                    'default_section' => true,
                    'cloned_section' => true,
                ),
                'sortable' => array(
                    'enabled' => true,
                ),
                'template_adder' => array(
                    'position' => 'right',
                    'show_list' => true,
                    'list_icon' => 'fa fa-list',
                    'list_width' => $option_adder_with,
                    'button_text' => esc_html__( 'Add Settings', 'pricely-dynamic-pricing-cart-discounts' ),
                ),
            );

            return $in_fields;
        }

        public function get_option_templates( $in_templates, $repeater_args ) {

            if ( $repeater_args[ 'screen' ] != 'option-page' || $repeater_args[ 'option_name' ] != $this->option_name ) {

                return $in_templates;
            }

            $args = array(
                'module' => 'product-promotions',
                'format_text' => esc_html__( 'settings', 'pricely-dynamic-pricing-cart-discounts' ),
            );

            $promotions_obj = Pricely_Admin_Product_Promotion_Types::get_instance();

            $options = $promotions_obj->get_types( $args );


            foreach ( $options as $key => $option ) {

                if ( !isset( $option[ 'title' ] ) ) {

                    $option[ 'title' ] = $option[ 'list_title' ];
                }

                $template = array(
                    'id' => $key,
                    'list_label' => $option[ 'list_title' ],
                    'head' => array(
                        'title' => $option[ 'title' ],
                    )
                );

                if ( isset( $option[ 'group_id' ] ) ) {

                    $template[ 'group_id' ] = $option[ 'group_id' ];
                }

                if ( isset( $option[ 'tooltip' ] ) ) {

                    $template[ 'head' ][ 'tooltip' ] = $option[ 'tooltip' ];
                }

                $in_templates[] = $template;

                if ( !in_array( $key, $this->template_added ) ) {

                    add_filter( 'reon_get-repeater-template-pricely-product_promotion_list-' . $key . '-fields', array( $this, 'get_option_fields' ), 10, 2 );

                    $this->template_added[] = $key;
                }
            }

            return $in_templates;
        }

        public function get_option_template_groups( $in_groups, $repeater_args ) {

            if ( $repeater_args[ 'screen' ] != 'option-page' || $repeater_args[ 'option_name' ] != $this->option_name ) {

                return $in_groups;
            }

            $args = array(
                'module' => 'product-promotions',
                'format_text' => esc_html__( 'settings', 'pricely-dynamic-pricing-cart-discounts' ),
            );

            $promotions_obj = Pricely_Admin_Product_Promotion_Types::get_instance();

            $in_groups = $promotions_obj->get_groups( $args );

            return $in_groups;
        }

        public function get_option_fields( $in_fields, $repeater_args ) {

            $option_type_id = $repeater_args[ 'id' ];

            $args = array(
                'module' => 'product-promotions',
                'format_text' => esc_html__( 'settings', 'pricely-dynamic-pricing-cart-discounts' ),
                'desc_text' => esc_html__( 'Choose the products to which these settings should be applied. Leave blank to apply the settings to all products.', 'pricely-dynamic-pricing-cart-discounts' ),
            );

            $in_fields[] = array(
                'id' => 'id',
                'type' => 'autoid',
                'autoid' => 'pricely',
            );

            $promotions_obj = Pricely_Admin_Product_Promotion_Types::get_instance();

            return $promotions_obj->get_fields( $option_type_id, $in_fields, $args );
        }

        public function get_disabled_option_list( $list, $args ) {
            
            if ( class_exists( 'Pricely_Pro_Admin_Product_Promotion_Types' ) ) {

                return $list;
            }

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

                $this->admin_page = Pricely_Admin_Page::get_instance();
            }

            $args = array(
                'module' => 'product-promotions',
                'format_text' => esc_html__( 'settings', 'pricely-dynamic-pricing-cart-discounts' ),
            );

            $promotions_obj = Pricely_Admin_Product_Promotion_Types::get_instance();

            $grouped_options = array();

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

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

                $grouped_options[ $group_key ] = array(
                    'label' => $group_label,
                    'options' => array()
                );
            }


            $options = $promotions_obj->get_types( $args );

            foreach ( $options as $option_key => $option ) {

                $group_id = $option[ 'group_id' ];

                $grouped_options[ $group_id ][ 'options' ][ $option_key ] = $option[ 'list_title' ];
            }

            return $this->admin_page->get_grouped_disabled_list( $list, $grouped_options );
        }

    }

    new Pricely_Admin_Product_Promotions_Panel_Options();
}