<?php

namespace addons\csmtable\library\xcore\xcore\utils;

use app\admin\library\Auth;
use app\admin\model\csmtable\Cloginlog;
use addons\csmtable\library\xcore\xcore\base\XcAdminAuth;

/**
 * 基于Header#clogintoken的SessionUser的工具封装类
 * 通用用户api方法中
 */
/**
 * 获取Admin的用户用户信息，可用于Admin_Application和Addons_AdminAPI
 * 获取session的顺序:
 * 1. Auth
 * 2. header_clogintoken
 * 3. request_get_clogintoken
 */
class XcAdminSessionUtils
{
    public static function directApplicationAdminLogin(){
        $auth = XcAdminAuth::instance();
        $auth->directLogin(static::getUserId());
    }

    public static function getToken($assertLoginError = true)
    {
        $userinfo = static::getUserinfo($assertLoginError);
        return $userinfo ? $userinfo["token"] : null;
    }


    public static function getUserId($assertLoginError = true)
    {
        $userinfo = static::getUserinfo($assertLoginError);
        if ($assertLoginError === true && $userinfo == null) {
            XcResponseUtils::error("need_login");
        }
        return ($userinfo) ? $userinfo['id'] : null;
    }

    public static function getUserinfo($assertLoginError = true)
    {
        $userinfo = null;
        if (true) {
            $auth = XcAdminAuth::instance();
            $userinfo = $auth->getUserInfo();
        }

        if ($userinfo == null) {
            $token = null;
            if (true) {
                $token = XcRequestUtils::header("clogintoken");
                $token = (empty($token)) ? XcRequestUtils::get("clogintoken") : $token;
                $token = (empty($token)) ? XcRequestUtils::post("clogintoken") : $token;
            }

            $userinfo = null;
            if ($token != null && $token != "") {
                $userinfo = XcDaoUtils::getRowBySql(new \app\admin\model\Admin(), [
                    ["token", "=", $token]
                ], "id, username, nickname", true, false);
            }
        }


        if ($assertLoginError === true && $userinfo == null) {
            XcResponseUtils::error("need_login");
        }
        return $userinfo;
    }

    public static function getUserNickName()
    {
        $userinfo = static::getUserinfo();
        return $userinfo['nickname'];
    }

    public static function savelog($operate, $object_id, $content = null)
    {
        $user_id = static::getUserId();
        return static::savelog_unlogined($operate, $user_id, $object_id, $content);
    }

    public static function savelog_unlogined($operate, $user_id, $object_id, $content = null)
    {
        $port = 21;
        $agent = $_SERVER['HTTP_USER_AGENT'];
        if (preg_match('/MicroMessenger/', $_SERVER['HTTP_USER_AGENT'])) {
            $port = 11;
        } else if (preg_match('/Electron/', $_SERVER['HTTP_USER_AGENT'])) {
            $port = 22;
        }

        $row = null;
        if (true) {
            $dao = new Cloginlog();
            $row = $dao->create([
                "admin_id" => $user_id,
                "operate" => $operate,
                "port" => $port,
                "object_id" => $object_id,
                "createtime" => time(),
                "cyear" => date('Y'),
                "cmonth" => date('m'),
                "cdate" => date('d'),
                "cweek" => date("w"),
                "chour" => date("H"),
                "cmin" => date("i"),
                "content" => $content
            ]);
        }
        return $row;
    }

    public static function savelog_after_response($logRow, $recontent)
    {
        $logRow->save([
            "recontent" => $recontent,
            "septime" => (time() - $logRow->createtime)
        ]);
    }

    public static function matchRule($path)
    {
        $auth = Auth::instance();
        $rules = $auth->getRuleList(static::getUserId());
        if (in_array($path, $rules)) {
            return true;
        } else {
            return false;
        }
    }
}
