package com.yidianling.tests.home.plugin

import android.app.Activity
import android.net.Uri
import android.util.Log
import com.ydl.webview.H5Params
import com.ydl.webview.NewH5Activity
import com.ydl.ydl_router.manager.YDLRouterManager
import com.ydl.ydl_router.manager.YDLRouterParams
import com.ydl.ydlcommon.base.config.HttpConfig
import com.ydl.ydlcommon.router.IYDLRouterConstant
import com.yidianling.tests.list.view.TestCategoryListActivity
import com.yidianling.tests.router.TestsIn
import com.yidianling.tests.search.TestSearchActivity
import io.flutter.app.FlutterActivity
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel

/**
 * Created by xj on 2019/11/19.
 */
class TestPlugin : MethodChannel.MethodCallHandler {
    private var mActivity: FlutterActivity? = null
    private constructor(activity: FlutterActivity) {
        mActivity = activity
    }
    companion object {
        const val CHANNEL: String = "lib/test_module/channel"
        const val GO_BACK = "goBack" // 左上角返回按钮
        const val GO_SEARCH = "goSearch" // 查找测评
        const val GO_MY_TESTS = "goMyTests" // 我的测评
        const val BANNER_CLICK = "bannerClick" // Banner点击
        const val CATEGORY_CLICK = "categoryClick" // 分类点击
        const val ADVERT_CLICK = "advertClick" // 实时测评点击
        const val TEST_LIST_ITEM_CLICK = "testListItemClick" // 测评列表点击
        const val LOOK_ALL_TEST = "lookAllTest" // 查看全部测评

        fun rigister(activity: FlutterActivity) {
            MethodChannel(activity.flutterView, CHANNEL).setMethodCallHandler(TestPlugin(activity))
        }
    }

    override fun onMethodCall(methodCall: MethodCall, result: MethodChannel.Result) {
        when (methodCall.method) {
            GO_BACK -> {
                mActivity?.finish()
            }
            GO_SEARCH -> {
                mActivity?.let {
                    TestSearchActivity.start(mActivity!!)
                }
            }
            GO_MY_TESTS -> {
                if (!TestsIn.isLogin()) {
                    //如果未登录 走登录逻辑
                    mActivity?.startActivity(TestsIn.loginWayIntent(mActivity!!))
                    return
                }
                val testParam = H5Params(HttpConfig.MH5_URL + "ceshi/my-test", "测试记录")
                mActivity?.let {
                    NewH5Activity.start(mActivity, testParam)
                }
            }
            BANNER_CLICK -> {
                var linkUrl: String? = methodCall.argument<String>("linkUrl")
                linkUrl?.let {
                    link(linkUrl)
                }
            }
            CATEGORY_CLICK -> {
                mActivity?.let {
                    var tabName: String = methodCall.argument<String>("tabName")?:""
                    TestCategoryListActivity.start(mActivity!!, tabName)
                }
            }
            ADVERT_CLICK -> {
                var linkUrl: String? = methodCall.argument<String>("linkUrl")
                linkUrl?.let {
                    link(linkUrl)
                }
            }
            TEST_LIST_ITEM_CLICK -> {
                var linkUrl: String? = methodCall.arguments.toString()
                linkUrl?.let {
                    link(linkUrl)
                }
            }
            LOOK_ALL_TEST -> {
                mActivity?.let {
                    TestCategoryListActivity.start(mActivity!!)
                }
            }
        }
    }

    private fun link(linkUrl: String?) {
        if (null == linkUrl || linkUrl.isEmpty()) {
            return
        }
        if (linkUrl.startsWith("app")) {
            val uri = Uri.parse(linkUrl)
            if ("ceshi" == uri.host) {
                val id = uri.getQueryParameter("id")
//                YDLRouterManager.router(IYDLRouterConstant.ROUTER_TEST_DETAIL, YDLRouterParams().putExtra(IYDLRouterConstant.EXTRA_ID, id))
                TestsIn.getTestsImpl().testDetailH5(id)
            }
        } else if (linkUrl.startsWith("http")) {
            YDLRouterManager.router(
                    IYDLRouterConstant.ROUTER_H5_H5,
                    YDLRouterParams().putExtra(IYDLRouterConstant.EXTRA_URL, linkUrl), "")
        } else {
            YDLRouterManager.router(linkUrl)
        }
    }
}