<?php

class MiscHandler extends RequestHandler
{

    public function GET_main() {
        global $config;
        $this->tpl->set_title('Главная');
        $this->tpl->set([
            'grafana_sensors_url' => $config['grafana_sensors_url'],
            'grafana_inverter_url' => $config['grafana_inverter_url'],
            'cameras' => $config['cam_list']['labels']
        ]);
        $this->tpl->render_page('index.twig');
    }

    public function GET_sensors_page() {
        global $config;

        $clients = [];
        foreach ($config['temphumd_servers'] as $key => $params) {
            $cl = new TemphumdClient(...$params);
            $clients[$key] = $cl;

            $cl->readSensor();
        }

        $this->tpl->set(['sensors' => $clients]);
        $this->tpl->set_title('Датчики');
        $this->tpl->render_page('sensors.twig');
    }

    public function GET_pump_page() {
        global $config;

        list($set) = $this->input('set');
        $client = new GPIORelaydClient($config['pump_host'], $config['pump_port']);

        if ($set == GPIORelaydClient::STATUS_ON || $set == GPIORelaydClient::STATUS_OFF) {
            $client->setStatus($set);
            redirect('/pump/');
        }

        $status = $client->getStatus();

        $this->tpl->set([
            'status' => $status
        ]);
        $this->tpl->set_title('Насос');
        $this->tpl->render_page('pump.twig');
    }

    public function GET_cams() {
        global $config;

        list($hls_debug, $video_events, $high, $camera_ids) = $this->input('b:hls_debug, b:video_events, b:high, id');
        if ($camera_ids != '') {
            $camera_param = $camera_ids;
            $camera_ids = explode(',', $camera_ids);
            $camera_ids = array_filter($camera_ids);
            $camera_ids = array_map('trim', $camera_ids);
            $camera_ids = array_map('intval', $camera_ids);
        } else {
            $camera_ids = array_keys($config['cam_list']['labels']);
            $camera_param = '';
        }

        $tab = $high ? 'high' : 'low';

        // h264
        $js_hls_config = [
            'opts' => [
                'startPosition' => -1,

                // // https://github.com/video-dev/hls.js/issues/3884#issuecomment-842380784
                'liveSyncDuration' => 2,
                'liveMaxLatencyDuration' => 3,
                'maxLiveSyncPlaybackRate' => 2,
                'liveDurationInfinity' => true,
            ],
            'debugVideoEvents' => !!$video_events,
        ];
        if ($hls_debug)
            $js_hls_config['debug'] = true;

        // h265
        $js_h265webjs_config = [
            // https://github.com/numberwolf/h265web.js/blob/master/README_EN.MD#freetoken
            'token' => 'base64:QXV0aG9yOmNoYW5neWFubG9uZ3xudW1iZXJ3b2xmLEdpdGh1YjpodHRwczovL2dpdGh1Yi5jb20vbnVtYmVyd29sZixFbWFpbDpwb3JzY2hlZ3QyM0Bmb3htYWlsLmNvbSxRUTo1MzEzNjU4NzIsSG9tZVBhZ2U6aHR0cDovL3h2aWRlby52aWRlbyxEaXNjb3JkOm51bWJlcndvbGYjODY5NCx3ZWNoYXI6bnVtYmVyd29sZjExLEJlaWppbmcsV29ya0luOkJhaWR1',
        ];

        $js_config = [
            'isLow' => $tab == 'low',
            'proto' => config::get('cam_hls_proto'),
            'host' => config::get('cam_hls_host'),
            'camIds' => $camera_ids,
            'camLabels' => array_map(fn($id) => $config['cam_list']['labels'][$id], $camera_ids)
        ];

        $cams_by_type = [];
        $include_h264 = false;
        $include_h265 = false;
        foreach ($camera_ids as $camera_id) {
            $var_name = 'include_'.$config['cam_list']['full'][$camera_id]['type'];
            $cams_by_type[$camera_id] = $config['cam_list']['full'][$camera_id]['type'];
            $$var_name = true;
        }
        if ($include_h264) {
            $js_config['hlsConfig'] = $js_hls_config;
            $this->tpl->add_static('hls.js');
        }
        if ($include_h265) {
            $js_config['h265webjsConfig'] = $js_h265webjs_config;
            $this->tpl->add_static('h265webjs-dist/missile.js');
            $this->tpl->add_static('h265webjs-dist/h265webjs-v20221106-reminified.js');
        }

        $js_config['camsByType'] = $cams_by_type;

        $hls_key = config::get('cam_hls_access_key');
        if ($hls_key)
            setcookie_safe('hls_key', $hls_key);

        // $cam_filter = function($id) use ($config, $camera_ids) {
        //     return in_array($id, $camera_ids);
        // };

        $this->tpl->set([
            'js_config' => $js_config,

            // 'hls_access_key' => $config['cam_hls_access_key'],

            'camera_param' => $camera_param,
            // 'cams' => array_values(array_filter($config['cam_list'][$tab], $cam_filter)),
            'tab' => $tab,
            'video_events' => $video_events
        ]);
        $this->tpl->set_title('Камеры');
        $this->tpl->render_page('cams.twig');
    }

    public function GET_cams_stat() {
        global $config;
        list($ip, $port) = explode(':', $config['ipcam_server_api_addr']);
        $body = jsonDecode(file_get_contents('http://'.$ip.':'.$port.'/api/timestamp/all'));

        header('Content-Type: text/plain');
        $date_fmt = 'd.m.Y H:i:s';

        foreach ($body['response'] as $cam => $data) {
            $fix = date($date_fmt, $data['fix']);
            $start = date($date_fmt, $data['motion_start']);
            $motion = date($date_fmt, $data['motion']);
            echo "$cam:\n          motion: $motion\n";
            echo "    motion_start: $start\n";
            echo "             fix: $fix\n\n";
        }
    }

    public function GET_debug() {
        print_r($_SERVER);
    }

    public function GET_phpinfo() {
        phpinfo();
    }

}