<?php

/**
 * Redux Framework is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * any later version.
 * Redux Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with Redux Framework. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package     ReduxFramework
 * @subpackage  Field_button_color
 * @author      ioDSGN
 * @version     1.2.0
 */

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

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

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

        /**
         * 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;

            if ( empty( $this->extension_dir ) ) {
                $this->extension_dir = trailingslashit( str_replace( '\\', '/', dirname( __FILE__ ) ) );
                $this->extension_url = site_url( str_replace( trailingslashit( str_replace( '\\', '/', ABSPATH ) ), '', $this->extension_dir ) );
            }

            $defaults    = array(
                'bg'       => true,
                'bghover'  => true,
                'bgactive' => true,
                'fg'       => true,
                'fghover'  => true,
                'fgactive' => true,
            );
            $this->field = wp_parse_args( $this->field, $defaults );

            $defaults = array(
                'bg'       => '',
                'bghover'  => '',
                'bgactive' => '',
                'fg'       => '',
                'fghover'  => '',
                'fgactive' => '',
            );

            $this->value = wp_parse_args( $this->value, $defaults );

            // In case user passes no default values.
            if ( isset( $this->field['default'] ) ) {
                $this->field['default'] = wp_parse_args( $this->field['default'], $defaults );
            } else {
                $this->field['default'] = $defaults;
            }
        }

        /**
         * 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() {
            if ( $this->field['bg'] === true && $this->field['default']['bg'] !== false ) {
                echo '<span class="buttonColor"><strong>' . __( 'Background', 'dvl-uxshop-extensions' ) . '</strong>&nbsp;<input id="' . $this->field['id'] . '-bg" name="' . $this->field['name'] . $this->field['name_suffix'] . '[bg]' . '" value="' . $this->value['bg'] . '" class="redux-color redux-color-bg redux-color-init ' . $this->field['class'] . '"  type="text" data-default-color="' . $this->field['default']['bg'] . '" /></span>';
            }

            if ( $this->field['fg'] === true && $this->field['default']['fg'] !== false ) {
                echo '<span class="buttonColor"><strong>' . __( 'Text Color', 'dvl-uxshop-extensions' ) . '</strong>&nbsp;<input id="' . $this->field['id'] . '-fg" name="' . $this->field['name'] . $this->field['name_suffix'] . '[fg]' . '" value="' . $this->value['fg'] . '" class="redux-color redux-color-fg redux-color-init ' . $this->field['class'] . '"  type="text" data-default-color="' . $this->field['default']['fg'] . '" /></span>';
            }

            if ( $this->field['bghover'] === true && $this->field['default']['bghover'] !== false ) {
                echo '<span class="buttonColor"><strong>' . __( 'Background (hover)', 'dvl-uxshop-extensions' ) . '</strong>&nbsp;<input id="' . $this->field['id'] . '-bghover" name="' . $this->field['name'] . $this->field['name_suffix'] . '[bghover]' . '" value="' . $this->value['bghover'] . '" class="redux-color redux-color-bghover redux-color-init ' . $this->field['class'] . '"  type="text" data-default-color="' . $this->field['default']['bghover'] . '" /></span>';
            }

            if ( $this->field['fghover'] === true && $this->field['default']['fghover'] !== false ) {
                echo '<span class="buttonColor"><strong>' . __( 'Text Color (hover)', 'dvl-uxshop-extensions' ) . '</strong>&nbsp;<input id="' . $this->field['id'] . '-fghover" name="' . $this->field['name'] . $this->field['name_suffix'] . '[fghover]' . '" value="' . $this->value['fghover'] . '" class="redux-color redux-color-fghover redux-color-init ' . $this->field['class'] . '"  type="text" data-default-color="' . $this->field['default']['fghover'] . '" /></span>';
            }

            if ( $this->field['bgactive'] === true && $this->field['default']['bgactive'] !== false ) {
                echo '<span class="buttonColor"><strong>' . __( 'Background (active)', 'dvl-uxshop-extensions' ) . '</strong>&nbsp;<input id="' . $this->field['id'] . '-bgactive" name="' . $this->field['name'] . $this->field['name_suffix'] . '[bgactive]' . '" value="' . $this->value['bgactive'] . '" class="redux-color redux-color-bgactive redux-color-init ' . $this->field['class'] . '"  type="text" data-default-color="' . $this->field['default']['bgactive'] . '" /></span>';
            }

            if ( $this->field['fgactive'] === true && $this->field['default']['fgactive'] !== false ) {
                echo '<span class="buttonColor"><strong>' . __( 'Text Color (active)', 'dvl-uxshop-extensions' ) . '</strong>&nbsp;<input id="' . $this->field['id'] . '-fgactive" name="' . $this->field['name'] . $this->field['name_suffix'] . '[fgactive]' . '" value="' . $this->value['fgactive'] . '" class="redux-color redux-color-fgactive redux-color-init ' . $this->field['class'] . '"  type="text" data-default-color="' . $this->field['default']['fgactive'] . '" /></span>';
            }

        }

        /**
         * Enqueue Function.
         * If this field requires any scripts, or css define this function and register/enqueue the scripts/css
         *
         * @since       1.0.0
         * @access      public
         * @return      void
         */
        public function enqueue() {
            wp_enqueue_style( 'wp-color-picker' );

            wp_enqueue_script(
                'redux-field-button-color-js',
                $this->extension_url . 'field_button_color.js',
                array( 'jquery', 'wp-color-picker', 'redux-js' ),
                time(),
                true
            );

            if ($this->parent->args['dev_mode']) {
                wp_enqueue_style( 'redux-color-picker-css' );

                wp_enqueue_style(
                    'redux-field-button_color-js',
                    $this->extension_url . 'field_button_color.css',
                    array(),
                    time(),
                    'all'
                );
            }
        }

        public function output() {

            $style = $style_hover = $style_active = array();

            if ( ! empty( $this->value['bg'] ) && $this->field['bg'] === true && $this->field['default']['bg'] !== false ) {
                $style[] = 'background-color:' . $this->value['bg'] . ';';
            }

            if ( ! empty( $this->value['fg'] ) && $this->field['fg'] === true && $this->field['default']['fg'] !== false ) {
                $style[] = 'color:' . $this->value['fg'] . ';';
            }

            if ( ! empty( $this->value['bghover'] ) && $this->field['bghover'] === true && $this->field['default']['bghover'] !== false ) {
                $style_hover[] = 'background-color:' . $this->value['bghover'] . ';';
            }

            if ( ! empty( $this->value['fghover'] ) && $this->field['fghover'] === true && $this->field['default']['fghover'] !== false ) {
                $style_hover[] = 'color:' . $this->value['fghover'] . ';';
            }

            if ( ! empty( $this->value['bgactive'] ) && $this->field['bgactive'] === true && $this->field['default']['bgactive'] !== false ) {
                $style_active[] = 'background-color:' . $this->value['bgactive'] . ';';
            }

            if ( ! empty( $this->value['fgactive'] ) && $this->field['fgactive'] === true && $this->field['default']['fgactive'] !== false ) {
                $style_active[] = 'color:' . $this->value['fgactive'] . ';';
            }

            $style['hover']  = implode( ' ', $style_hover );
            $style['active'] = implode( ' ', $style_active );
            $style['focus']  = implode( ' ', $style_active );

            if ( ! empty( $style ) ) {
                if ( ! empty( $this->field['output'] ) && is_array( $this->field['output'] ) ) {
                    $styleString = "";

                    foreach ( $style as $key => $value ) {
                        if ( is_numeric( $key ) ) {
                            $styleString .= implode( ",", $this->field['output'] ) . "{" . $value . '}';
                        } else {
                            if ( count( $this->field['output'] ) == 1 ) {
                                foreach($this->field['output'] as $sel => $elem) {
                                    continue;
                                }

                                if (strpos($elem, ',') != false) {
                                    $selector_arr = explode(',',$elem);
                                    $sel_list = '';

                                    foreach($selector_arr as $idx => $selector) {
                                        $sel_list .= $selector . ":" . $key . ",";
                                    }

                                    $sel_list = rtrim($sel_list,',');
                                    $styleString .= $sel_list . "{" . $value . '}';
                                } else {
                                    $styleString .= $elem . ":" . $key . "{" . $value . '}';
                                }
                            }
                        }
                    }

                    $this->parent->outputCSS .= $styleString;
                }

                if ( ! empty( $this->field['compiler'] ) && is_array( $this->field['compiler'] ) ) {
                    $styleString = "";

                    foreach ( $style as $key => $value ) {
                        if ( is_numeric( $key ) ) {
                            $styleString .= implode( ",", $this->field['compiler'] ) . "{" . $value . '}';

                        } else {
                            if ( count( $this->field['compiler'] ) == 1 ) {
                                $styleString .= $this->field['compiler'][0] . ":" . $key . "{" . $value . '}';
                            } else {
                                $blah = '';
                                foreach($this->field['compiler'] as $k => $sel) {
                                    $blah .= $sel . ':' . $key . ',';
                                }

                                $blah = substr($blah, 0, strlen($blah) - 1);
                                $styleString .= $blah . '{' . $value . '}';
                            }
                        }
                    }
                    $this->parent->compilerCSS .= $styleString;
                }
            }
        }
    }
}