<?php

/**
 * @package: EGPS - Easy Sell for Google Photo
 */

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

class EgpsOrders{

    private static $tablename = 'egps_orders';


    public static function getAllOrders($offset, $limit){
        global $wpdb;
        $wpdb->show_errors();

        $table_name = $wpdb->prefix . "egps_orders";

        try{

            $query = $wpdb->prepare(
                "SELECT * FROM $table_name 
                GROUP BY order_invoice_number 
                ORDER BY id 
                DESC LIMIT %d, %d",
                $offset, 
                $limit
            );

            $ret_val = $wpdb->get_results($query);

            return $ret_val;

        }catch (Error $error){
            Egps::egps_vardump($error);
        }
        return null;
    }

    public static function getAllOrdersLast($offset, $limit){
        global $wpdb;
        $wpdb->show_errors();

        $table_name = $wpdb->prefix . "egps_orders";

        try{

            $query = $wpdb->prepare(
                "SELECT customer_email, order_invoice_number, SUM(purchase_price) price, purchase_currency, time 
                FROM $table_name 
                GROUP BY order_invoice_number 
                ORDER BY id DESC 
                LIMIT %d, %d",
                $offset, 
                $limit
            );

            $ret_val = $wpdb->get_results($query);

            return $ret_val;

        }catch (Error $error){
            Egps::egps_vardump($error);
        }
        return null;
    }

    public static function getAllOrdersByCustomer($cust_email, $limit = ""){
        global $wpdb;
        $wpdb->show_errors();

        $table_name = $wpdb->prefix . "egps_orders";

        $limit = ($limit != "") ? $limit : "1";

        try{

            $query = $wpdb->prepare(
                "SELECT * 
                FROM $table_name 
                WHERE customer_email=%s  
                GROUP BY order_invoice_number 
                ORDER BY id DESC LIMIT %d",
                $cust_email,
                $limit
            );

            $ret_val = $wpdb->get_results($query);

            return $ret_val;

        }catch (Error $error){
            Egps::egps_vardump($error);
        }
        return null;
    }

    public static function getAllOrdersByCustomerAdminpage($cust_email){
        global $wpdb;
        $wpdb->show_errors();

        $table_name = $wpdb->prefix . "egps_orders";

        try{

            $query = $wpdb->prepare(
                "SELECT customer_email, order_invoice_number, SUM(purchase_price) price, purchase_currency, time 
                FROM $table_name 
                WHERE customer_email=%s  
                GROUP BY order_invoice_number 
                ORDER BY id DESC",
                $cust_email
            );
            $ret_val = $wpdb->get_results($query);

            return $ret_val;

        }catch (Error $error){
            Egps::egps_vardump($error);
        }
        return null;
    }


    public static function getOrdersByCustomers($offset, $limit){
        global $wpdb;
        $wpdb->show_errors();

        $table_name = $wpdb->prefix . "egps_orders";
        $order = " customer_email ";
        $asc = " ASC ";

        try{

            $query = $wpdb->prepare(
                "SELECT DISTINCT $order 
                FROM $table_name 
                ORDER BY $order $asc 
                LIMIT %d, %d",
                $offset, 
                $limit
            );
            
            $ret_val = $wpdb->get_results($query);

            return $ret_val;

        }catch (Error $error){
            Egps::egps_vardump($error);
        }
        return null;
    }

    public static function getOrdersByCustomersSearch($cust_email){
        global $wpdb;
        $wpdb->show_errors();

        $table_name = $wpdb->prefix . "egps_orders";
        $order = " customer_email ";
        $asc = " ASC ";

        try{

            $query = $wpdb->prepare(
                "SELECT DISTINCT $order 
                FROM $table_name 
                WHERE $order lIKE %s 
                ORDER BY $order $asc",
                '%' . $cust_email . '%'
            );

            $ret_val = $wpdb->get_results($query);

            return $ret_val;

        }catch (Error $error){
            Egps::egps_vardump($error);
        }
        return null;
    }

