<?php

namespace App\Controller;

use App\Controller\AppController;
use AsdCurl\AsdMstr;
use AsdCurl\AsdUs;
use Cake\I18n\Time;
use Cake\Filesystem\File;

class CurlsController extends AppController
{
    //public $uses = array('Monitoring');

    public function monitmstr()
    {
        $this->loadModel('Monitorings');
        $curl = new AsdMstr();
        $ch = $curl->init_curl_params();
        $curl->login_school($ch);
        $curl->login_mstr($ch);
        $data['data'] = '';
        $result = '';  
        if(!($curl->status)) {
            $curl->message .= 'Login failed! ' . $curl->error;               
        }
        else {
            $curl->monit_mstr($ch);
            if(!($curl->status)) {
                $curl->message .= 'Report failed: ' . $curl->error;
            }
            else {
                $data['data'] = json_encode($result);
                $result = iconv($in_charset = 'UTF-16LE', $out_charset = 'UTF-8', $curl->html);
            }
        }
        $curl->logout_mstr($ch);

        //Ergebnis in Datenbank speichern        
        $data['action'] = 'mstr';
        $data['school'] = '';
        $data['data'] = json_encode($result);
        $now = Time::parse();
        $now->timezone = "Europe/Berlin";
        $data['date'] = $now;
        if (strpos($result, 'MMIHG') !== false) {
            $data['comment'] = 'Success';
        } else {
            $data['comment'] = 'Fail';
        }
        $data['message'] = $curl->message;
        
        $all = $this->Monitorings->find('all', array('conditions' => array('action' => 'mstr'), 'order' => array('id' => 'DESC')));
        $last = $all->first();   
        
        $monitoring = $this->Monitorings->newEntity();
        $monitoring = $this->Monitorings->patchEntity($monitoring, $data);

        if($last['comment'] != $monitoring['comment']) {
            if($last['comment'] == 'Success') {
                
                $curl->send_mail($ch,'Fail',$curl->message, '');
            }
            else {
                $allsuccess = $this->Monitorings->find('all', array('conditions' => array('action' => 'mstr','comment' => 'Success'), 'order' => array('id' => 'DESC')));
                $lastsuccess = $allsuccess->first();
                $datestring = $lastsuccess['date']->i18nFormat('dd.MM.yyyy HH:mm');
                $curl->send_mail($ch,'Success',$curl->message, $datestring);
            }
        }

        $result_save = $this->Monitorings->save($monitoring);

        //$curl->send_mail($ch);

        $this->set('html', $result);
        $this->viewBuilder()->layout('ajax');       
    }

    public function saveus() {
        $curl = new AsdUs();
        //debug($curl->status);
        $ch = $curl->init_curl_params();
        $curl->login_school($ch);
        
        $curl->login_asd($ch);
        //echo $curl->html;die;
        $curl->save_us_data($ch);
        //echo $curl->html;die;
        $this->set('html',$curl->html);
        $this->redirect(['action' => 'getjsonus']);
    }

    public function saveup() {
        $curl = new AsdUs();
        //debug($curl->status);
        $ch = $curl->init_curl_params();
        $curl->login_school($ch);
        $curl->login_asd($ch);
        $curl->save_up_data($ch);
        $this->set('html',$curl->html);
        $this->redirect(['action' => 'getjsonup']);
    }


    public function showus() {
        $this->loadModel('Results');
        $results = $this->Results->find('all');
        $this->set('results',$results);
    }

    public function getjsonus() {
        echo 'Write Json data to file';
        $this->loadModel('Results');
        $results = $this->Results->find('all');
        $this->set('results',$results);
        $file = new File('/var/www/html/asd-control/webroot/files/data-us.json', true, 0644);
        $file->create();
        debug(json_encode($results));
        $status = $file->write(json_encode($results), 'w', true);
        echo $status;
        $file->close();
        $this->viewBuilder()->setLayout('getjson');
    }

    public function getjsonup() {
        echo 'Write Json data to file';
        $this->loadModel('Results');
        $results = $this->Results->find('all');
        $this->set('results',$results);
        $file = new File('/var/www/html/asd-control/webroot/files/data-up.json', true, 0644);
        $file->create();
        $status = $file->write(json_encode($results), 'w', true);
        echo $status;
        $file->close();
        $this->viewBuilder()->layout('getjson');
    }

    public function usStatistik() {
        $file = new File('/var/www/html/asd-control/webroot/files/data-us.json', true, 0644);
        $ts = $file->lastChange();
        $time = Time::createFromTimestamp($ts);
        $time->timezone = 'Europe/Paris';
        $this->set('time', $time);
        $this->viewBuilder()->setLayout('pivot');
    }

    public function upStatistik() {
        $file = new File('/var/www/html/asd-control/webroot/files/data-up.json', true, 0644);
        $ts = $file->lastChange();
        $time = Time::createFromTimestamp($ts);
        $time->timezone = 'Europe/Paris';
        $this->set('time', $time);
        $this->viewBuilder()->layout('pivot');
    }

    public function asdus()
    {
        $curl = new AsdUs();
        //debug($curl->status);
        $ch = $curl->init_curl_params();
        $curl->login_school($ch);
        $curl->login_asd($ch);

        $output = $curl->get_us_statistic($ch);

        $this->loadModel('Monitorings');
        if (!empty($this->request['pass'])) {
            $command = $this->request['pass']['0'];
            if ($command == 'save') {
                $data['action'] = 'asdus';
                $data['school'] = '';
                $data['data'] = json_encode($output);
                $now = Time::parse();
                $now->timezone = "Europe/Berlin";
                $data['date'] = $now;
                $data['comment'] = '';
                $monitoring = $this->Monitorings->newEntity();
                $monitoring = $this->Monitorings->patchEntity($monitoring, $data);
                $result = $this->Monitorings->save($monitoring);
            }
        }

        $all = $this->Monitorings->find('all', array('conditions' => array('action' => 'asdus'), 'order' => array('id' => 'DESC')));
        $old = $all->first();
        $now = Time::parse();
        $now->timezone = "Europe/Berlin";
        $this->set('now', $now);
        $this->set('old', $old);
        $this->set('output', $output);
        $this->set('html', $this->html);
    }
}
