<?php

namespace App\Controller\Component;

use Cake\Controller\Component;
use Screen\Capture;

class ToolsComponent extends Component
{

    public function screen($url)
    {
        //$url = 'http://www.heise.de';
        debug($url);
        $screenCapture = new Capture($url);
        $screenCapture->setClipWidth(1200);
        $screenCapture->setClipHeight(800);
        $screenCapture->setUserAgentString('Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0');
        //$screenCapture->setOptions(array('proxy' => '10.1.248.1:82'));
        $screenCapture->setOptions(array('proxy' => '10.1.248.1:82',
            'cookies-file' => '/tmp/cookies.txt',
            'ssl-client-certificate-file' => '/home/wilfried/phantomjs-2.1.1-linux-x86_64/clientcert.key',
            'ssl-certificates-path' => '/home/wilfried/phantomjs-2.1.1-linux-x86_64/certs/',
            'ignore-ssl-errors' => true,
            'ssl-client-key-passphrase' => '2pRaTkCSrw')); 
        $screenCapture->setImageType('png');
        $path = 'files/screen/test.' . $screenCapture->getImageType()->getFormat();
        $fileLocation = WWW_ROOT . $path;
        debug($fileLocation);
        $screenCapture->save($fileLocation);
        return ($path);
    }

    public function init_curl_params()
    {
        $cert_file = '/var/www/html/ssl_KM_Z2016_asd.trinkl.pem';
        $proxy = '10.1.248.1:82';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_PROXY, $proxy);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0');
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        //curl_setopt($ch, CURLOPT_NOBODY, TRUE); // remove body
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_COOKIESESSION, true);
        curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt');
        curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt');
        curl_setopt($ch, CURLOPT_COOKIE, 'cookiename=cookievalue');
        curl_setopt($ch, CURLOPT_SSLCERT, $cert_file);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        return ($ch);
    }

    public function asd_curl($ch, $post, $header, $url)
    {

        $SCHULPORTAL_ASD_BASE_URL = SCHULPORTAL_BASE0_URL . "/uniquesigc3ba1685059aaf325d44099df697a0a635e79a4ec36ec614a98f3bf4b79d7c043ac3880605b7751df91d11233380ad86/uniquesig0/asd";
        if (!empty($post)) {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
        } else {
            curl_setopt($ch, CURLOPT_POST, 0);
        }

        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

        curl_setopt($ch, CURLOPT_URL, $SCHULPORTAL_ASD_BASE_URL . $url);
        //debug($SCHULPORTAL_ASD_BASE_URL . $url);
        $text = curl_exec($ch);
        return ($text);
    }

    public function asd_basic($text)
    {

        $htmlParser = new \DOMDocument(); //to parse html using DOMDocument
        libxml_use_internal_errors(true); // your HTML gives parser warnings, keep them internal
        $htmlParser->loadHTML($text); //Loaded the html string we took from simple xml
        $htmlParser->preserveWhiteSpace = false;

        //$dienststellentable = $htmlParser->getElementById('mainForm:dienststellenTable');

        $fields = array(
            'schulnummer' => 'mainForm:schulnummer',
            'schule' => 'mainForm:schule',
            'schulart' => 'mainForm:schulstellePlzStrasse');

        foreach ($fields as $key => $field) {
            $output['dienststellentable'][$key] = $htmlParser->getElementById($field)->getAttribute('value');
        }
        $output['bemerkung'] = $htmlParser->getElementById('mainForm:erhebungsinformationskommentar')->textContent;
        return ($output);
    }

    public function asd_lk($text, $schooltype)
    {

        $htmlParser = new \DOMDocument(); //to parse html using DOMDocument
        libxml_use_internal_errors(true); // your HTML gives parser warnings, keep them internal
        $htmlParser->loadHTML($text); //Loaded the html string we took from simple xml
        $htmlParser->preserveWhiteSpace = false;

        //debug($schooltype);

        if ($schooltype == 'gms') {

            //$lk_table = $htmlParser->getElementById('mainForm:resultTable:cfhf');
            $lk_table = $htmlParser->getElementById('mainForm:resultTable:tbtf');
        } else {

            $lk_table = $htmlParser->getElementById('mainForm:resultTable:tbtf');
        }
        $rows = $lk_table->getElementsByTagName('tr');
        $t_heads1 = array('Name', 'LBef', 'AbwDst', 'UPZber', 'UPZSoll', 'StdIst', 'StdSoll', 'Diff');

        $i = 0;
        $teachers = '';
        foreach ($rows as $row) {
            $cols = $row->getElementsByTagName('td');
            $j = 0;
            foreach ($cols as $col) {
                $spans = $col->getElementsByTagName('span');
                if ($spans->length > 1) {
                    $teachers[$i][$t_heads1[$j]] = $spans->item(0)->getAttribute('title');
                } else {
                    $teachers[$i][$t_heads1[$j]] = $spans->item(0)->nodeValue;
                }
                $j++;
            }
            $i++;
        }

        $lk_table = $htmlParser->getElementById('mainForm:resultTable:tbtn');
        $rows = $lk_table->getElementsByTagName('tr');
        $t_heads2 = array('MM', 'MA', 'AZK', 'NT', 'RES', 'PF', 'ABO', 'AN', 'ER', 'ABW', 'FR', 'UNTwiss', 'UNTNwiss', 'EASwiss', 'EASnwiss', 'EASwiss', 'EASNwiss', 'Leer');

        $i = 0;
        foreach ($rows as $row) {
            $cols = $row->getElementsByTagName('td');
            $j = 0;
            foreach ($cols as $col) {
                $spans = $col->getElementsByTagName('span');
                if ($spans->length > 1) {
                    $teachers[$i][$t_heads2[$j]] = $spans->item(0)->getAttribute('title');
                } elseif ($spans->length == 0) {

                } else {
                    $teachers[$i][$t_heads2[$j]] = $spans->item(0)->nodeValue;
                }
                $j++;
            }
            $i++;
        }
        $data['t_heads'] = array_merge($t_heads1, $t_heads2);
        $data['teachers'] = $teachers;
        //debug($data);
        return ($data);
    }

    public function asd_lf($text)
    {
        $htmlParser = new \DOMDocument(); //to parse html using DOMDocument
        libxml_use_internal_errors(true); // your HTML gives parser warnings, keep them internal
        $htmlParser->loadHTML($text); //Loaded the html string we took from simple xml
        $htmlParser->preserveWhiteSpace = false;

        $lf_table = $htmlParser->getElementById('leftTable');
        $rows = $lf_table->getElementsByTagName('tr');

        $i = 0;
        $lehrfach = '';
        foreach ($rows as $row) {
            $atags = $row->getElementsByTagName('a');
            $lehrfach[$i]['Name'] = str_replace(array("\n", "\t", "\r"), '', $atags->item(0)->nodeValue);
            $i++;
        }

        $lf_table = $htmlParser->getElementById('lehrkapazitaetFach');

        $rows = $lf_table->getElementsByTagName('tr');

        $t_headslf = array('PKZ', 'ADB', 'RV', 'LB', 'UPZInd', 'UStdSoll', 'UStdIst', 'EinsatzBes', 'Fach1', 'Std1', 'Fach2', 'Std2', 'Fach3', 'Std3', 'Fach4', 'Std4', 'Weitere');

        $i = 0;

        foreach ($rows as $row) {
            $cols = $row->getElementsByTagName('td');
            $j = 0;
            foreach ($cols as $col) {
                $divs = $col->getElementsByTagName('div');
                $lehrfach[$i][$t_headslf[$j]] = $divs->item(0)->nodeValue;
                $j++;
            }
            $i++;
        }
        $data['t_headslf'] = $t_headslf;
        $data['lehrfach'] = $lehrfach;
        return ($data);
    }

}
