<?php


namespace Api\PhpServices\PushToken;


use Api\PhpUtils\Http\Request;

class Base
{
    const ERROR_CODE_PARAM = 1;
    const ERROR_CODE_KAFKA = 2;

    // appid 最大长度
    const MAX_APPID_LENGTH = 20;

    // android token 的最大长度
    const MAX_ANDROID_TOKEN_LENGTH = 40;

    // ios token 的最大长度
    const MAX_IOS_TOKEN_LENGTH = 80;

    // 特殊的appid（普通appid只包含数字字母）
    static $SPECIAL_APPIDS = array('web_mixapp' => '');

    // android token 合法前缀
    static $ANDROID_VALID_TOKEN_PREFIX = array('MMPP', 'UMPP', 'GTPP', 'JGPP', 'OPPP', 'HMSP', 'FCMP', 'OPPO', 'VIVO');

    const DEVICE_MASK = 0xF0;
    const DEVICE_NONE = 0x00;
    const DEVICE_XIAOMI = 0x10;
    const DEVICE_HUAWEI = 0x20;
    const DEVICE_MEIZU = 0x30;
    const DEVICE_UNKNOWN = 0x40;
    const DEVICE_NUBIA = 0x50;
    const DEVICE_ZTE = 0x60;
    const DEVICE_OPPO = 0x70;
    const DEVICE_VIVO = 0x80;
    const DEVICE_SAMSUNG = 0x90;

    const DEVICE_ID_LENGTH = 80;

    /**
     * 校验appid
     * @param $appid
     * @return bool
     */
    protected static function valid_appid($appid){
        return strlen($appid) <= self::MAX_APPID_LENGTH
                && ctype_alnum($appid)
                || isset(self::$SPECIAL_APPIDS[$appid]);
    }

    /**
     * 校验安卓token
     * @param $token
     * @return bool
     */
    protected static function valid_android_token($token)
    {
        return strlen($token) <= self::MAX_ANDROID_TOKEN_LENGTH
            && in_array(substr($token, 0, 4), self::$ANDROID_VALID_TOKEN_PREFIX)
            && ctype_alnum(substr($token, 4));
    }

    protected static function valid_ios_token($token)
    {
        return strlen($token) <= self::MAX_IOS_TOKEN_LENGTH && ctype_alnum($token);
    }

    protected static  function valid_device_id($device_id)
    {
        return strlen($device_id) < self::DEVICE_ID_LENGTH && ctype_alnum($device_id);
    }

    protected static function version($version = '', $default = '000000')
    {
        if ($version)
        {
            if (preg_match('/\d{6}/', trim($version)))
            {
                return trim($version);
            }
            return false;
        }

        return $default;
    }


    protected static function enable($enable, $personal_rec){
        return $enable == 0 ? '0' : (!$personal_rec ? '3' : '1');
    }




    protected static function set_device_type_from_brand($push_level, $brand)
    {
        if(!$brand){
            return $push_level;
        }
        switch ($brand)
        {
            case 'Xiaomi':
            case 'xiaomi':
            case 'Redmi':
                $push_level =
                    strval(($push_level & (~self::DEVICE_MASK)) | self::DEVICE_XIAOMI);
                break;
            case 'HUAWEI':
            case 'HONOR':
            case 'Huawei':
            case 'Honor':
            case 'honor':
                $push_level =
                    strval(($push_level & (~self::DEVICE_MASK)) | self::DEVICE_HUAWEI);
                break;
            case 'Meizu':
                $push_level =
                    strval(($push_level & (~self::DEVICE_MASK)) | self::DEVICE_MEIZU);
                break;
            case 'nubia':
                $push_level =
                    strval(($push_level & (~self::DEVICE_MASK)) | self::DEVICE_NUBIA);
                break;
            case 'ZTE':
                $push_level =
                    strval(($push_level & (~self::DEVICE_MASK)) | self::DEVICE_ZTE);
                break;
            case 'OPPO':
            case 'oppo':
            case 'realme':
            case 'Realme':
                $push_level =
                    strval(($push_level & (~self::DEVICE_MASK)) | self::DEVICE_OPPO);
                break;
            case 'vivo':
                $push_level =
                    strval(($push_level & (~self::DEVICE_MASK)) | self::DEVICE_VIVO);
                break;
            case 'samsung':
            case 'SAMSUNG':
                $push_level =
                    strval(($push_level & (~self::DEVICE_MASK)) | self::DEVICE_SAMSUNG);
                break;
            default:
                $push_level =
                    strval(($push_level & (~self::DEVICE_MASK)) | self::DEVICE_UNKNOWN);
                break;
        }

        return $push_level;
    }

    protected static function sendKafka($data){
//        echo json_encode($data);exit();
        $url = config("interface","service.push_token") . '?' . config("interface","service.push_token_topic");
        $res = (new Request())->post($url, $data, 200000, 'json');
        if($res['code']==0){
            return true;
        }
        return false;
    }

}