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

class Webinar extends MY_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		if (! $this->uri->segment(3)) {
			echo "<h1>Access Denied</h1>";
			exit;
		}
	}

	public function start()
	{
		if (! $this->uri->segment(3)) {
			echo "<h1>Access Denied</h1>";
			exit;
		}
		$access_token = $this->uri->segment(3);
		$this->load->model('Applicant_model', 'applicant');
		$applicant = $this->applicant->token_search($access_token);
		$this->session->set_userdata('applicant', $applicant);
		if (! $applicant) {
			echo "<h1>Invalid access token</h1>";
			exit;
		}
		$this->load->model('User_model', 'user');
		$user = $this->user->one($applicant['user_id']);
		$this->load->model('Webinar_model', 'webinar');
		$webinar = $this->webinar->one($applicant['webinar_id']);
		$speaker = $this->user->one($webinar['speaker_id']);
		$this->load->model('Chat_model', 'chat');
		$chat = $this->chat->all($webinar['id']);
		$this->load->view('webinar/index', [
			'base_url' => base_url(),
			'page_title' => 'NLP Academy - Webinar page - ' . $webinar['title'],
			'vendors' => [
				'css' => [
				],
				'js' => [
					'https://player.vimeo.com/api/player.js',
				],
			],
			'class' => $this->router->class,
			'module' => '',
			'webinar' => [
				'webinar_token' => $webinar['webinar_token'],
				'title' => $webinar['title'],
				'date' => $applicant['application_date'],
				'vimeo_id' => $webinar['vimeo_id'],
				'applicant' => $user,
				'speaker' => $speaker,
				'num_of_applicants' => $webinar['num_of_applicants'],
				'chat_history' => $chat,
			],
		]);
	}
}
