<?php

namespace Ministra\Lib;

use Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\k8281faec2a4a41f470b00112682ddabf;
use Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\b0144dbc621686d5838378f3b96bd17b;
use Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\U7b993fd611168684ddb26c8f5ab7e336;
class Watchdog extends \Ministra\Lib\AjaxResponse implements \Ministra\Lib\StbApi\Watchdog
{
    public function __construct()
    {
        parent::__construct();
    }
    public function getEvents()
    {
        $just_started = isset($_REQUEST['init']) ? (int) $_REQUEST['init'] : 0;
        if (isset($_REQUEST['init']) && \Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\k8281faec2a4a41f470b00112682ddabf::getSafe('log_mac_clones', false) && $just_started == 0 && \Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\U7b993fd611168684ddb26c8f5ab7e336::getInstance()->getParam('just_started') == 0) {
            $clone_ip = \Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\b0144dbc621686d5838378f3b96bd17b::o39138d989795feb704fa489ad5b80cd5();
            if ($clone_ip) {
                \Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\U7b993fd611168684ddb26c8f5ab7e336::k0f82ad723f0c5e5ace1f86b83914c8d5($clone_ip);
            }
        }
        if ($this->stb->getParam('ip') != $this->stb->ip) {
            $user = \Ministra\Lib\User::getInstance($this->stb->id);
            $user->getInfoFromOSS();
        }
        $this->db->update('users', ['keep_alive' => 'NOW()', 'ip' => $this->stb->ip, 'now_playing_type' => (int) $_REQUEST['cur_play_type'], 'just_started' => $just_started, 'last_watchdog' => 'NOW()'], ['mac' => $this->stb->mac]);
        $events = \Ministra\Lib\Event::getAllNotEndedEvents($this->stb->id);
        $messages = \is_array($events) ? \count($events) : 0;
        $res['data'] = [];
        $res['data']['msgs'] = $messages;
        if ($messages > 0) {
            if ($events[0]['sended'] == 0) {
                \Ministra\Lib\Event::setSended($events[0]['id']);
                if ($events[0]['need_confirm'] == 0) {
                    \Ministra\Lib\Event::setEnded($events[0]['id']);
                }
            }
            if ($events[0]['id'] != @$_GET['data']['event_active_id']) {
                $res['data']['id'] = $events[0]['id'];
                $res['data']['event'] = $events[0]['event'];
                $res['data']['need_confirm'] = $events[0]['need_confirm'];
                $res['data']['msg'] = $events[0]['msg'];
                $res['data']['reboot_after_ok'] = $events[0]['reboot_after_ok'];
                $res['data']['auto_hide_timeout'] = $events[0]['auto_hide_timeout'];
                $res['data']['post_function'] = $events[0]['post_function'];
                $res['data']['param1'] = $events[0]['param1'];
                $res['data']['valid_until'] = \strtotime($events[0]['eventtime']);
                if (\Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\k8281faec2a4a41f470b00112682ddabf::getSafe('display_send_time_in_message', false)) {
                    $res['data']['send_time'] = $events[0]['addtime'];
                }
            }
        }
        $res['data']['additional_services_on'] = \Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\k8281faec2a4a41f470b00112682ddabf::getSafe('enable_tariff_plans', false) ? '1' : $this->stb->additional_services_on;
        return $res;
    }
    public function confirmEvent()
    {
        \Ministra\Lib\Event::setConfirmed((int) $_REQUEST['event_active_id']);
        $res['data'] = 'ok';
        return $res;
    }
}
