<?php

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

if ( !class_exists( 'Reon' ) ) {

    return;
}

if ( !class_exists( 'Pricely_Admin_Settings_Styles_Price_Table_Settings' ) ) {

    class Pricely_Admin_Settings_Styles_Price_Table_Settings {

        private $admin_utils;

        public function __construct() {

            add_filter( 'pricely_admin_get-settings-styles-section-panels', array( $this, 'get_panel' ), 20 );

            add_filter( 'reon_get-repeater-field-pricely_pricing_tables-templates', array( $this, 'get_templates' ), 10, 2 );

            add_filter( 'reon_get-repeater-template-pricely_pricing_tables-ui_default-fields', array( $this, 'get_template_fields' ), 10, 2 );
            add_filter( 'reon_get-repeater-template-pricely_pricing_tables-ui_option-fields', array( $this, 'get_template_fields' ), 10, 2 );
        }

        public function get_panel( $in_fields ) {

            $this->init_settings();

            $in_fields[] = array(
                'id' => 'any_id',
                'type' => 'panel',
                'last' => true,
                'white_panel' => false,
                'panel_size' => 'smaller',
                'width' => '100%',
                'merge_fields' => false,
                'css_class' => array( 'rn-round-small' ),
                'fields' => $this->get_fields( array() ),
            );

            return $in_fields;
        }

        public function get_templates( $in_templates, $repeater_args ) {

            $in_templates[] = array(
                'id' => 'ui_default',
                'head' => array(
                    'title' => '',
                    'defaut_title' => esc_html__( 'Product Pricing Table', 'pricely-dynamic-pricing-cart-discounts' ),
                    'title_field' => 'admin_note',
                ),
                'empy_button' => true,
            );

            $in_templates[] = array(
                'id' => 'ui_option',
                'head' => array(
                    'title' => '',
                    'defaut_title' => esc_html__( 'Product Pricing Table', 'pricely-dynamic-pricing-cart-discounts' ),
                    'title_field' => 'admin_note',
                ),
            );

            return $in_templates;
        }

        public function get_template_fields( $in_fields, $repeater_args ) {

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

            $in_fields[] = array(
                'id' => 'any_ids',
                'type' => 'columns-field',
                'columns' => 1,
                'full_width' => true,
                'merge_fields' => false,
                'fields' => $this->admin_note_fields( array() ),
            );

            $in_fields[] = array(
                'id' => 'any_ids',
                'type' => 'columns-field',
                'columns' => 4,
                'merge_fields' => false,
                'full_width' => true,
                'center_head' => true,
                'title' => esc_html__( 'Table Settings', 'pricely-dynamic-pricing-cart-discounts' ),
                'desc' => esc_html__( 'Use these settings to control the table layout', 'pricely-dynamic-pricing-cart-discounts' ),
                'field_css_class' => array( 'pricely_subtitles' ),
                'fields' => $this->table_layout_fields( array() ),
            );

            $in_fields[] = array(
                'id' => 'any_ids',
                'type' => 'columns-field',
                'columns' => 4,
                'merge_fields' => false,
                'full_width' => true,
                'center_head' => true,
                'title' => esc_html__( 'Header Text Settings', 'pricely-dynamic-pricing-cart-discounts' ),
                'desc' => esc_html__( 'Use these settings to control the table header texts', 'pricely-dynamic-pricing-cart-discounts' ),
                'field_css_class' => array( 'pricely_subtitles' ),
                'fields' => $this->header_texts_fields( array() ),
            );

            $in_fields[] = array(
                'id' => 'any_ids',
                'type' => 'columns-field',
                'columns' => 5,
                'merge_fields' => false,
                'full_width' => true,
                'center_head' => true,
                'title' => esc_html__( 'Cells Settings', 'pricely-dynamic-pricing-cart-discounts' ),
                'desc' => esc_html__( 'Use these settings to control the table cells', 'pricely-dynamic-pricing-cart-discounts' ),
                'field_css_class' => array( 'pricely_subtitles' ),
                'fields' => $this->cell_fields( array() ),
            );

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

            return $in_fields;
        }

        private function get_fields( $in_fields ) {

            $in_fields[] = array(
                'id' => 'any_id',
                'type' => 'paneltitle',
                'full_width' => true,
                'center_head' => true,
                'title' => esc_html__( 'Product Pricing Table - Designs', 'pricely-dynamic-pricing-cart-discounts' ),
                'desc' => esc_html__( 'Use these settings to create and manage product pricing table designs', 'pricely-dynamic-pricing-cart-discounts' ),
            );

            $in_fields[] = array(
                'id' => 'ui_pricing_tables',
                'filter_id' => 'pricely_pricing_tables',
                'type' => 'repeater',
                'white_repeater' => true,
                'repeater_size' => 'smaller',
                'accordions' => true,
                'buttons_sep' => false,
                'delete_button' => true,
                'clone_button' => false,
                'width' => '100%',
                'css_class' => array( 'rn-round-small' ),
                'default' => $this->get_default_ui(),
                'static_template' => 'ui_default',
                'section_type_id' => 'option_type',
                'auto_expand' => array(
                    'new_section' => true,
                    'cloned_section' => false,
                ),
                'sortable' => array(
                    'enabled' => false,
                ),
                'template_adder' => array(
                    'position' => 'right',
                    'show_list' => false,
                    'button_text' => esc_html__( 'New Design', 'pricely-dynamic-pricing-cart-discounts' ),
                ),
            );

            return $in_fields;
        }

        private function admin_note_fields( $in_fields ) {

            $in_fields[] = array(
                'id' => 'admin_note',
                'type' => 'textbox',
                'tooltip' => esc_html__( 'Adds a private note for reference purposes', 'pricely-dynamic-pricing-cart-discounts' ),
                'column_size' => 1,
                'column_title' => esc_html__( 'Admin Note', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => '',
                'placeholder' => esc_html__( 'Type here...', 'pricely-dynamic-pricing-cart-discounts' ),
                'width' => '100%',
            );

            return $in_fields;
        }

        private function table_layout_fields( $in_fields ) {

            $in_fields[] = array(
                'id' => 'layout',
                'type' => 'select2',
                'column_size' => 1,
                'column_title' => esc_html__( 'Orientation', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Determines the layout of the table', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => 'vertical',
                'options' => array(
                    'vertical' => esc_html__( 'Vertical', 'pricely-dynamic-pricing-cart-discounts' ),
                    'horizontal' => esc_html__( 'Horizontal', 'pricely-dynamic-pricing-cart-discounts' )
                ),
                'width' => '100%',
            );

            $in_fields[] = array(
                'id' => 'width',
                'type' => 'group-field',
                'merge_fields' => true,
                'fluid-group' => true,
                'column_size' => 1,
                'column_title' => esc_html__( 'Width', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Determines the width of the table', 'pricely-dynamic-pricing-cart-discounts' ),
                'width' => '100%',
                'fields' => $this->admin_utils->get_size_fields( array( 'default_size' => '', 'placeholder' => 'auto' ) ),
            );

            $in_fields[] = array(
                'id' => 'margin',
                'type' => 'group-field',
                'column_no_size' => true,
                'merge_fields' => true,
                'fluid-group' => true,
                'column_title' => esc_html__( 'Margin', 'pricely-dynamic-pricing-cart-discounts' ),
                'column_classes' => array( 'pricely_box_hint' ),
                'tooltip' => esc_html__( 'Determines the margin of the table', 'pricely-dynamic-pricing-cart-discounts' ),
                'width' => '100%',
                'fields' => $this->admin_utils->get_boundary_sizes_fields( array( 'default_size' => array( 'top' => 0, 'right' => -1, 'bottom' => 0, 'left' => -1 ), 'placeholder' => '', 'default_unit' => 'px' ) ),
            );

            return $in_fields;
        }

        private function header_texts_fields( $in_fields ) {

            $in_fields[] = array(
                'id' => 'pricing_titles_text',
                'type' => 'textbox',
                'column_size' => 1,
                'column_title' => esc_html__( 'Quantity &amp; Adjustment - Header Text', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Determines the text of the price titles header cell', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => esc_html__( 'Header Text', 'pricely-dynamic-pricing-cart-discounts' ),
                'placeholder' => esc_html__( 'Type here...', 'pricely-dynamic-pricing-cart-discounts' ),
                'width' => '100%',
            );

            $in_fields[] = array(
                'id' => 'price_text',
                'type' => 'textbox',
                'column_size' => 1,
                'column_title' => esc_html__( 'Price Header Text', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Determines the text of the price header cell', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => esc_html__( 'Price', 'pricely-dynamic-pricing-cart-discounts' ),
                'placeholder' => esc_html__( 'Type here...', 'pricely-dynamic-pricing-cart-discounts' ),
                'width' => '100%',
            );

            $in_fields[] = array(
                'id' => 'discount_text',
                'type' => 'textbox',
                'column_size' => 1,
                'column_title' => esc_html__( 'Discount Header Text', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Determines the text of the discount header cell', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => esc_html__( 'Discount', 'pricely-dynamic-pricing-cart-discounts' ),
                'placeholder' => esc_html__( 'Type here...', 'pricely-dynamic-pricing-cart-discounts' ),
                'width' => '100%',
            );

            $in_fields[] = array(
                'id' => 'discount_per_text',
                'type' => 'textbox',
                'column_size' => 1,
                'column_title' => esc_html__( 'Discount Percent Header Text', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Determines the text of the discount percent header cell', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => esc_html__( 'Discount %', 'pricely-dynamic-pricing-cart-discounts' ),
                'placeholder' => esc_html__( 'Type here...', 'pricely-dynamic-pricing-cart-discounts' ),
                'width' => '100%',
            );

            return $in_fields;
        }

        private function cell_fields( $in_fields ) {

            $in_fields[] = array(
                'id' => 'font_size',
                'type' => 'group-field',
                'column_width' => '170px',
                'merge_fields' => true,
                'fluid-group' => true,
                'column_title' => esc_html__( 'Font Size', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Determines the font size of the table cells', 'pricely-dynamic-pricing-cart-discounts' ),
                'width' => '100%',
                'fields' => $this->admin_utils->get_size_fields( array( 'default_size' => 12, 'default_unit' => 'px' ) ),
            );

            $in_fields[] = array(
                'id' => 'border_style',
                'type' => 'select2',
                'column_title' => esc_html__( 'Border Style', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Determines the border style of the table cells', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => 'solid',
                'options' => array(
                    'none' => esc_html__( 'None', 'pricely-dynamic-pricing-cart-discounts' ),
                    'solid' => esc_html__( 'Solid', 'pricely-dynamic-pricing-cart-discounts' ),
                    'dotted' => esc_html__( 'Dotted', 'pricely-dynamic-pricing-cart-discounts' ),
                    'dashed' => esc_html__( 'Dashed', 'pricely-dynamic-pricing-cart-discounts' )
                ),
                'width' => '100%',
                'column_attributes' => array(
                    'style' => 'min-width:120px;'
                ),
                'fold_id' => 'border_style',
            );

            $in_fields[] = array(
                'id' => 'border_color',
                'type' => 'colorpicker',
                'column_no_size' => true,
                'column_title' => esc_html__( 'Border Color', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Controls the border color of the table cells', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => $this->admin_utils->get_theme_value( 'color_5' ),
                'buton_text' => esc_html__( 'Pick color', 'pricely-dynamic-pricing-cart-discounts' ),
                'width' => '100%',
                'column_attributes' => array(
                    'style' => 'min-width:148px;'
                ),
                'fold' => array(
                    'target' => 'border_style',
                    'attribute' => 'value',
                    'value' => array( 'none' ),
                    'oparator' => 'neq',
                    'clear' => false,
                ),
            );

            $in_fields[] = array(
                'id' => 'border_width',
                'type' => 'group-field',
                'column_width' => '170px',
                'merge_fields' => true,
                'fluid-group' => true,
                'column_title' => esc_html__( 'Border Width', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Determines the border width of the table cells', 'pricely-dynamic-pricing-cart-discounts' ),
                'width' => '100%',
                'fields' => $this->admin_utils->get_size_fields( array( 'default_size' => 1, 'default_unit' => 'px' ) ),
                'fold' => array(
                    'target' => 'border_style',
                    'attribute' => 'value',
                    'value' => array( 'none' ),
                    'oparator' => 'neq',
                    'clear' => false,
                ),
            );

            $in_fields[] = array(
                'id' => 'padding',
                'type' => 'group-field',
                'column_width' => '240px',
                'merge_fields' => true,
                'fluid-group' => true,
                'column_title' => esc_html__( 'Padding', 'pricely-dynamic-pricing-cart-discounts' ),
                'column_classes' => array( 'pricely_box_hint' ),
                'tooltip' => esc_html__( 'Determines the paddings of the table cells', 'pricely-dynamic-pricing-cart-discounts' ),
                'width' => '100%',
                'fields' => $this->admin_utils->get_two_sides_sizes_fields( array( 'default_size' => array( 'top_bottom' => 5, 'left_right' => 8 ), 'placeholder' => '', 'default_unit' => 'px' ) ),
            );

            return $in_fields;
        }

        private function cell_two_fields( $in_fields ) {

            $in_fields[] = array(
                'id' => 'header_text_color',
                'type' => 'colorpicker',
                'column_size' => 1,
                'column_title' => esc_html__( 'Header - Text Color', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Controls the text color of the header cells', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => $this->admin_utils->get_theme_value( 'color_1' ),
                'buton_text' => esc_html__( 'Pick color', 'pricely-dynamic-pricing-cart-discounts' ),
                'width' => '100%',
                'column_attributes' => array(
                    'style' => 'min-width:148px;'
                ),
            );

            $in_fields[] = array(
                'id' => 'header_bg_color',
                'type' => 'colorpicker',
                'column_size' => 1,
                'column_title' => esc_html__( 'Header - Background Color', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Controls the background color of the header cells', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => $this->admin_utils->get_theme_value( 'color_3' ),
                'buton_text' => esc_html__( 'Pick color', 'pricely-dynamic-pricing-cart-discounts' ),
                'width' => '100%',
                'column_attributes' => array(
                    'style' => 'min-width:148px;'
                ),
            );

            $in_fields[] = array(
                'id' => 'data_text_color',
                'type' => 'colorpicker',
                'column_size' => 1,
                'column_title' => esc_html__( 'Data - Text Color', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Controls the text color of the price discount cells', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => $this->admin_utils->get_theme_value( 'color_1' ),
                'buton_text' => esc_html__( 'Pick color', 'pricely-dynamic-pricing-cart-discounts' ),
                'width' => '100%',
                'column_attributes' => array(
                    'style' => 'min-width:148px;'
                ),
            );

            $in_fields[] = array(
                'id' => 'data_bg_color',
                'type' => 'colorpicker',
                'column_size' => 1,
                'column_title' => esc_html__( 'Data - Background Color', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Controls the background color of the price discount cells', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => $this->admin_utils->get_theme_value( 'color_4' ),
                'buton_text' => esc_html__( 'Pick color', 'pricely-dynamic-pricing-cart-discounts' ),
                'width' => '100%',
                'column_attributes' => array(
                    'style' => 'min-width:148px;'
                ),
            );

            $in_fields[] = array(
                'id' => 'data_suffix_color',
                'type' => 'colorpicker',
                'column_size' => 1,
                'column_title' => esc_html__( 'Price Suffix - Color', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Controls the color of the price suffix', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => $this->admin_utils->get_theme_value( 'color_2' ),
                'buton_text' => esc_html__( 'Pick color', 'pricely-dynamic-pricing-cart-discounts' ),
                'width' => '100%',
                'column_attributes' => array(
                    'style' => 'min-width:148px;'
                ),
            );

            return $in_fields;
        }

        private function get_default_ui() {

            return array(
                array(
                    'option_type' => 'ui_default',
                    'ui_id' => '2234343',
                ),
            );
        }

        private function init_settings() {

            $this->admin_utils = Pricely_Admin_Utils::get_instance();
        }

    }

    new Pricely_Admin_Settings_Styles_Price_Table_Settings();
}