<?php

namespace Api\PhpServices\PushToken;


use Api\PhpUtils\Http\Request;


class Android extends Base
{
    /**
     * @param bool $err 引用类型，是否存在错误
     * @param $appid
     * @param $userid
     * @param $data
     */
    public static function save(&$err = false, $appid, $data, $userid=''){
        $new_token      = $data['new_token'] ?? "";
        $old_token      = $data['old_token'] ?? "";
        $token          = $data['new_token'] ?? $data['old_token'] ?? "";
        $version        = self::version($data['version'] ?? "");
        $personal_rec   = $data['personalRec'] ?? 1;
        $enable         = self::enable($data['enable'] ?? 1, $personal_rec);
        $push_key       = $data['push_key'] ?? "";
        $device_id      = $data["device_id"] ?? "";
        if($device_id && !self::valid_device_id($device_id)){
            $device_id = "";
        }
        $android_id = $data["androidId"] ?? "";
        if($android_id && !self::valid_device_id($android_id)){
            $android_id = "";
        }

        if (!self::valid_appid($appid)){
            return ["code"=> self::ERROR_CODE_PARAM, "msg" => "appid有误"];
        }


        if( empty($token) || !self::valid_android_token($token) ){
            $err = true;
            return ["code"=> self::ERROR_CODE_PARAM, "msg" => "token有误"];
        }

        if( !$version ){
            $err = true;
            return ["code"=> self::ERROR_CODE_PARAM, "msg" => "version有误"];
        }

        if($new_token && substr($new_token, 0, 4) === 'OPPO' && (empty($push_key)) ){
            $err = true;
            return ["code"=> self::ERROR_CODE_PARAM, "msg" => "该类型token的push_key必传"];
        }

//        $action = explode('?', $_SERVER['REQUEST_URI'])[0];   // 与运营中台沟通，暂时写死成 website 路径，之后重构再调整
        $action = "/Website/push/BindingTokenForAndroidAction.php";

        $send = [
            'action'        => $action,
            'appid'         => $appid,
            'request_time'  => intval($_SERVER['REQUEST_TIME']),
            'version'       => $version,
            'userid'        => $userid,
            'time_zone'     => $data["time_zone"] ?? "",
            'personal_rec'  => $personal_rec,
            'enable'        => $enable,
            'sound'         => $data["sound"] ?? "",
            'push_level'    => self::set_device_type_from_brand($data["push_level"] ?? "1048576", $data["brand"] ??""),
            'old_token'     => $old_token,
            'new_token'     => $new_token,
            'push_key'      => $push_key,
            'cv'            => $data["cv"] ?? "",
            'device_id'     => $device_id,
            'android_id'    => $android_id,
            'os'            => $data["os"] ?? "",
            'distribution'  => $data["distribution"] ?? "",
            'platform'      => "Android"
        ];

        $send = array_filter ($send,function($v){if($v===''){return false;}else{return true;}});

        if(!self::sendKafka($send)){
            $err = true;
            return ["code"=> self::ERROR_CODE_KAFKA, "msg" => "send failed"];
        };
        $err = false;
        return true;
    }
}