<?php

/**
 *  Get cache time
 *  @return int
 */
function siga4w_get_cache_time(){
    $options = get_option(SIGA4W_OPTION);
    if (
        isset($options['cache_time']) &&
        $options['cache_time'] !== '' &&
        filter_var($options['cache_time'], FILTER_VALIDATE_INT, ["options" => ["min_range" => 1]])
    ) {
        return (int)$options['cache_time'];
    }
    return SIGA4W_CACHE_TIME;
}

/**
 * Get post view cache time
 * @return int  
 */
function siga4w_get_cache_time_pv(){
    $options = get_option(SIGA4W_OPTION);
    if (
        isset($options['cache_time_pv']) &&
        $options['cache_time_pv'] !== '' &&
        filter_var($options['cache_time_pv'], FILTER_VALIDATE_INT, ["options" => ["min_range" => 1]])
    ) {
        return (int)$options['cache_time_pv'];
    }
    return SIGA4W_CACHE_TIME_PV;
}

/**
 * Get cache expire time by cache name
 * @param string $cache_name
 * @return int  
 */
function siga4w_get_cache_expire($cache_name=''){
    $option_name = '_transient_timeout_' . $cache_name;
    $expire = get_option($option_name);
    if ($expire === false) {
        return 0; // Cache does not exist       
    }else{
        return (int)$expire - time(); // Return remaining seconds until expiration
    }
}

/**
 * Get property_id 
 * @return string
 */
function siga4w_get_property_id(){
    $options = get_option(SIGA4W_OPTION);
    if( isset($options['property_id']) && !empty($options['property_id']) ){
        return $options['property_id'];
    }
    return '';
}

/**
 *  Get GA4 data
 *  @since 1.0.1
 *  @param array $args
 *  @param string $cache_name
 *  @param int $cache_time
 *  @return array|false
*/
function siga4w_get_data( $args=[], $cache_name='', $cache_time=0 ){

    $data = false;

    if( $cache_name !== '' && $cache_time > 0 ){
        $data = get_transient($cache_name);
    }

    if( $data === false && class_exists('SIGA4W_ga4') ){

        $property_id = siga4w_get_property_id();

        if( empty( $property_id ) ){
            return [ 'message' => __('Please set GA4 property ID in settings.', 'sig-ga4-widget') ];
        }

        $ga4 = new SIGA4W_ga4( $property_id );

        if( !empty($args['metrics']) )  $ga4->setMetricsName($args['metrics']);
        
        if( !empty($args['dimensions']) )  $ga4->setDimensionsName( $args['dimensions'] );
        
        if( !empty($args['dateRange']) )  $ga4->setDateRanges($args['dateRange'][0],$args['dateRange'][1]);
        
        if( !empty($args['dimensionFilter']) )  $ga4->setDimensionFilter($args['dimensionFilter']);
        
        if( !empty($args['limit']) )  $ga4->setLimit($args['limit']);

        $data = $ga4->getData();

        if( $cache_name !== '' &&  !isset($data['message']) ) {
            set_transient( $cache_name, $data, $cache_time );
        }

    }

    return $data;
}



/**
 *  Covert time to local time & format
 *  @param string
 *  @param string
 *  @return string
 */
function siga4w_local_time($time='',$format='Y-m-d H:i:s'){

    if( empty($time) ) return;

    $dt = new DateTime();
    $dt->setTimezone(new DateTimeZone( get_option('timezone_string') ));
    $dt->setTimestamp($time);
    return $dt->format($format);
}

/**
 *  custom strip_tags, allow <b><p><div><ul><li><br>
 *  @param string
 */
function siga4w_strip_tags($string='') {

    if( !empty($string) ){
        $string= strip_tags($string,'<i><b><p><div><ul><li><br>');
    }
    return $string;
}


/**
 *  only post: page & custom post type
 */
function siga4w_get_post_types(){

    $post_types = get_post_types([
        'public'   => true,
        ], 'objects');

    $posts = [];
    $exclude = [ 'attachment', 'elementor_library', 'e-landing-page' ];

    foreach ($post_types as $post_type) {
        if( !in_array( $post_type->name, $exclude )) {
            $posts[$post_type->name] = $post_type->labels->singular_name;
        }
    }

    return $posts;
}

/**
 *
 */
function siga4w_day_loop($dateString='1 day',$day=array(),$format='Y-m-d'){

    $dt = new DateTime();

    if(empty($day[0])) $day[0] = $dt->format("Y-m-1");
    if(empty($day[1])) $day[1] = $dt->format("Y-m-t");

    $begin = new DateTime($day[0]);
    $end = new DateTime($day[1]);

    $interval = DateInterval::createFromDateString($dateString);
    $period = new DatePeriod($begin, $interval, $end);

    $days = [];
    foreach ($period as $dt) {
        $days[] =$dt->format($format);
    }

    return $days;
}

/**
 *  check is json
 */

function siga4w_isJson($string) {
   json_decode($string);
   return json_last_error() === JSON_ERROR_NONE;
}

/**
 *  @since 1.0.1
 */
function siga4w_del_all_cache(){
    delete_transient('siga4w_get_today_cache');
    delete_transient('siga4w_get_all_cache');

    delete_transient('sig4w_charts_month');
    delete_transient('sig4w_charts_year');
    delete_transient('sig4w_hot_posts_today');
    delete_transient('sig4w_hot_posts_week');
}

/**
 *  Add log
 */
if (!function_exists('write_log')) {

    function siga4w_log($log) {
        if (true === WP_DEBUG) {
            if (is_array($log) || is_object($log)) {
                error_log(print_r($log, true));
            } else {
                error_log($log);
            }
        }
    }

}


/**
 * get default postview label
 * @return string
 */
function siga4w_get_default_postview_label() {
    
    $options = get_option(SIGA4W_OPTION);
    
    if (isset($options['postview_label']) && !empty($options['postview_label'])) {
        return $options['postview_label'];
    }
    /* translators: %s is post pageviews. */
    return __('Pageviews: %s', 'sig-ga4-widget');
}