<?php

namespace loyalsoft;

require_once __DIR__ . '/TapDB_config.php';
require_once __DIR__ . '/Events/TapDBEvent.php';
require_once __DIR__ . '/Events/TapDBPayEvent.php';

/**
 * Description of TapDBUtil
 *
 * @author gwang
 */
class TapDBUtil {

    /**
     * 主动推送充值记录
     * @param \loyalsoft\pay\OrderNotice $order
     * @param string $payMent 支付渠道: wx/zfb/yyb/hw
     */
    public static function PushPayEvent($order, $payMent) {
        $eventData = new TapDBPayEvent($order, $payMent);
        $json = json_encode($eventData);
        CLog::pay("TapDB上报 $json");
        return self::PushEventData($json);
    }

    /**
     * 上报用户登录
     * @return type
     */
    public static function SOnUserLogin() {
        $data = new TapDBPreEvent(req()->uid, 'user_login', array());
        return self::PushEventData(JsonUtil::encode($data));
    }

    /**
     * 接收客户端上报数据并转发给tapdb
     */
    public static function OnClientReport() {
        $params = query_paras();
        ['UID' => $uid, 'eventName' => $eventName] = $params;
        unset($params['eventName']);
        $props = self::dealPropsName($params);
        return self::PushJsonEvent($uid, $eventName, $props);
    }

    public static function TestPay() {
        $args = [];
        $args['openid'] = 'oEW_p0gt1shMJMY4S2KiUSaLxNp0_yyb'; // openid                                    # 玩家uid(不带_yyb)
        $args['amt'] = 600;                                              # 支付金额, 游戏后台统一单位是分
        $args['appmeta'] = 'oEW_p0gt1shMJMY4S2KiUSaLxNp0_yyb,1,1022304201408000815*qbqd*wechat';
        $order = pay\OrderNotice::Parse_ysdkOrder($args);
        self::PushPayEvent($order, 'yyb');
    }

    public static function Test() {

        $params = ['UID' => "Wanggang", 'eventName' => "#EventCount", 'zoneid' => 1];
        ['UID' => $uid, 'eventName' => $eventName] = $params;
        unset($params['eventName']);
        $props = self::dealPropsName($params);
        return self::PushJsonEvent($uid, $eventName, $props);
    }

    /**
     * 处理下事件属性, 加上'#'前缀
     * @param dic $param
     * @return asocArray
     */
    private static function dealPropsName($param) {
        $arr = [];
        foreach ($param as $k => $v) {
            $k = urldecode($k);
            $nk = str_starts_with($k, '#') ? $k : "#$k";
            $arr[$nk] = urldecode($v);
        }
        return $arr;
    }

    /**
     * 向tapdb后台主动推送事件
     * @param string $uid 玩家唯一id
     * @param string $eventName
     * @param dic $params
     */
    public static function PushJsonEvent($uid, $eventName, $params) {
        $data = new TapDBEvent($uid, $eventName, $params);
        return self::PushEventData(JsonUtil::encode($data));
    }

    /**
     * 向TapDB后台推送事件数据
     * @param string $json
     * @return boolean 推送成功 true, 失败: false
     */
    private static function PushEventData($json) {
        $cfg = TapDB_config::Ins();
        ['result' => $ok, 'msg' => $resp] = HttpUtil::makeRequest($cfg->serverUrl, $json, [], [$cfg->ContentType], $cfg->method);
        if (!$ok) {                                                         # 如果失败, 打下日志
            CLog::err("TapDB上报失败!( $resp ) " . $json);
        }
//        $arr = HttpUtil::makeRequest($cfg->serverUrl, $json, [], [$cfg->ContentType], $cfg->method);
//        $ok = $arr['result'];
//        $resp = $arr['msg'];
//        if (!$ok) {                                                         # 如果失败, 打下日志
//            CLog::err("TapDB上报失败!( $resp ) " . $json);
//        }

        return $ok;
    }

}
