<?php

// Exit if accessed directly
if ( !defined ( 'ABSPATH' ) ) {
    exit;
}

// Don't duplicate me!
if ( !class_exists ( 'ReduxFramework_intense_icon' ) ) {

    /**
     * Main ReduxFramework_intense_icon class
     *
     * @since       1.0.0
     */
    class ReduxFramework_intense_icon {

        /**
         * Field Constructor.
         * Required - must call the parent constructor, then assign field and value to vars, and obviously call the render field function
         *
         * @since         1.0.0
         * @access        public
         * @return        void
         */
        function __construct ( $field = array(), $value = '', $parent ) {
            $this->parent = $parent;
            $this->field = $field;
            $this->value = $value;
        }

        /**
         * Field Render Function.
         * Takes the vars and outputs the HTML for the field in the settings
         *
         * @since         1.0.0
         * @access        public
         * @return        void
         */
        public function render () {
            $ajax_nonce = wp_create_nonce( "intense-plugin-noonce" );

            $packs = Intense_Icon::get_icon_sources();
            $option_packs = array();

            foreach ($packs as $key => $value) {
                $option_packs[ $key ] = $value['title'];
            }           
            
            if ( ! empty( $this->field['width'] ) ) {
                $width = ' style="' . $this->field['width'] . '"';
            } else {
                $width = ' style="width: 100%;"';
            }

            $nameBrackets = "";
            if ( ! empty( $multi ) ) {
                $nameBrackets = "[]";
            }

            $placeholder = ( isset( $this->field['placeholder'] ) ) ? esc_attr( $this->field['placeholder'] ) : __( 'Select an item', 'redux-framework' );

            if ( isset( $this->field['select2'] ) ) { // if there are any let's pass them to js
                $select2_params = json_encode( $this->field['select2'] );
                $select2_params = htmlspecialchars( $select2_params, ENT_QUOTES );

                echo '<input type="hidden" class="select2_params" value="' . $select2_params . '">';
            }

            $sortable = ( isset( $this->field['sortable'] ) && $this->field['sortable'] ) ? ' select2-sortable"' : "";
            echo '<div class="intense_icon_wrapper">';            
            echo '<div style="width: 30%; display: inline-block; padding-right: 5px;">';
            echo '<label style="  display: block;  font-size: smaller;  color: #888;">' . __( 'Icon Source', 'intense' ) . '</label>';
            echo '<select id="' . $this->field['id'] . '-intense-icon-source" data-nonce="' . $ajax_nonce . '" data-placeholder="' . $placeholder . '" name="' . $this->field['name'] . $this->field['name_suffix'] . $nameBrackets . '[source]" class="redux-intense-icon-source-item ' . $this->field['class'] . $sortable . '"' . $width . ' rows="6">';
            echo '<option></option>';

            foreach ( $option_packs as $k => $v ) {

                if (is_array($v)) {
                    echo '<optgroup label="' . $k . '">';

                    foreach($v as $opt => $val) {
                        $this->make_option($opt, $val, $k);
                    }

                    echo '</optgroup>';

                    continue;
                }

                $this->make_option($k, $v);
            }

            echo '</select>';
            echo '</div>';
            echo '<div style="width: 65%; display: inline-block;">';
            echo '<label style="display: block;  font-size: smaller;  color: #888;">' . __( 'Icon Type', 'intense' ) . '</label>';
            echo '<select id="' . $this->field['id'] . '-intense-icon-type" name="' . $this->field['name'] . $this->field['name_suffix'] . $nameBrackets . '[type]" class="redux-intense-icon-type-item' . ( !empty( $this->value['type'] ) ? ' __' . $this->value['type'] : '') . '">';
            echo '<option value="' . $this->value['type'] . '" selected></option>';
            echo '</select>';
            echo '</div>';
            echo '</div>';
        }

        private function make_option($id, $value, $group_name = '') {
            if ( is_array( $this->value ) ) {
                $selected = ( is_array( $this->value ) && in_array( $id, $this->value ) ) ? ' selected="selected"' : '';
            } else {
                $selected = selected( $this->value, $id, false );
            }

            echo '<option value="' . $id . '"' . $selected . '>' . $value . '</option>';
        }

        /**
         * Enqueue Function.
         * If this field requires any scripts, or css define this function and register/enqueue the scripts/css
         *
         * @since ReduxFramework 1.0.0
         */
        public function enqueue() {
            wp_enqueue_script(
                'redux-field-intense-icon-js',
                //ReduxFramework::$_url . 'inc/fields/select/field_select' . Redux_Functions::isMin() . '.js',
                INTENSE_PLUGIN_URL . '/inc/options/redux/intense_icon/field_intense_icon.js',
                array( 'jquery', 'selectize', 'redux-js' ),
                time(),
                true
            );

            // if ($this->parent->args['dev_mode']) {
            //     wp_enqueue_style(
            //         'redux-field-select-css',
            //         ReduxFramework::$_url . 'inc/fields/select/field_select.css',
            //         array(),
            //         time(),
            //         'all'
            //     );
            // }
        } //function
    }
}