<?php

/**
 * Utility functions for the plugin.
 *
 * @package Flexible_FAQs
 */
namespace WPGO_Plugins\Flexible_Faqs;

/**
 * Plugin utility functions.
 */
class Utility
{
    /**
     * Common root paths/directories.
     *
     * @var $module_roots
     */
    protected  $module_roots ;
    /**
     * Main class constructor.
     *
     * @param Array $module_roots Root plugin path/dir.
     * @param Array $custom_plugin_data Plugin data.
     */
    public function __construct( $module_roots, $custom_plugin_data )
    {
        $this->module_roots = $module_roots;
        $this->custom_plugin_data = $custom_plugin_data;
    }
    
    /**
     * Build attributes for the element
     *
     * @param Array  $class_attribute Array of strings of classes names to assign this element.
     * @param Array  $style_attribute Array of style attributes for inline styling of this element.
     * @param String $title_attribute Title for this element.
     * @return String Attributes for an HTML element.
     */
    public static function build_el_attributes( $class_attribute, $style_attribute, $title_attribute )
    {
        $el_attributes = '';
        
        if ( !empty($class_attribute) ) {
            $el_attributes .= ' class="';
            foreach ( $class_attribute as $key => $value ) {
                $el_attributes .= $value;
            }
            $el_attributes .= '"';
        }
        
        
        if ( !empty($style_attribute) ) {
            $el_attributes .= ' style="';
            foreach ( $style_attribute as $key => $value ) {
                $el_attributes .= $value;
            }
            $el_attributes .= '"';
        }
        
        if ( !empty($title_attribute) ) {
            $el_attributes .= ' title="' . $title_attribute . '"';
        }
        return $el_attributes;
    }
    
    /**
     * Build and return tab HTML. Numbered icon is added via JS.
     *
     * @param Object $plugin_data All the settings and configurations of this plugin.
     * @return String formatted as HTML.
     */
    public function build_settings_tabs_html( $plugin_data )
    {
        if ( FFAQ_FREEMIUS_NAVIGATION === 'menu' ) {
            return '';
        }
        $settings_page_main_url = admin_url() . 'options-general.php?page=' . $this->custom_plugin_data->settings_pages['settings']['slug'];
        $settings_page_new_features_url = admin_url() . 'options-general.php?page=' . $this->custom_plugin_data->settings_pages['new-features']['slug'];
        $settings_page_welcome_url = admin_url() . 'options-general.php?page=' . $this->custom_plugin_data->settings_pages['welcome']['slug'];
        $main_active = ( isset( $_GET['page'] ) && $_GET['page'] === $this->custom_plugin_data->settings_pages['settings']['slug'] ? ' nav-tab-active' : '' );
        $new_features_active = ( isset( $_GET['page'] ) && $_GET['page'] === $this->custom_plugin_data->settings_pages['new-features']['slug'] ? ' nav-tab-active' : '' );
        $welcome_active = ( isset( $_GET['page'] ) && $_GET['page'] === $this->custom_plugin_data->settings_pages['welcome']['slug'] ? ' nav-tab-active' : '' );
        $tabs_list_html = '<h2 class="nav-tab-wrapper"><a href="' . $settings_page_main_url . '" class="nav-tab fs-tab' . $main_active . '">Settings</a><a href="' . $settings_page_new_features_url . '" class="nav-tab fs-tab' . $new_features_active . '">New Features</a><a href="' . $settings_page_welcome_url . '" class="nav-tab fs-tab' . $welcome_active . '">About</a></h2>';
        return $tabs_list_html;
    }
    
    /**
     * Decode and return the JSON encoded string in the form of Object.
     *
     * @param String $data JSON  encoded string.
     * @return Array List of premium new available features.
     */
    public static function filter_and_decode_json( $data )
    {
        $new_features = json_decode( $data );
        
        if ( flexible_faqs_fs()->can_use_premium_code() ) {
            // Remove all entries that are 'free-only'.
            foreach ( $new_features as $key => $new_feature ) {
                if ( 'free' === $new_feature->license ) {
                    unset( $new_features[$key] );
                }
            }
            $new_features = array_values( $new_features );
            // reindex array.
        }
        
        return $new_features;
    }
    
