package com.ez.ertc.meeting.api

import com.ez.network.APIResponse
import com.ez.network.BaseException
import com.ez.network.RestfulUtils
import org.json.JSONException
import org.json.JSONObject
import java.util.*

class RtcAPI {
    private val TAG = "EzAPI"

    companion object {
        var instance: RtcAPI? = null
            get() {
                if (field == null) {
                    field = RtcAPI()
                }
                return field
            }
    }

    @Throws(BaseException::class)
    fun getAccessToken(appKey: String, appSecret: String): String? {
        val url = "/api/lapp/token/get"
        val paramMap: HashMap<String, Any> = HashMap()
        paramMap["appKey"] = appKey
        paramMap["appSecret"] = appSecret
        val resultObject = RestfulUtils.getInstance().post(null, paramMap, url, object : APIResponse() {
            @Throws(BaseException::class, JSONException::class)
            override fun parse(response: String): Any {
                parseCode(response)
                val rootObject = JSONObject(response)
                return JSONObject(rootObject.optString("data")).getString("accessToken")
            }
        }, false)
        return resultObject as String?
    }
}