<?php
defined('BASEPATH') OR exit('No direct script access allowed');


require_once(APPPATH . "/controllers/Auth.php");
class Ajax extends Auth {

    public function action(){
        if (!empty($this->input->post("action"))){
            switch($this->input->post("action")){
                case "like_video":
                    $this->load->model('CMS_model');
                    if($this->ion_auth->logged_in() && !empty($this->input->post("id")) && $this->CMS_model->isVideo($this->input->post("id"))){
                        $videoID = $this->input->post("id");
                        $user = $this->ion_auth->user()->row();
                        $this->CMS_model->userLikeVideo($user->id,$videoID);

                        echo json_encode(array("success" => true, "video_id" => $videoID, "user_id" => $user));
                    }else{
                        echo json_encode(array("success" => false));
                    }
                    break;

                case "watch_later_video":
                    $this->load->model('CMS_model');
                    if($this->ion_auth->logged_in()  && !empty($this->input->post("id")) && $this->CMS_model->isVideo($this->input->post("id"))){
                        $videoID = $this->input->post("id");
                        $user = $this->ion_auth->user()->row();
                        $this->CMS_model->userWatchLaterVideo($user->id,$videoID);

                        echo json_encode(array("success" => true, "video_id" => $videoID, "user_id" => $user));
                    }else{
                        echo json_encode(array("success" => false));
                    }

                    break;

                case "contact":
                    $this->load->model('CMS_model');
                    $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
                    $this->form_validation->set_rules('name', "Name", 'required|min_length[2]|max_length[12]');
                    $this->form_validation->set_rules('message', "Message", 'required|min_length[5]');

                    if ($this->form_validation->run() == FALSE){
                        echo json_encode(array("success" => FALSE));
                    }else{
                        $this->CMS_model->contactUS($this->input->post("email"),$this->input->post("name"),nl2br($this->input->post("message")));
                        echo json_encode(array("success" => true));
                    }
                    break;

                case "career_request":
                    $this->load->model('CMS_model');
                    $this->form_validation->set_rules('career', 'Career', 'required|min_length[3]|max_length[40]');
                    if ($this->form_validation->run() == FALSE){
                        echo json_encode(array("success" => FALSE, "error" => strip_tags(validation_errors())));
                    }elseif(!$this->ion_auth->logged_in()){
                        echo json_encode(array("success" => FALSE, "error" => "not_logged"));
                    }else{
                        $user = $this->ion_auth->user()->row();
                        $this->CMS_model->requestCareer($this->input->post("career"),$user->id );
                        echo json_encode(array("success" => true));
                    }

                    break;

                case "rate_video":
                    $this->load->model('CMS_model');
                    if($this->ion_auth->logged_in()  && !empty($this->input->post("id"))) {
                        $videoID = $this->input->post("id");
                        $user = $this->ion_auth->user()->row();
                        $rating = $this->input->post("rating");

                        $allowedRates = array("0.5", "1", "1.5", "2", "2.5", "3", "3.5", "4", "4.5", "5");
                        if (in_array($rating, $allowedRates)) {
                            $isRated = $this->CMS_model->userRateVideo($user->id, $videoID, $rating);
                            if($isRated){
                                echo json_encode(array("success" => true));
                                die();
                            }else{
                                echo json_encode(array("success" => false, "error" => "You have already rated this video"));
                                die();
                            }
                        }
                    }
                    echo json_encode(array("success" => false));
                    break;

                case "job_shadow":
                    $this->load->model('CMS_model');
                    if($this->ion_auth->logged_in()) {
//                        $schoolID = $this->input->post("school_id");
//                        $allowedSchools = array_map(function($item){
//                            return $item['id'];
//                        },$this->CMS_model->getSchools());
//                        if(!in_array($schoolID,$allowedSchools)){
//                            echo json_encode(array("success" => false, "error" => "School not found"));
//                            die();
//                        }

                        $videoID = (int) $this->input->post("video_id");
                        $message = $this->input->post("message");

                        $user = $this->ion_auth->user()->row();
                        $applied = $this->CMS_model->addJobShadow($videoID,$user->id,$message);
                        if($applied){
                            echo json_encode(array("success" => true));
                        }elseif(empty($message)){
                            echo json_encode(array("success" => false, "error" => "The message is required"));
                        }else{
                            echo json_encode(array("success" => false, "error" => "You have already applied for this job recently, please try again later."));
                        }
                        die();


                    }

                    break;
            }

        }else{
            echo json_encode(array("success" => false));
        }
    }

}
