<?php

namespace Ministra\Lib;

use Ministra\Lib\bd5c9022e9a9029209e5fd05775ea272\B4291b8d8fa2d898727b1f446db1e661e;
use Ministra\Lib\bd5c9022e9a9029209e5fd05775ea272\bcb5276dcc10d28aee463111bf6dfaa1;
class Log
{
    private static $instance = null;
    private $db;
    private $stb;
    public function __construct()
    {
        $this->db = \Ministra\Lib\bd5c9022e9a9029209e5fd05775ea272\B4291b8d8fa2d898727b1f446db1e661e::getInstance();
        $this->stb = \Ministra\Lib\bd5c9022e9a9029209e5fd05775ea272\bcb5276dcc10d28aee463111bf6dfaa1::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\bd5c9022e9a9029209e5fd05775ea272\bcb5276dcc10d28aee463111bf6dfaa1::getInstance()->id) && (empty($_SERVER['TARGET']) || $_SERVER['TARGET'] !== 'API' && $_SERVER['TARGET'] !== 'ADM')) {
            $data['initiator_id'] = \Ministra\Lib\bd5c9022e9a9029209e5fd05775ea272\bcb5276dcc10d28aee463111bf6dfaa1::getInstance()->id;
            $data['initiator'] = 'user';
        } else {
            $data['initiator_id'] = \Ministra\Lib\Admin::getInstance()->getId();
            if (!empty($data['initiator_id'])) {
                $data['initiator'] = 'admin';
            }
        }
        \Ministra\Lib\bd5c9022e9a9029209e5fd05775ea272\B4291b8d8fa2d898727b1f446db1e661e::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]);
    }
}
