<?php

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

if ( !class_exists( 'Reon' ) ) {

    return;
}

if ( !class_exists( 'Pricely_Admin_Product_Promotion_Type_CountDown' ) ) {

    class Pricely_Admin_Product_Promotion_Type_CountDown {

        public function __construct() {

            add_filter( 'pricely_admin_product-promotions_get-main-promotion-types', array( $this, 'get_types' ), 10, 2 );

            add_filter( 'pricely_admin_product-promotions_get-promotion-type-countdown-fields', array( $this, 'get_fields' ), 10, 2 );
        }

        public function get_types( $in_options, $args = array() ) {

            $in_options[ 'countdown' ] = array(
                'title' => esc_html__( 'Countdown', 'pricely-dynamic-pricing-cart-discounts' ),
            );

            return $in_options;
        }

        public function get_fields( $in_fields, $args ) {

            $in_fields[] = array(
                'id' => 'any_ids',
                'type' => 'columns-field',
                'columns' => 8,
                'merge_fields' => false,
                'fields' => $this->get_panel_fields( $args ),
            );

            $in_fields[] = array(
                'id' => 'any_ids',
                'type' => 'columns-field',
                'columns' => 2,
                'merge_fields' => false,
                'fields' => $this->get_custom_fields( $args ),
                'fold' => array(
                    'target' => 'countdown_source',
                    'attribute' => 'value',
                    'value' => 'custom',
                    'oparator' => 'eq',
                    'clear' => false,
                ),
            );

            $in_fields[] = array(
                'id' => 'any_ids',
                'type' => 'columns-field',
                'columns' => 3,
                'merge_fields' => false,
                'fields' => $this->get_auto_refresh_fields( $args ),
            );

            $in_fields[] = array(
                'id' => 'shop',
                'type' => 'columns-field',
                'columns' => 8,
                'full_width' => true,
                'center_head' => true,
                'title' => esc_html__( 'Shop Loops', 'pricely-dynamic-pricing-cart-discounts' ),
                'desc' => esc_html__( "Use these settings to configure the display options for the countdown timer in shop loops", 'pricely-dynamic-pricing-cart-discounts' ),
                'field_css_class' => array( 'pricely_positions_title' ),
                'merge_fields' => true,
                'fields' => $this->get_shop_panel_fields( $args ),
            );

            $in_fields[] = array(
                'id' => 'single_product',
                'type' => 'columns-field',
                'columns' => 8,
                'full_width' => true,
                'center_head' => true,
                'title' => esc_html__( 'Product Summary', 'pricely-dynamic-pricing-cart-discounts' ),
                'desc' => esc_html__( "Use these settings to configure the display options for the countdown timer in product summary area", 'pricely-dynamic-pricing-cart-discounts' ),
                'field_css_class' => array( 'pricely_positions_title' ),
                'merge_fields' => true,
                'fields' => $this->get_product_panel_fields( $args ),
            );

            return $in_fields;
        }

        private function get_panel_fields( $args ) {

            $in_fields = array();

            $in_fields[] = array(
                'id' => 'countdown_source',
                'type' => 'select2',
                'column_size' => 3,
                'column_title' => esc_html__( 'Countdown Source', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Specifies the source of the countdown timer', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => 'custom',
                'disabled_list_filter' => 'pricely_admin_get-disabled-list',
                'options' => $this->get_sources(),
                'width' => '100%',
                'fold_id' => 'countdown_source',
            );

            $in_fields[] = array(
                'id' => 'custom_mode',
                'type' => 'select2',
                'column_size' => 3,
                'column_title' => esc_html__( 'Countdown Mode', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Specifies the mode for the custom countdown', 'pricely-dynamic-pricing-cart-discounts' ),
                'disabled_list_filter' => 'pricely_admin_get-disabled-grouped-list',
                'options' => $this->get_modes(),
                'default' => 'date',
                'width' => '100%',
                'fold_id' => 'countdown_mode',
                'fold' => array(
                    'target' => 'countdown_source',
                    'attribute' => 'value',
                    'value' => array( 'custom' ),
                    'oparator' => 'eq',
                    'clear' => true,
                    'empty' => 'date',
                ),
            );

            $in_fields[] = array(
                'id' => 'auto_hide_days',
                'type' => 'select2',
                'column_size' => 2,
                'column_title' => esc_html__( 'Auto Hide Days', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Controls the automatic hiding of the days counter when it reaches zero', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => 'yes',
                'options' => array(
                    'yes' => esc_html__( 'Yes', 'pricely-dynamic-pricing-cart-discounts' ),
                    'no' => esc_html__( 'No', 'pricely-dynamic-pricing-cart-discounts' ),
                ),
                'width' => '100%',
            );

            return $in_fields;
        }

        private function get_custom_fields( $args ) {

            return $this->get_date_fields( array(), $args );
        }

        private function get_auto_refresh_fields( $args ) {

            $in_fields = array();

            $in_fields[] = array(
                'id' => 'refresh_shop',
                'type' => 'select2',
                'column_size' => 1,
                'column_title' => esc_html__( 'Auto Refresh On Shop Page', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Determines whether the countdown should refresh the shop page when it reaches zero', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => 'no',
                'options' => array(
                    'yes' => esc_html__( 'Yes, refresh page', 'pricely-dynamic-pricing-cart-discounts' ),
                    'no' => esc_html__( 'No, do not refresh page', 'pricely-dynamic-pricing-cart-discounts' ),
                ),
                'width' => '100%',
            );

            $in_fields[] = array(
                'id' => 'refresh_single',
                'type' => 'select2',
                'column_size' => 1,
                'column_title' => esc_html__( 'Auto Refresh On Product Page', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Determines whether the countdown should refresh the product page when it reaches zero', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => 'no',
                'options' => array(
                    'refresh' => esc_html__( 'Yes, refresh product page', 'pricely-dynamic-pricing-cart-discounts' ),
                    'no' => esc_html__( 'No, do not refresh page', 'pricely-dynamic-pricing-cart-discounts' ),
                ),
                'width' => '100%',
                'fold_id' => 'countdown_refresh_mode',
            );


            return $in_fields;
        }

        private function get_shop_panel_fields( $args ) {

            $in_fields = array();

            $in_fields[] = array(
                'id' => 'enable',
                'type' => 'select2',
                'column_size' => 2,
                'column_title' => esc_html__( 'Show In Shop Loops', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Specify whether the countdown timer should be shown in shop loops', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => 'yes',
                'options' => array(
                    'yes' => esc_html__( 'Yes', 'pricely-dynamic-pricing-cart-discounts' ),
                    'no' => esc_html__( 'No', 'pricely-dynamic-pricing-cart-discounts' ),
                ),
                'width' => '100%',
                'fold_id' => 'shop_countdown_enable',
            );

            $in_fields[] = array(
                'id' => 'position',
                'type' => 'select2',
                'column_size' => 3,
                'column_title' => esc_html__( 'Shop Loops - Position', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( "Choose the display position of the countdown timer in the shop loops", 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => '',
                'data' => 'pricely:view_positions:shop',
                'width' => '100%',
                'fold' => array(
                    'target' => 'shop_countdown_enable',
                    'attribute' => 'value',
                    'value' => array( 'yes' ),
                    'oparator' => 'eq',
                    'clear' => false,
                ),
            );

            $in_fields[] = array(
                'id' => 'ui_id',
                'type' => 'select2',
                'column_size' => 3,
                'column_title' => esc_html__( 'Shop Loops - UI Design', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( "Select a UI design for the countdown timer in the shop loops", 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => 'inline_2234343',
                'data' => 'pricely:countdown_designs',
                'width' => '100%',
                'fold' => array(
                    'target' => 'shop_countdown_enable',
                    'attribute' => 'value',
                    'value' => array( 'yes' ),
                    'oparator' => 'eq',
                    'clear' => false,
                ),
            );

            return $in_fields;
        }

        private function get_product_panel_fields( $args ) {

            $in_fields = array();

            $in_fields[] = array(
                'id' => 'enable',
                'type' => 'select2',
                'column_size' => 2,
                'column_title' => esc_html__( 'Show In Product Summary', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Specify whether the countdown timer should be shown in product summary area', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => 'no',
                'options' => array(
                    'yes' => esc_html__( 'Yes', 'pricely-dynamic-pricing-cart-discounts' ),
                    'no' => esc_html__( 'No', 'pricely-dynamic-pricing-cart-discounts' ),
                ),
                'width' => '100%',
                'fold_id' => 'product_countdown_enable',
            );

            $in_fields[] = array(
                'id' => 'position',
                'type' => 'select2',
                'column_size' => 3,
                'column_title' => esc_html__( 'Product Summary - Position', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( "Choose the display position of the countdown timer in the product summary area", 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => '',
                'data' => 'pricely:view_positions:single-product',
                'width' => '100%',
                'fold' => array(
                    'target' => 'product_countdown_enable',
                    'attribute' => 'value',
                    'value' => array( 'yes' ),
                    'oparator' => 'eq',
                    'clear' => false,
                ),
            );

            $in_fields[] = array(
                'id' => 'ui_id',
                'type' => 'select2',
                'column_size' => 3,
                'column_title' => esc_html__( 'Product Summary - UI Design', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( "Select a UI design for the countdown timer in the product summary area", 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => 'inline_2234343',
                'data' => 'pricely:countdown_designs',
                'width' => '100%',
                'fold' => array(
                    'target' => 'product_countdown_enable',
                    'attribute' => 'value',
                    'value' => array( 'yes' ),
                    'oparator' => 'eq',
                    'clear' => false,
                ),
            );

            return $in_fields;
        }

        private function get_date_fields( $in_fields, $args ) {

            $in_fields[] = array(
                'id' => 'date',
                'type' => 'date',
                'column_size' => 1,
                'column_title' => esc_html__( 'Specific Date', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Enter a specific date to define the start or end of the countdown timer', 'pricely-dynamic-pricing-cart-discounts' ),
                'number_of_months' => 1,
                'show_button_panel' => false,
                'change_month' => true,
                'change_year' => true,
                'first_day' => 0,
                'default' => '',
                'placeholder' => esc_html__( 'yy-mm-dd', 'pricely-dynamic-pricing-cart-discounts' ),
                'date_format' => 'yy-mm-dd',
                'fold' => array(
                    'target' => 'countdown_mode',
                    'attribute' => 'value',
                    'value' => array( 'date' ),
                    'oparator' => 'eq',
                    'clear' => true,
                ),
                'width' => '100%',
            );

            $in_fields[] = array(
                'id' => 'to_date',
                'type' => 'date',
                'column_size' => 1,
                'column_title' => esc_html__( 'To Date', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Specify the date when the countdown timer should end', 'pricely-dynamic-pricing-cart-discounts' ),
                'number_of_months' => 1,
                'show_button_panel' => false,
                'change_month' => true,
                'change_year' => true,
                'first_day' => 0,
                'default' => '',
                'placeholder' => esc_html__( 'yy-mm-dd', 'pricely-dynamic-pricing-cart-discounts' ),
                'date_format' => 'yy-mm-dd',
                'fold' => array(
                    'target' => 'countdown_mode',
                    'attribute' => 'value',
                    'value' => array( 'to_date' ),
                    'oparator' => 'eq',
                    'clear' => true,
                ),
                'width' => '100%',
            );

            return $in_fields;
        }

        private function get_modes() {

            $options = array(
                'date' => array(
                    'label' => esc_html__( 'Date', 'pricely-dynamic-pricing-cart-discounts' ),
                    'options' => array(
                        'date' => esc_html__( 'Specific Date', 'pricely-dynamic-pricing-cart-discounts' ),
                        'to_date' => esc_html__( 'To Date', 'pricely-dynamic-pricing-cart-discounts' ),
                    )
                ),
            );

            return $options;
        }

        private function get_sources() {

            return array(
                'custom' => esc_html__( 'Custom countdown', 'pricely-dynamic-pricing-cart-discounts' ),
            );
        }

    }

    new Pricely_Admin_Product_Promotion_Type_CountDown();
}

