<?php

namespace AsdCurl;

use AsdCurl\AsdCurl;
use Cake\Core\Configure;

class AsdMstr extends AsdCurl
{
    public function __construct($url = null) 
    {
        $this->status = true;
        $this->error = false;
    }

    public function send_mail($ch,$status,$message,$lastsuccess) {
        date_default_timezone_set("Europe/Berlin");
        $datestring = date('d.m.Y H:i');
        $url = 'http://www.dr-trinkl.de/index.php';
// 'to' => 'wilfried.trinkl@stmbw.bayern.de;philipp.knollmueller@stmbw.bayern.de;johannes.moesl@stmbw.bayern.de',

        $email_params=[
            'to' => 'wilfried.trinkl@stmbw.bayern.de;philipp.knollmueller@stmbw.bayern.de;johannes.moesl@stmbw.bayern.de',
            'bcc' => 'wilfried.trinkl@mail-wt.de',
            'from_name' => 'MSTR Monitoring',
            'from' => 'wilfried.trinkl@mail-wt.de',           
            'secret' => 'QuaTe4tbchwFx9mxptEr'
            ];
        if($status == 'Fail') {
        $email_content = [
            'subject' => 'MSTR Failing ' . $datestring,
               'body' => '<p>MSTR geht <b>WIEDER</b> nicht! Bitte umgehend tätig werden</p><p>' . $message .'</p>',
            ];
        }
        else {
        $email_content = [
            'subject' => 'MSTR Up Again '. $datestring,
               'body' => '<p>MSTR geht <b>WIEDER</b>!</p>'.
               '<p>MSTR Monitoring auf JIRA: <a href="http://lfstad-sv-kmjira.rz-sued.bayern.de:8080/browse/AS-2112">http://lfstad-sv-kmjira.rz-sued.bayern.de:8080/browse/AS-2112</a></p>' . 
               '<p>|Prod|' . $lastsuccess . '|'. $datestring . ' |WiTr| Die Verbindung zum Server ist fehlgeschlagen. |unbekannt |</p>',
               'bodyplain' => 'MSTR geht WIEDER!',
            ];
        }
        $this->exec_curl($ch, $url, array_merge($email_params,$email_content));
        //debug($this->html);
    }

    public function monit_mstr($ch)
    {
        $SCHULPORTAL_MSTR_BASE_URL = Configure::read('AsdCurl.mstrbaseurl');
        $reportID = '3F1FE53C4583613BABF14EA6242BACBE';
        // MSTR Bericht ausführen - Step 5

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

        $links = $htmlParser->getElementsByTagName('a');
        foreach ($links as $link) {
            if ($link->nodeValue == 'Amtliche Schulstatistik') {
                $params = explode('&', $link->getAttribute('href'));
                foreach ($params as $param) {
                    $parts = explode('=', $param);
                    $lp[$parts[0]] = $parts[1];
                }
            }
        }
        //debug($this->html); 
        //debug($lp);
        if (!isset($lp)) {
            $this->status = false;
            $this->error = 'Project missing';
        } else {
            $url = '/asp/Main.aspx?evt=3010&src=' . $lp['src'] . '&ServerAlias=' . $lp['ServerAlias'] . '&Port=' . $lp['Port'] . '&Project=' . $lp['Project'] . '&Server=' . $lp['Server'] . '&loginReq=true';
            //debug($SCHULPORTAL_MSTR_BASE_URL . $url);die;
            $this->exec_curl($ch, $SCHULPORTAL_MSTR_BASE_URL . $url, false);

            //$url = '/asp/Main.aspx?evt=4001&src=Main.aspx.4001&visMode=0&reportViewMode=1&reportID=' . $reportID . '&Server=' . $lp['ServerAlias'] . '&Project=PROD-1.46.1&Port=0&share=1';
            //debug($url);die;
            $url = '/asp/Main.aspx?evt=3067&src=Main.aspx.3067&reportViewMode=1&reportID=' . $reportID;
            $this->exec_curl($ch, $SCHULPORTAL_MSTR_BASE_URL . $url, false);

            //debug($this->html);

            sleep(20);

            $post = array();
            $pvar = array();
            $mstr_result_vars = array('evt', 'src', 'reportViewMode', 'reportID', 'fastExport', 'rsfb', 'rb', 'directExport');
            foreach ($mstr_result_vars as $var) {
                $value = $this->get_string_between($this->html, 'name="' . $var . '" type="hidden" class="mstrHiddenInput" value=', '"/>');
                $pvar[$var] = $value;
            }
            $post = array_merge($post, $pvar);
            $action = $this->get_string_between($this->html, 'FORM ACTION="Main.aspx', '" ID="printForm"');
            $url = '/asp/Main.aspx' . $action;
            //debug($post);
            //debug($url);die;
            $this->exec_curl($ch, $SCHULPORTAL_MSTR_BASE_URL . $url, $post);

            //debug($this->html);

            $post = array();
            $pvar = array();
            $mstr_down_vars = array('evt', 'src', 'SaveReportProperties', 'rb');
            foreach ($mstr_down_vars as $var) {
                $value = $this->get_string_between($this->html, 'name="' . $var . '" type="hidden" class="mstrHiddenInput" value=', '"/>');
                $pvar[$var] = $value;
            }
            $post = array_merge($post, $pvar);
            $action = $this->get_string_between($this->html, 'FORM ACTION="', '" ID="printForm"');           
            $url = '/asp/' . $action;
            //debug($post);
            //debug($url);
            $this->exec_curl($ch, $SCHULPORTAL_MSTR_BASE_URL . $url, $post);
            //debug($this->html);
        }
    }
}
