<?php

namespace loyalsoft;

require_once __DIR__ . '/Volcengine_config.php';
require_once __DIR__ . '/VolcModels.php';

/**
 * 火山引擎
 * @author gwang
 */
class VolcUtil {

    /**
     * 初始化
     *  接收客户端上传的附加参数信息.并存入redis
     *  后面上报时直接提取信息并合并到headers
     */
    public static function Init($app_name, $app_package, $app_channel, $app_version, #
            $os_name, $os_version, $device_model, #
            $ab_version, $traffic_type) {
        $client_ip = HttpUtil::clientIP();                                      # 客户端ip
        $headers = compact('app_name', 'app_package', 'app_channel', 'app_version', #
                'os_name', 'os_version', 'device_model', #
                'ab_version', 'traffic_type', 'client_ip'
        );
        $zoneid = req()->zoneid;
        $uid = req()->uid;
        $key = MemKey_User::stat_volc_headers($zoneid, $uid);
        gMem()->set($key, $headers);
    }

    private static function Headers() {
        $zoneid = req()->zoneid;
        $uid = req()->uid;
        return gMem()->get(MemKey_User::stat_volc_headers($zoneid, $uid));
    }

    private static function User() {
        $user = new Volc_User();
        $user->user_unique_id = req()->uid;
        return $user;
    }

    /**
     * 上报统计事件
     * @param type $event_name
     * @param type $params
     */
    public static function Report($event_name, $params) {
        $user = self::User();
        $header = self::Headers();
        $event = new Volc_Event();
        $event->event = $event_name;
        $event->local_time_ms = millisecond();
        $event->params = $params;
        $data = array(
            'user' => $user,
            'header' => $header,
            'events' => array($event)
        );
        HttpUtil::makeRequest(Volcengine_config::ReportOneURL, $data, array(), Volcengine_config::Headers());
    }

}
