<?php
/**
 * Progress
 * v3.1.0
 * @package Ielts_lms
 * @since   1.0.0
 */

defined( 'ABSPATH' ) || exit;

/**
 * Class Progress
 */
class Progress {

    /**
     * Progress version.
     *
     * @var string
     */
    public $version             = '1.0.0';
    public $session             = [];
    public $db_logic            = [];

    public $api_id              = null;
    public $course_id           = null;
    public $topic_id            = null;
    public $lesson_id           = null;
    public $course_category     = null;
    public $quiz_id             = null;
    public $quiz_category       = null;
    public $session_type        = null;
    public $session_category    = null;
    public $session_status      = null;


    /**
     * Constructor method to initialize the plugin.
     */
    public function __construct($params = []) {
        $params         = (object) $params;
        $this->session  = (object) $params;

    }

    public function check_session(){
        global $wpdb, $table_proqyz_v3_progress_submissions;
		
        $filters = '';
        $student_id         = get_current_user_id();
        $session_category   = isset($this->session->session_category)? $this->session->session_category : null;
        $session_type       = isset($this->session->session_type)? $this->session->type : null;
        $quiz_api_id        = isset($this->session->api_id)? $this->session->api_id : null;
        $quiz_category      = isset($this->session->quiz_category)? $this->session->quiz_category : null;
        $course_category    = isset($this->session->course_category)? $this->session->course_category : null;
        $course_id          = isset($this->session->course_id)? $this->session->course_id : null;
        $lesson_id          = isset($this->session->lesson_id)? $this->session->lesson_id : null;
        $topic_id           = isset($this->session->topic_id)? $this->session->topic_id : null;


        if(!$session_category){ 
            return (object) [
                "status"    => false,
                "res"       => "missing session category"
            ]; 
        }

        if(!$quiz_category){
            return (object) [
                "status"    => false,
                "res"       => "missing quiz category"
            ];
        }

        if(!$quiz_api_id) {
            return (object) [
                "status"    => false,
                "res"       => "missing api id"
            ];
        }

        # main data
        $this->quiz_category    = $quiz_category;
        $this->api_id           = $quiz_api_id;
        
        $session_type = 'fresh';
        $query = '';
        if( $session_category == "single" ) {
            $query = "SELECT `ID` FROM `$table_proqyz_v3_progress_submissions` WHERE 
                            `user_id` = '$student_id' AND 
                            `quiz_category` = '$quiz_category' AND 
                            `session_category` = '$session_category' AND 
                            `session_type` = '$session_type' AND
                            `api_id`  = '$quiz_api_id' AND
                            `session_status` = 1 AND
                            `status` = 1 LIMIT 1   
            ";
        } else if( $session_category == "course" ) {
            if( $course_category == null ){
                return (object) [
                    "status"    => false,
                    "res"       => "missing course category"
                ];
            }

            if( $course_id == null ) {
                return (object) [
                    "status"    => false,
                    "res"       => "missing course id"
                ];
            } else {
                $this->course_id        =   $course_id;
                $this->course_category  =   $course_category;
            }

            $filters .= " `course_category` = '$course_category' AND `course_id` = '$course_id' AND ";
            if( $lesson_id !== null ) { 
                $filters .= " `lesson_id` = '$lesson_id' AND "; 
                $this->lesson_id = $lesson_id;
            }

            if( $topic_id !== null ) { 
                $filters .= " `topic_id` = '$topic_id' AND "; 
                $this->topic_id = $topic_id;
            }

            $query = "SELECT `ID` FROM `$table_proqyz_v3_progress_submissions` WHERE 
                            `user_id` = '$student_id' AND 
                            `quiz_category` = '$quiz_category' AND 
                            `session_category` = '$session_category' AND 
                            `session_type` = '$session_type' AND
                            $filters
                            `api_id`  = '$quiz_api_id' AND
                            `session_status` = 1 AND
                            `status` = 1 LIMIT 1   
            ";

        } else if ( $session_category == "fullmock-test" ) {
            
            if( $course_category == null ){
                return (object) [
                    "status"    => false,
                    "res"       => "missing course category"
                ];
            }

            if( $course_id == null ) {
                return (object) [
                    "status"    => false,
                    "res"       => "missing course id"
                ];
            } else {
                $this->course_id        =   $course_id;
                $this->course_category  =   $course_category;
            }

            $filters .= " `course_category` = '$course_category' AND `course_id` = '$course_id' AND ";
            if( $lesson_id !== null ) { 
                $filters .= " `lesson_id` = '$lesson_id' AND "; 
                $this->lesson_id = $lesson_id;
            } else {
                return (object) [
                    "status"    => false,
                    "res"       => "missing lesson id"
                ];
            }
            
            $query = "SELECT `ID` FROM `$table_proqyz_v3_progress_submissions` WHERE 
                            `user_id` = '$student_id' AND 
                            `quiz_category` = '$quiz_category' AND 
                            `session_category` = '$session_category' AND 
                            `session_type` = '$session_type' AND
                            $filters
                            `api_id`  = '$quiz_api_id' AND
                            `session_status` = 1 AND
                            `status` = 1 LIMIT 1   
            ";
        }

        if($query !== ''){
            # for fresh session check completed or not
            $result = $wpdb->get_row($query);
            if(!$result){
                # no result found for fresh session
                # check for resume
                $query = "SELECT `ID` FROM `$table_proqyz_v3_progress_submissions` WHERE 
                            `user_id` = '$student_id' AND
                            `quiz_category` = '$quiz_category' AND 
                            `session_category` = '$session_category' AND 
                            `session_type` = '$session_type' AND
                            $filters
                            `api_id`  = '$quiz_api_id' AND
                            `session_status` = 3 AND
                            `status` = 1 LIMIT 1   
                ";

                $resume_available = $wpdb->get_row($query);
                if(!$resume_available) {
                    # resume is not avaible , which mean fresh start - insert
                    $logic = "insert";
                    $this->db_logic = (object) [
                        "status"            => true,
                        "logic"             => $logic,
                        "session_type"      => $session_type,
                        "session_category"  => $session_category
                    ];
                    $this->session_type         = $session_type;
                    $this->session_category     = $session_category;
                    $this->session_status       = 3;  
                } else {
                    # resume avaible, resume & fresh - update
                    $resume_id = $resume_available->ID;
                    $logic = "update";
                    $this->db_logic = (object) [
                        "status"            => true,
                        "logic"             => $logic,
                        "session_type"      => $session_type,
                        "session_category"  => $session_category,
                        "resume_id"         => $resume_id
                    ];
                    $this->session_type         = $session_type;
                    $this->session_category     = $session_category;
                    $this->session_status       = 3;  
                }

            } else {
                # result found with fresh session
                $session_type = 'retake';
                # do the retake logic
                # check any retake session avaible and completed
                $query = "SELECT `ID` FROM `$table_proqyz_v3_progress_submissions` WHERE 
                            `user_id` = '$student_id' AND 
                            `quiz_category` = '$quiz_category' AND 
                            `session_category` = '$session_category' AND 
                            `session_type` = '$session_type' AND
                            $filters
                            `api_id`  = '$quiz_api_id' AND
                            `session_status` = 1 AND
                            `status` = 1 LIMIT 1   
                ";

                // logic: 2.1.0.1
                // count total retakes attempted 
                $total_retakes_attempted = $wpdb->get_var("SELECT COUNT(*) FROM `$table_proqyz_v3_progress_submissions` WHERE `user_id` = '$student_id' AND `quiz_category` = '$quiz_category' AND `session_category` = '$session_category' AND `session_type` = '$session_type' AND $filters `api_id`  = '$quiz_api_id' AND `session_status` = 1 AND `status` = 1");
                if(defined('ST_PROQYZ_MAX_RETAKES')){
                    if( $total_retakes_attempted >= ST_PROQYZ_MAX_RETAKES ) {
                        // how page 
                        $url_course = '';
                        if( $session_category == "course" ) {
                            $url_course = st_proqyz_get_course_url($course_id);
                        }
                        if(
                            isset($_POST['action']) && 
                            ($_POST['action'] == 'proqyz-save-quiz-progress' || $_POST['action'] == 'proqyz-save-speaking-progress')
                        ){
                            wp_send_json([
                                "success" => false,
                                "res"     => "Retake limit reached",
                                "url"     => $url_course,
                                "reload"  => true
                            ]);
                        }


                        require_once ST_PROQYZ_TEMPLATE_DIR . '/errors/403.php';
                        exit;
                    }
                }

                $check_retake_completed = $wpdb->get_row($query);
                if( $check_retake_completed ) {
                    # retake is already completed get fresh retake
                    # check if retake is in progress or resume state
                    $query = "SELECT `ID` FROM `$table_proqyz_v3_progress_submissions` WHERE 
                            `user_id` = '$student_id' AND 
                            `quiz_category` = '$quiz_category' AND 
                            `session_category` = '$session_category' AND 
                            `session_type` = '$session_type' AND
                            $filters
                            `api_id`  = '$quiz_api_id' AND
                            `session_status` = 3 AND
                            `status` = 1 LIMIT 1   
                    ";

                    $check_retake_resume = $wpdb->get_row($query);
                    if( $check_retake_resume ) {
                        # retake resume avaible return the id
                        $logic = "update";
                        $resume_id = $check_retake_resume->ID;
                        $this->db_logic = (object) [
                            "status"            => true,
                            "logic"             => $logic,
                            "session_type"      => $session_type,
                            "session_category"  => $session_category,
                            "resume_id"         => $resume_id
                        ];
                        $this->session_type         = $session_type;
                        $this->session_category     = $session_category;
                        $this->session_status       = 3;  
                    } else {
                        # no retake resume session available
                        # logic : insert
                        $logic = "insert";
                        $this->db_logic = (object) [
                            "status"            => true,
                            "logic"             => $logic,
                            "session_type"      => $session_type,
                            "session_category"  => $session_category
                        ];
                        $this->session_type         = $session_type;
                        $this->session_category     = $session_category;
                        $this->session_status       = 3;  
                    }
                } else {
                    # if retake is not yet completed, get resume or fresh
                    # check if retake is in progress or resume state
                    $query = "SELECT `ID` FROM `$table_proqyz_v3_progress_submissions` WHERE 
                            `user_id` = '$student_id' AND 
                            `quiz_category` = '$quiz_category' AND 
                            `session_category` = '$session_category' AND 
                            `session_type` = '$session_type' AND
                            $filters
                            `api_id`  = '$quiz_api_id' AND
                            `session_status` = 3 AND
                            `status` = 1 LIMIT 1   
                    ";

                    $check_retake_resume = $wpdb->get_row($query);
                    if( $check_retake_resume ) {
                        # retake resume avaible return the id
                        $logic = "update";
                        $resume_id = $check_retake_resume->ID;
                        $this->db_logic = (object) [
                            "status"            => true,
                            "logic"             => $logic,
                            "session_type"      => $session_type,
                            "session_category"  => $session_category,
                            "resume_id"         => $resume_id
                        ];

                        $this->session_type         = $session_type;
                        $this->session_category     = $session_category;
                        $this->session_status       = 3;  

                    } else {
                        # no retake resume session available
                        # logic : insert
                        $logic = "insert";
                        $this->session_type         = $session_type;
                        $this->session_category     = $session_category;
                        $this->session_status       = 3;          

                        $this->db_logic = (object) [
                            "status"            => true,
                            "logic"             => $logic,
                            "session_type"      => $session_type,
                            "session_category"  => $session_category
                        ];
                    }
                }
            }
        }

        return $this->db_logic; 


    }

    public function get_session(){
        return (object) [
            "api_id"                => $this->api_id,
            "course_id"             => $this->course_id,
            "topic_id"              => $this->topic_id,
            "lesson_id"             => $this->lesson_id,
            "course_category"       => $this->course_category,
            "quiz_category"         => $this->quiz_category,
            "session_type"          => $this->session_type,
            "session_category"      => $this->session_category,
            "session_status"        => $this->session_status    
        ];
    }

    public function includes(){
        
    }
}


