package com.example.kingway.ptt;

import com.stand.standapp.MyApplication;

import timber.log.Timber;

/**
 * 指令发送类
 * */
public class SendAtUtil {

    public static void sendRawCmd(String cmd) {
        Timber.tag("PTT_TX").i("CMD: %s", cmd);
        MyApplication.getAppInstance().getpNative().pttNativeSendAtCmd(cmd);
    }

    private static void send(String cmd) {
        sendRawCmd(cmd);
    }

    /**
     * 获取登录参数
     */
    public static void toGetParam() {
        send("020000\r\n");
    }

    /**
     * 取消登录接口 (清除服务端/本地登录状态)
     */
    public static void toCancelLogin() {
        send("050000\r\n");
    }

    /**
     * 账号注销接口
     */
    public static void toLogout() {
        send("040000\r\n");
    }

    /**
     * 账号登录接口
     * account 账号
     * pwd 密码
     * ip 服务器ip
     * location 是否开启定位 1-开启，0关闭
     * tempCall 是否开启单呼 1开启单呼，0关闭单呼
     * */
    public static void toLogin(String account, String pwd, String ip, int location, int tempCall){
        Timber.tag("PTT").i("Login: account=%s, ip=%s, gps=%d, tempCall=%d", account, ip, location, tempCall);
        String ipHex = str2HexStr("ip=" + ip + ";");
        String accountHex = str2HexStr("id=" + account + ";");
        String pwdHex = str2HexStr("pwd=" + pwd + ";");
        String locationHex = str2HexStr("gps=" + location + ";");
        String tempCallHex = str2HexStr("inv=" + tempCall + ";");
        String loginAt = "010000" + ipHex + "" + accountHex + "" + pwdHex + locationHex + tempCallHex + "\r\n";
        send(loginAt);
        send("00000000\r\n");
    }

    /**
     * 按下PTT开始呼叫
     * */
    public static void speakPlay(){
        Timber.tag("PTT").i("PTT speak START (press)");
        send("0B0000");
    }

    /** 松开PTT结束呼叫*/
    public static void speakRelease(){
        Timber.tag("PTT").i("PTT speak STOP (release)");
        AudioRecordManager.getInstance().stopRecord();
        send("0C0000");
    }

    /** 发送TCP包 登录成功后需要定时发送TCP包*/
    public static void sendTCP(){
        Timber.tag("PTT").v("TCP keepalive sent");
        send("570000\r\n");
    }

    /** 发送UDP包 登录后需要定时发送UDP包*/
    public static void sendUDP(){
        Timber.tag("PTT").v("UDP keepalive sent");
        send("580000\r\n");
    }

    /** 请求群组信息 */
    public static void getGroupInfo(){
        send("0D0000");
    }

    /**  请求群组成员信息
     * groupId 十六进制群组Id
     * */
    public static void getGroupMemberInfo(String groupId){
        send("0E0000" + groupId);
    }

    /**  切换群组
     * groupId 十六进制群组Id
     * */
    public static void jumpGroup(String groupId){
        Timber.tag("PTT").i("Jump to group: %s", groupId);
        send("090000" + groupId);
    }

    /** 请求好友信息 */
    public static void getFriendInfo(){
        send("0E000000000000\r\n");
    }

    /** 结束多成员临时呼叫 */
    public static void stopTempCall(){
        send("5500000100000000\r\n");
    }


    /**
     * 字符串转换成十六进制字符串
     *
     * @param str 待转换的ASCII字符串
     * @return String
     */
    public static String str2HexStr(String str) {

        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = str.getBytes();
        int bit;

        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f;
            sb.append(chars[bit]);
        }
        return sb.toString().trim();
    }
}
