<?php

namespace Ministra\Lib;

use Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\d3507c2999d38573a0a0c91771d70af2;
use Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\U7b993fd611168684ddb26c8f5ab7e336;
class Log
{
    private static $instance = null;
    private $db;
    private $stb;
    public function __construct()
    {
        $this->db = \Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\d3507c2999d38573a0a0c91771d70af2::getInstance();
        $this->stb = \Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\U7b993fd611168684ddb26c8f5ab7e336::getInstance();
    }
    public static function getInstance()
    {
        if (self::$instance == null) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    public static function writePackageSubscribeLog($user_id, $package_id, $set_state)
    {
        $data = ['user_id' => $user_id, 'set_state' => $set_state, 'package_id' => $package_id];
        if (!empty(\Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\U7b993fd611168684ddb26c8f5ab7e336::getInstance()->id) && (empty($_SERVER['TARGET']) || $_SERVER['TARGET'] !== 'API' && $_SERVER['TARGET'] !== 'ADM')) {
            $data['initiator_id'] = \Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\U7b993fd611168684ddb26c8f5ab7e336::getInstance()->id;
            $data['initiator'] = 'user';
        } else {
            $data['initiator_id'] = \Ministra\Lib\Admin::getInstance()->getId();
            if (!empty($data['initiator_id'])) {
                $data['initiator'] = 'admin';
            }
        }
        \Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\d3507c2999d38573a0a0c91771d70af2::getInstance()->insert('package_subscribe_log', $data);
    }
    public function savePageGenerationTime($time)
    {
        $time = $time * 1000;
        if ($time >= 500) {
            $default_row = '500ms';
        } elseif ($time >= 400) {
            $default_row = '400ms';
        } elseif ($time >= 300) {
            $default_row = '300ms';
        } elseif ($time >= 200) {
            $default_row = '200ms';
        } elseif ($time >= 100) {
            $default_row = '100ms';
        } else {
            $default_row = '0ms';
        }
        $item = $this->db->from('generation_time')->where(['time' => $default_row])->get()->first();
        $this->db->update('generation_time', ['counter' => $item['counter'] + 1], ['time' => $default_row]);
    }
}
