package com.yidianling.muse.dialog

import android.webkit.JavascriptInterface
import com.google.gson.GsonBuilder
import com.ydl.webview.H5JsBean
import com.yidianling.common.tools.LogUtil
import com.yidianling.muse.activity.ChooseMusicActivity

/**
 * Created by Ykai on 2021/8/23.
 * 个人主页js交互类
 */
class JsInterface(webActivity: ChooseMusicActivity) {
    private var mWebActivity = webActivity

    private val gson by lazy {
        GsonBuilder().create()
    }

    @JavascriptInterface
    fun sendDataToOC(params: String) {
        LogUtil.i("js", params)
        val jsData = gson.fromJson(params, H5JsBean::class.java)
        jumpAction(jsData)
    }

    private fun jumpAction(jsData: H5JsBean) {
        when (jsData.cmd?.action_name) {
            "switch_sound" -> {
                jsData.cmd!!.params?.let{
                    val mediaId = it.mediaId
                    val meditationId = it.meditationId
                    val meditationType = it.meditionType
                    val businessType = it.businessType
                    val buried = it.buried
                    val mediaUrl = it.mediaUrl
                    val mediaCoverUrl = it.coverImageUrl
                    val title = it.title
                    val desc = it.desc
                    val status = it.status
                    if (mediaId != null && meditationId!=null && meditationType!=null) {
                        mWebActivity.switchSound(mediaId,meditationType.toLong(),businessType!!, meditationId,buried,mediaUrl,mediaCoverUrl,title,desc,status)
                    }
                }
            }
        }
    }
}