package com.gh.gamecenter.qa.editor

import android.content.Context
import android.webkit.JavascriptInterface
import com.gh.gamecenter.common.base.activity.BaseActivity
import com.gh.gamecenter.core.AppExecutor
import com.gh.common.util.*
import com.gh.gamecenter.GameDetailActivity
import com.gh.gamecenter.core.utils.GsonUtils
import com.gh.gamecenter.common.utils.clickToastByStatus
import com.gh.gamecenter.common.utils.tryWithDefaultCatch
import com.gh.gamecenter.core.utils.MtaHelper
import com.gh.gamecenter.common.entity.CommunityEntity
import com.gh.gamecenter.entity.MtaEvent
import com.gh.gamecenter.entity.MyVideoEntity
import com.gh.gamecenter.gamecollection.detail.GameCollectionDetailActivity
import com.gh.gamecenter.qa.answer.detail.SimpleAnswerDetailActivity
import com.gh.gamecenter.qa.article.detail.ArticleDetailActivity
import com.gh.gamecenter.qa.entity.EditorInsertEntity

/**
 * 富文本编辑框的链接回调
 */
class OnLinkClickListener(
    val context: Context,
    val title: String = "",
    val status: String = "",
    val entrance: String,
    val path: String,
    val mtaEvent: MtaEvent? = null
) {
    @JavascriptInterface
    fun onClick(content: String) {
        AppExecutor.uiExecutor.execute {
            tryWithDefaultCatch {
                val insertEntity = GsonUtils.fromJson(content, EditorInsertEntity::class.java)
                when (insertEntity.type) {
                    "answer" -> {
                        val intent = SimpleAnswerDetailActivity.getIntent(
                            context,
                            insertEntity.id ?: "",
                            entrance,
                            "$path-链接"
                        )
                        context.startActivity(intent)
                    }
                    "community_article" -> {
                        val community = CommunityEntity(insertEntity.communityId!!, "")
                        val intent = ArticleDetailActivity.getIntent(
                            context,
                            community,
                            insertEntity.id!!,
                            entrance,
                            "$path-链接"
                        )
                        context.startActivity(intent)
                    }
                    "game" -> {
                        GameDetailActivity.startGameDetailActivity(
                            context,
                            insertEntity.id ?: "",
                            BaseActivity.mergeEntranceAndPath(entrance, "$path-链接")
                        )
                    }
                    "game_collection" -> {
                        context.startActivity(GameCollectionDetailActivity.getIntent(context, insertEntity.id ?: ""))
                    }
                    "video" -> {
                        DirectUtils.directToVideoDetail(context, insertEntity.id ?: "", entrance, "$path-链接")
                    }
                }
            }
        }
    }

    @JavascriptInterface
    fun onVideoClick(content: String) {
        AppExecutor.uiExecutor.execute {
            tryWithDefaultCatch {
                if (mtaEvent != null) MtaHelper.onEvent(mtaEvent.name, mtaEvent.key, mtaEvent.value)
                val videoEntity = GsonUtils.fromJson(content, MyVideoEntity::class.java)
                clickToastByStatus(videoEntity.status) {
                    FullScreenVideoActivity.start(context, title, videoEntity.url, videoEntity.poster)
                }
            }
        }
    }

    @JavascriptInterface
    fun onVideoClick(url: String, poster: String) {
        clickToastByStatus(status) {
            FullScreenVideoActivity.start(context, title, url, poster)
        }
    }
}