<?php
/*
This file it's included by default if the plugin is active.
$unique_identificator = woo commerce order id, ump transaction_id, etc
$ulp_order = new UlpOrder();
# Buy Course:
$payment_details = [ 'unique_identificator' => $unique_identificator, ... ]
$ulp_order->save($user_id, $course_id, $payment_details);
# Make it completed:
$ulp_order->make_completed($unique_identificator);
*/
if (!defined('ABSPATH')) exit();
if (class_exists('UlpOrder')) return;
class UlpOrder{
    private $entity_type = 'ulp_order';
    private $pending_status = 'ulp_pending';
    private $complete_status = 'ulp_complete';
    private $fail_status = 'ulp_fail';
    private $order_meta_object = null;
    public function __construct(){
      require_once ULP_PATH . 'classes/Db/DbUlpOrdersMeta.class.php';
      $this->order_meta_object = new DbUlpOrdersMeta();
    }
    public function save($uid=0, $course_id=0, $order_meta=array()){
        /// insert post
        $post_data = array(
              'ping_status' => 'closed',
              'comment_status' => 'closed',
              'post_title' => 'ULP ORDER @ ' . date('Y-m-d H:i:s', time()) . ' by ' . $uid . ' for course id : ' . $course_id . '.',
              'menu_order' => 0,
              'post_type' => $this->entity_type,
              'post_status' => $this->pending_status,
        );
        $order_id = wp_insert_post($post_data);
        $order_meta ['code'] = $this->_generate_order_code($order_id);
        /// insert order metas
        if ($order_id){
            $order_meta['user_id'] = $uid;
            $order_meta['course_id'] = $course_id;
            if (empty($order_meta['unique_identificator'])){
                $order_meta['unique_identificator'] = $uid . '_' . $course_id . '_' . time();
            }
            $order_meta ['used'] = 0;
            foreach ($order_meta as $meta_key=>$meta_value){
                $this->order_meta_object->save($order_id, $meta_key, $meta_value);
            }
        }
        
        do_action('ulp_new_order', $order_id, $uid, $course_id);

        return $order_id;
    }
    public function delete($uid=0, $course_id=0){
        $order_id = $this->getOrderId($uid, $course_id);
        if ($order_id){
            $this->order_meta_object->deleteAllByOrderId($order_id);
        }
    }
    /**
     * @param int (user id)
     * @param int (course id)
     * @return int ( ulp_order custom post type id ), 0 if not exists
     */
    public function getOrderId($uid=0, $course_id=0){
        global $wpdb;
        $uid = esc_sql($uid);
        $course_id = esc_sql($course_id);
        $table_name = $wpdb->prefix . 'ulp_order_meta';
        $q = "SELECT a.order_id FROM
              	$table_name a
              	INNER JOIN $table_name b
              	ON a.order_id=b.order_id
              	where
              	a.meta_key='user_id'
              	AND
              	a.meta_value=$uid
              	AND
              	b.meta_key='course_id'
              	AND
              	b.meta_value=$course_id;";
        $order_id = $wpdb->get_var($q);
        if ($order_id==FALSE){
            $order_id = 0;
        }
        return $order_id;
    }
    public function getOrderByUniqueIdentificator($unique_identificator=''){
        $unique_identificator = esc_sql($unique_identificator);
        $order_id = $this->order_meta_object->getVar('order_id', " meta_key='unique_identificator' AND meta_value='$unique_identificator' ORDER BY order_id DESC ");
        if ($order_id==FALSE){
            $order_id = 0;
        }
        return $order_id;
    }
    public function getUniqueByOrderId($id=0){
        $id = esc_sql($id);
        return $this->order_meta_object->getVar('meta_value', " meta_key='unique_identificator' AND order_id=$id ");
    }
/*
    public function make_completed($unique_identificator=''){
        $order_id = $this->getOrderByUniqueIdentificator($unique_identificator);
        if ($order_id){
            wp_update_post(array(
                'ID' => $order_id,
                'post_status' => $this->complete_status,
            ));
            do_action('ulp_make_order_complete', $order_id);
        }
    }
    public function make_faild($unique_identificator=''){
        $order_id = $this->getOrderByUniqueIdentificator($unique_identificator);
        if ($order_id){
            wp_update_post(array(
                'ID' => $order_id,
                'post_status' => $this->fail_status,
            ));
        }
    }
*/
    public function modify_status($unique_identificator='', $new_status='', $only_hooks=FALSE){
        $order_id = $this->getOrderByUniqueIdentificator($unique_identificator);
        if ($order_id){
            switch ($new_status){
                case 'pending':
                case 'ulp_pending':
                  $status = $this->pending_status;
                  break;
                case 'complete':
                case 'ulp_complete':
                  $status = $this->complete_status;
                  break;
                case 'fail':
                case 'ulp_fail':
                default:
                  $status = $this->fail_status;
                  break;
            }
            if (empty($only_hooks)){
                wp_update_post([
                      'ID' => $order_id,
                      'post_status' => $status]
                );
            }
            if ($status==$this->complete_status){
                do_action('ulp_make_order_complete', $order_id);
            } else {
                $this->make_order_unused($order_id);
                do_action('ulp_remove_user_from_course', $order_id);
            }

            do_action('ulp_order_update_the_status', $order_id, $status);
        }
    }
    public function got_access($uid=0, $course_id=0){
        $order_id = $this->getOrderId($uid, $course_id);
        if ($order_id){
            $status = DbUlp::get_post_status($order_id);
            if ($status==$this->complete_status){
                return TRUE;
            }
        }
        return FALSE;
    }
    public function user_got_order_unused_for_course($uid=0, $course_id=0){
        global $wpdb;
        $uid = esc_sql($uid);
        $course_id = esc_sql($course_id);
        $q = "
            SELECT a.order_id
            FROM {$wpdb->prefix}ulp_order_meta a
            INNER JOIN {$wpdb->prefix}ulp_order_meta b
            ON a.order_id=b.order_id
            INNER JOIN {$wpdb->prefix}ulp_order_meta c
            ON b.order_id=c.order_id
            INNER JOIN {$wpdb->posts} d
            ON c.order_id=d.ID
            WHERE
            a.meta_key='user_id'
            AND a.meta_value=$uid
            AND b.meta_key='course_id'
            AND b.meta_value=$course_id
            AND c.meta_key='used'
            AND (c.meta_value=0 OR c.meta_value IS NULL)
            AND d.post_status='ulp_complete'
        ";
        // echo $q;
        return $wpdb->get_var($q);
    }
    public function is_order_used($order_id=0){
        global $wpdb;
        $order_id = esc_sql($order_id);
        return $wpdb->get_var("SELECT id FROM {$wpdb->prefix}ulp_order_meta WHERE meta_key='order_used' AND order_id=$order_id AND meta_value=1;");
    }
    public function make_order_used($order_id=0){
        global $wpdb;
        $order_id = esc_sql($order_id);
        return $wpdb->query("UPDATE {$wpdb->prefix}ulp_order_meta SET meta_value=1 WHERE order_id=$order_id AND meta_key='used'; ");
    }
    public function make_order_unused($order_id=0){
        global $wpdb;
        $order_id = esc_sql($order_id);
        return $wpdb->query("UPDATE {$wpdb->prefix}ulp_order_meta SET meta_value=0 WHERE order_id=$order_id AND meta_key='used'; ");
    }
    public function save_service_transaction_details($order_id=0, $value=''){
        global $wpdb;
        $key = '_ulp_service_transaction_details';
        $order_id = esc_sql($order_id);
        $value = esc_sql($value);
        $exists = $wpdb->get_var("SELECT id FROM {$wpdb->prefix}ulp_order_meta WHERE meta_key='$key' AND order_id=$order_id;");
        if ($exists){
            $wpdb->query("UPDATE {$wpdb->prefix}ulp_order_meta SET meta_value='$value' WHERE meta_key='$key' AND order_id=$order_id;");
        } else {
            $wpdb->query("INSERT INTO {$wpdb->prefix}ulp_order_meta VALUES(NULL, $order_id, '$key', '$value'); ");
        }
    }
    private function _generate_order_code($order_id=0){
        $prefix = get_option('ulp_order_prefix_code');
        if (empty($prefix)){
          $prefix = 'ULP';
        }
        while (strlen($order_id)<6){
          $order_id = '0' . $order_id;
        }
        $the_code = $prefix . $order_id;
        return $the_code;
    }
}