    public static function searchInvoiceNumber($inv){
        global $wpdb;
        $wpdb->show_errors();

        $table_name = $wpdb->prefix . "egps_orders";

        try{

            $query = $wpdb->prepare(
                " SELECT customer_email, order_invoice_number, SUM(purchase_price) price, purchase_currency, time
                FROM $table_name 
                WHERE order_invoice_number lIKE %s 
                GROUP BY order_invoice_number 
                ORDER BY id DESC",
                '%' . $inv . '%'
            );
            $ret_val = $wpdb->get_results($query);

            return $ret_val;

        }catch (Error $error){
            Egps::egps_vardump($error);
        }
        return null;
    }

    public static function getOrderItemsByInvoice($invoice){
        global $wpdb;
        $wpdb->show_errors();

        $table_name = $wpdb->prefix . "egps_orders";

        try{

            $query = $wpdb->prepare(
                " SELECT id, order_items, order_invoice_number  
                FROM $table_name 
                WHERE order_invoice_number=%s  
                ORDER BY id DESC",
                $invoice
            );

            $ret_val = $wpdb->get_results($query);

            return $ret_val;

        }catch (Error $error){
            Egps::egps_vardump($error);
        }
        return null;
    }

    public static function egps_inc_dwldcount($email, $photoid){
        global $wpdb;
        $wpdb->show_errors();

        $table_name = $wpdb->prefix . "egps_orders";

        try{

            $query = $wpdb->prepare(
                "UPDATE $table_name 
                SET  dwld_count=dwld_count + 1 
                WHERE customer_email=%s AND order_items=%s",
                $email,
                $photoid
            );
            $ret_val = $wpdb->get_results($query);

            return $ret_val;

        }catch (Error $error){
            Egps::egps_vardump($error);
        }
        return null;
    }

    public static function getCustomerAllOrders($email){
        global $wpdb;
        $wpdb->show_errors();

        $table_name = $wpdb->prefix . "egps_orders";

        try{

            $query = $wpdb->prepare(
                "SELECT * 
                FROM $table_name 
                WHERE customer_email=%s 
                ORDER BY id ASC ",
                $email
            );
            $ret_val = $wpdb->get_results($query);

            return $ret_val;

        }catch (Error $error){
            Egps::egps_vardump($error);
        }
        return null;
    }

    public static function getCustomerAllInvoices($email){
        global $wpdb;
        $wpdb->show_errors();

        $table_name = $wpdb->prefix . "egps_orders";

        try{

            $query = $wpdb->prepare(
                "SELECT *
                FROM $table_name 
                WHERE customer_email=%s 
                GROUP BY order_invoice_number 
                ORDER BY id DESC",
                $email
            );
            $ret_val = $wpdb->get_results($query);

            return $ret_val;

        }catch (Error $error){
            Egps::egps_vardump($error);
        }
        return null;
    }

    public static function egps_get_one_photo_by_gid($gid){
        global $wpdb;
        $wpdb->show_errors();

        $ret_val = null;

        $table_name = $wpdb->prefix . "egps_photos";
        $where = " egps_id='" . $gid . "'";
        $limit = ' 1';

        try{

            $url = 'https://photoslibrary.googleapis.com/v1/mediaItems/'.$gid;

            $query_args = [
                'access_token' => EGPS_TOKEN
            ];
            $url = add_query_arg($query_args, $url);

            $response = wp_remote_request($url, ['sslverify' => false]);

            if (!is_wp_error($response)) {

                $imgobj = json_decode($response['body']);

                $imgurl = $imgobj->baseUrl . "=w" . $imgobj->mediaMetadata->width . "-h" . $imgobj->mediaMetadata->height;

            }else{
                //error
                Egps::egps_vardump($response);
            }

            return $imgurl;

        }catch (Error $error){
            Egps::egps_vardump($error);
        }
        return null;
    }


}