    /**
     * Utilized for converting the custom styled block's border object to CSS string.
     *
     * @param Object  $json_obj JSON encoded object.
     * @param Boolean $border_bottom Parameter for border bottom state.
     * @param Boolean $border_bottom_only Parameter for border bottom state.
     * @param Boolean $border_top_only Parameter for border top state.
     * @param Array   $args Additional arguments passed as an array.
     * @return String CSS property and value formatted as valid CSS statement.
     */
    public static function build_css_from_border_object(
        $json_obj,
        $border_bottom = true,
        $border_bottom_only = false,
        $border_top_only = false,
        $args = array()
    )
    {
        $css_obj = json_decode( $json_obj );
        // If for any reason the parsed JSON string doesn't evaluate to an object (it should) then return no CSS.
        if ( !is_object( $css_obj ) ) {
            return '';
        }
        // If JSON border style is in shorthand and the styles haven't been customised then output the literal value the first value is always assumed to be the border width.
        
        if ( property_exists( $css_obj, 'literal' ) ) {
            $literal_arr = explode( ' ', $css_obj->literal );
            if ( true === $border_bottom_only ) {
                return ( true === $border_bottom ? 'border-bottom: ' . $literal_arr[0] . 'px ' . $literal_arr[1] . ' ' . $literal_arr[2] . ';' : '' );
            }
            if ( true === $border_top_only ) {
                return 'border-top: ' . $literal_arr[0] . 'px ' . $literal_arr[1] . ' ' . $literal_arr[2] . ';';
            }
            return 'border: ' . $literal_arr[0] . 'px ' . $literal_arr[1] . ' ' . $literal_arr[2] . ';';
        } else {
            $border_top = 'border-top: ' . $css_obj->top->width . 'px ' . $css_obj->top->color . ' ' . $css_obj->top->style . ';';
            $border_right = 'border-right: ' . $css_obj->right->width . 'px ' . $css_obj->right->color . ' ' . $css_obj->right->style . ';';
            $border_bottom = ( true === $border_bottom ? 'border-bottom: ' . $css_obj->bottom->width . 'px ' . $css_obj->bottom->color . ' ' . $css_obj->bottom->style . ';' : '' );
            $border_left = 'border-left: ' . $css_obj->left->width . 'px ' . $css_obj->left->color . ' ' . $css_obj->left->style . ';';
        }
        
        if ( true === $border_bottom_only ) {
            return $border_bottom;
        }
        if ( true === $border_top_only ) {
            return $border_top;
        }
        if ( '1' === $css_obj->mode ) {
            return 'border: ' . $css_obj->top->width . 'px ' . $css_obj->top->color . ' ' . $css_obj->top->style;
        }
        if ( '4' === $css_obj->mode ) {
            return $border_top . ' ' . $border_right . ' ' . $border_bottom . ' ' . $border_left;
        }
    }
    
    /**
     * Get active item count for a set of FAQs.
     *
     * Note: FAQ items are defined as 'active' if not set to disabled, and have non-empty content for both Q/A.
     *
     * @param int $faq_post_id The Post ID.
     * @return int FAQ active item count.
     */
    // @todo Not needed anymore?
    public static function get_faq_items_count( $faq_post_id )
    {
        $faq_items_count = wp_cache_get( 'wpgo_active_faq_count' );
        
        if ( false === $faq_items_count || $faq_post_id !== $faq_items_count['post_id'] ) {
            $faq_questions = get_post_meta( $faq_post_id, '_wpgo_faq_cpt_questions', true );
            $faq_answers = get_post_meta( $faq_post_id, '_wpgo_faq_cpt_answers', true );
            // Initialize array with default values.
            $faq_items_count = array(
                'active_count'     => -1,
                'empty_item_index' => array(),
                'post_id'          => $faq_post_id,
            );
            $tmp_faq_count = 0;
            //$tmp_empty_faq_count = 0;
            if ( !empty($faq_questions) ) {
                // Loop through the FAQ items to check for active items.
                foreach ( $faq_questions as $faq_question_index => $faq_question_value ) {
                    //$tmp_q = trim( wp_strip_all_tags( $faq_questions[ $faq_question_index ] ) );
                    //$tmp_a = trim( wp_strip_all_tags( $faq_answers[ $faq_question_index ] ) );
                    // Count FAQ items that have content set in both Q/A content.
                    //if ( empty( $tmp_q ) || empty( $tmp_a ) ) {
                    
                    if ( empty($faq_questions[$faq_question_index]) || empty($faq_answers[$faq_question_index]) ) {
                        $faq_items_count['empty_item_index'][$faq_question_index + 1] = 1;
                        //$tmp_empty_faq_count++;
                        continue;
                    } else {
                        $faq_items_count['empty_item_index'][$faq_question_index + 1] = 0;
                    }
                    
                    $tmp_faq_count++;
                }
            }
            $faq_items_count['active_count'] = $tmp_faq_count;
            wp_cache_set( 'wpgo_active_faq_count', $faq_items_count );
        }
        
        // echo '<pre>';
        // print_r($faq_items_count);
        // echo $faq_question_index . '<br>';
        // echo $faq_question_value . '<br>';
        // print_r($faq_questions);
        // print_r($faq_answers);
        // print_r($faq_items_count);
        // echo '</pre>';
        return $faq_items_count;
    }
    
    /**
     * Get list of fonts that can be used in the block/shortcode.
     *
     * @return Array List of browser and Google fonts.
     */
    public static function get_font_families()
    {
        // Define browser safe fonts array.
        $browser_safe_fonts = array(
            'Arial',
            'Verdana',
            'Helvetica',
            'Tahoma',
            'Trebuchet MS',
            'Times New Roman',
            'Georgia',
            'Garamond',
            'Courier New'
        );
        return apply_filters( 'wpgo_faq_font_filter', $browser_safe_fonts );
    }

}
/* End class definition */