<?php

use Spipu\Html2Pdf\Html2Pdf;
use Spipu\Html2Pdf\Exception\Html2PdfException;
use Spipu\Html2Pdf\Exception\ExceptionFormatter;

class Pdfmaster extends MY_Controller
{

    public $imglogo;

    /**
     * Get All Data from this method.
     *
     * @return Response
     */
    public function __construct()
    {
        parent::__construct();
        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
        header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
        $this->imglogo = base_url("static/images/logo.png");
        $this->load->database();
    }

    public function exercise_get()
    {
        $isios = $this->input->get('isios');
        $exercisename = $this->input->get('exercisename');
        $exercisedate = $this->input->get('exercisedate');
        $coursefullname = $this->input->get('coursefullname');
        $questionids = $this->input->get('questionids');
        $showcorrect = $this->input->get('showcorrect');
        $this->load->model('Question_model', 'questions_model');
        $questions = $this->questions_model->getByIds($questionids);
        // create new PDF document
        //public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8', $diskcache=false, $pdfa=false)
        try {
            ob_start();
            $date = date('Y-m-d');
            $imglogo = $this->imglogo;
            $includepath = $isios !== 'false' ? APPPATH . 'views/html2pdf/example_exercise_ios.php' : APPPATH . 'views/html2pdf/example_exercise.php';
            include $includepath;
            $content = ob_get_clean();

            $html2pdf = new Html2Pdf('P', 'A4', 'cn');
            $html2pdf->setDefaultFont('stsongstdlight');
            $html2pdf->writeHTML($content);
            $filename = $this->GUID() . '.pdf';
            $filepath = UPLOADSPATH . "exercises" . DS . $filename;
            $html2pdf->output($filepath, 'F');
        } catch (Html2PdfException $e) {
            $html2pdf->clean();

            $formatter = new ExceptionFormatter($e);
            echo $formatter->getHtmlMessage();
        }

        $ret = array('filename' => $filename, 'success' => true);
        $this->response($ret, MY_Controller::HTTP_OK);
    }

    public function question_get()
    {
        $isios = $this->input->get('isios');
        $questionids = $this->input->get('questionids');
        $showcorrect = $this->input->get('showcorrect');
        $this->load->model('Question_model', 'questions_model');
        $questions = $this->questions_model->getByIds($questionids);

        try {
            ob_start();
            $date = date('Y-m-d');
            $imglogo = $this->imglogo;
            //log_message('debug','isios:'.$isios);
            $includepath = $isios !== 'false' ? APPPATH . 'views/html2pdf/example_question_ios.php' : APPPATH . 'views/html2pdf/example_question.php';
            include $includepath;
            $content = ob_get_clean();

            $html2pdf = new Html2Pdf('P', 'A4', 'cn');
            $html2pdf->setDefaultFont('stsongstdlight');
            $html2pdf->writeHTML($content);
            $filename = $this->GUID() . '.pdf';
            $filepath = UPLOADSPATH . "questions" . DS . $filename;
            $html2pdf->output($filepath, 'F');
        } catch (Html2PdfException $e) {
            $html2pdf->clean();

            $formatter = new ExceptionFormatter($e);
            echo $formatter->getHtmlMessage();
        }

        $ret = array('filename' => $filename, 'success' => true);
        $this->response($ret, MY_Controller::HTTP_OK);
    }

    public function note_get()
    {
        $noteids = $this->input->get('noteids');
        $this->load->model('notes', 'notes_model');
        $notes = $this->notes_model->getByIds($noteids);

        try {
            ob_start();
            $date = date('Y-m-d');
            $imglogo = $this->imglogo;
            $guid = $this->GUID();
            $uploadpath = UPLOADSPATH . DS . "notes" . DS;
            include APPPATH . 'views/html2pdf/example_note.php';
            $content = ob_get_clean();

            $html2pdf = new Html2Pdf('P', 'A4', 'cn');
            $html2pdf->setDefaultFont('stsongstdlight');
            $html2pdf->writeHTML($content);
            $filename = $this->GUID() . '.pdf';
            $filepath =  UPLOADSPATH . DS . "notes" . DS . $filename;
            $html2pdf->output($filepath, 'F');
        } catch (Html2PdfException $e) {
            $html2pdf->clean();

            $formatter = new ExceptionFormatter($e);
            echo $formatter->getHtmlMessage();
        }

        $ret = array('filename' => $filename, 'success' => true);
        $this->response($ret, MY_Controller::HTTP_OK);
    }

    function GUID()
    {
        if (function_exists('com_create_guid') === true) {
            return trim(com_create_guid(), '{}');
        }

        return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
    }
}
