<?php

/**
 * Apply Midnight Discount
 * 
 * @since 1.0.0
 */
if ( !function_exists( 'mdwc_is_deal_valid' ) ) {
    function mdwc_is_deal_valid(  $product_id  ) {
        $midnight_deals = get_post_meta( $product_id, 'mdwc_midnight_deals', true );
        $midnight_deals = ( $midnight_deals == 'yes' ? true : false );
        $start_time = get_post_meta( $product_id, 'mdwc_start_time', true );
        $start_time = ( $start_time ? strtotime( $start_time ) : false );
        // If sandbox is enabled, start showing the deals.
        if ( mdwc_is_sandbox() ) {
            $start_time = time();
        }
        $end_time = get_post_meta( $product_id, 'mdwc_end_time', true );
        $end_time = ( $end_time ? strtotime( $end_time ) : false );
        $current_time = strtotime( current_time( 'h:i A' ) );
        if ( $midnight_deals && $start_time && $end_time && $start_time <= $current_time && $end_time >= $current_time ) {
            return true;
        }
        return false;
    }

}
/**
 * Get visitors IP Address
 * 
 * @since 1.1.0
 */
if ( !function_exists( 'mdwc_get_ip_address' ) ) {
    function mdwc_get_ip_address() {
        $ip_address = false;
        if ( !empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
            $ip_address = $_SERVER['HTTP_CLIENT_IP'];
        } elseif ( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
            $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else {
            $ip_address = $_SERVER['REMOTE_ADDR'];
        }
        return $ip_address;
    }

}
/**
 * Insert User Visit
 * 
 * @param string $visitor_ip
 * 
 * @since 1.1.0
 */
if ( !function_exists( 'mdwc_insert_user_visit' ) ) {
    function mdwc_insert_user_visit(  $visitor_ip  ) {
        global $wpdb;
        $table_name = $wpdb->prefix . 'mdwc_visitors';
        $visitor_id = false;
        $visitor_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$table_name} WHERE visitor_ip = %s AND created_at >= CURDATE()", $visitor_ip ) );
        if ( !$visitor_id ) {
            $wpdb->insert( $table_name, array(
                'visitor_ip' => $visitor_ip,
                'created_at' => current_time( 'mysql' ),
            ), array('%s') );
            $visitor_id = $wpdb->insert_id;
        }
        return $visitor_id;
    }

}
/**
 * Save Settings
 * 
 * @param array $settings
 * 
 * @since 1.1.0
 */
if ( !function_exists( 'mdwc_save_settings' ) ) {
    function mdwc_save_settings(  $settings  ) {
        update_option( 'mdwc_settings', $settings );
        return true;
    }

}
/**
 * Get Settings
 * 
 * @since 1.1.0
 */
if ( !function_exists( 'mdwc_get_settings' ) ) {
    function mdwc_get_settings() {
        $settings = get_option( 'mdwc_settings' );
        return $settings;
    }

}
/**
 * Is Sandbox
 * 
 * @since 1.1.0
 */
if ( !function_exists( 'mdwc_is_sandbox' ) ) {
    function mdwc_is_sandbox() {
        $settings = mdwc_get_settings();
        $is_sandbox = ( $settings && isset( $settings['is_sandbox'] ) && $settings['is_sandbox'] === 'yes' ? true : false );
        return $is_sandbox;
    }

}
/**
 * Get Stats
 * 
 * @since 1.1.0
 */
if ( !function_exists( 'mdwc_get_stats' ) ) {
    function mdwc_get_stats(  $midnight = false  ) {
        global $wpdb;
        $visitor_table = $wpdb->prefix . 'mdwc_visitors';
        $insights_table = $wpdb->prefix . 'mdwc_insights';
        $stats_query = "SELECT COUNT(*) FROM {$visitor_table} WHERE created_at >= CURDATE()";
        $impressions_query = "SELECT COUNT(*) FROM {$insights_table} WHERE created_at >= CURDATE() AND event = 'impression'";
        $clicks_query = "SELECT COUNT(*) FROM {$insights_table} WHERE created_at >= CURDATE() AND event = 'click'";
        $sales_query = "SELECT COUNT(*) FROM {$insights_table} WHERE created_at >= CURDATE() AND event = 'sale'";
        if ( $midnight ) {
            $impressions_query .= " AND is_midnight = 1";
            $clicks_query .= " AND is_midnight = 1";
            $sales_query .= " AND is_midnight = 1";
        }
        $visitors = $wpdb->get_var( $stats_query );
        $impressions = $wpdb->get_var( $impressions_query );
        $clicks = $wpdb->get_var( $clicks_query );
        $sales = $wpdb->get_var( $sales_query );
        return array(
            'visitors'    => $visitors,
            'impressions' => $impressions,
            'clicks'      => $clicks,
            'sales'       => $sales,
        );
    }

}
/**
 * Get Visitor ID
 * 
 * @param string $visitor_ip
 * 
 * @since 1.1.0
 */
if ( !function_exists( 'mdwc_get_visitor_id' ) ) {
    function mdwc_get_visitor_id(  $visitor_ip = ''  ) {
        $visitor_ip = ( empty( $visitor_ip ) ? mdwc_get_ip_address() : $visitor_ip );
        // Check if the visitor already has a unique ID.
        if ( !isset( $_COOKIE['mdwc_visitor_id'] ) ) {
            $visitor_id = mdwc_insert_user_visit( $visitor_ip );
            setcookie(
                'mdwc_visitor_id',
                $visitor_id,
                strtotime( current_time( 'today 11:59 PM' ) ),
                '/'
            );
        } else {
            $visitor_id = $_COOKIE['mdwc_visitor_id'];
        }
        return $visitor_id;
    }

}
/**
 * Insert Event
 * 
 * @param string $product_id
 * @param string $event
 * @param string $is_midnight
 * @param string $price
 * @param string $visitor_id
 * 
 * @since 1.1.0
 */
if ( !function_exists( 'mdwc_insert_event' ) ) {
    function mdwc_insert_event(
        $product_id,
        $event,
        $is_midnight,
        $price = '',
        $visitor_id = ''
    ) {
        $visitor_id = ( empty( $visitor_id ) ? mdwc_get_visitor_id() : $visitor_id );
        global $wpdb;
        $table_name = $wpdb->prefix . 'mdwc_insights';
        $exists = false;
        // Check if not already exists with same ip and same product id
        if ( $event !== 'sale' ) {
            $exists = $wpdb->get_var( $wpdb->prepare(
                "SELECT id FROM {$table_name} WHERE product_id = %d AND visitor_id = %d AND event = %s AND created_at >= CURDATE()",
                $product_id,
                $visitor_id,
                $event
            ) );
        }
        if ( !$exists ) {
            return $wpdb->insert( $table_name, array(
                'product_id'  => $product_id,
                'visitor_id'  => $visitor_id,
                'event'       => $event,
                'is_midnight' => $is_midnight,
                'price'       => $price,
                'created_at'  => current_time( 'mysql' ),
            ), array(
                '%d',
                '%d',
                '%s',
                '%s',
                '%s',
                '%s'
            ) );
        }
        return false;
    }

}