package com.ydl.js_module.listener

import android.net.Uri
import android.text.TextUtils
import android.util.Log
import com.alibaba.android.arouter.launcher.ARouter
import com.ydl.ydl_router.manager.YDLRouterManager
import com.ydl.ydl_router.manager.YDLRouterParams
import com.yidianling.ydlcommon.BuildConfig
import com.yidianling.ydlcommon.custom.router.IYDLRouterConstant

/**
 * @author yuanWai
 * @描述:h5与本地交互（参数都为params的目的是，方便反射调用，只所以这么设计是因为方便，加方法直接改这个类就好）
 * 方法名为<host_path> 例如：test/detail 方法名为：test_detail 参数统一为 params
 * @Copyright Copyright (c) 2018
 * @Company 壹点灵
 * @date 2018/12/5
 */
class JSInterfaceImpl : IJSInterface{

    private val TAG =  JSInterfaceImpl::class.java.name

    override fun test_detail(params: String?) {
        if (TextUtils.isEmpty(params)){
            return
        }
        val uri = Uri.parse(params)
        val test_items_id = uri.getQueryParameter("test_items_id")
        if (TextUtils.isEmpty(test_items_id)){
            return
        }
        Log.e("test_detail", "--------params=$params")
        YDLRouterManager.router(
            IYDLRouterConstant.ROUTER_H5_H5,
                YDLRouterParams().putExtra(IYDLRouterConstant.EXTRA_URL, BuildConfig.MH5_URL+"ceshi/"+test_items_id),"")
    }

    override fun test_result(params: String?) {
        if (TextUtils.isEmpty(params)){
            return
        }
        val uri = Uri.parse(params)
        val test_items_id = uri.getQueryParameter("test_items_id")
        if (TextUtils.isEmpty(test_items_id)){
            return
        }
        YDLRouterManager.router(
            IYDLRouterConstant.ROUTER_H5_H5,
                YDLRouterParams().putExtra(IYDLRouterConstant.EXTRA_URL, BuildConfig.MH5_URL+"ceshi/result/"+test_items_id),"")
    }

    override fun test_pay(params: String?) {
        if (TextUtils.isEmpty(params)){
            return
        }
        val uri = Uri.parse(params)
        val weiMoney = uri.getQueryParameter("weiMoney")
        val redPackArray = uri.getQueryParameter("redPackArray")
        val payId = uri.getQueryParameter("payId")
        val test_items_id = uri.getQueryParameter("test_items_id")
        val orderId = uri.getQueryParameter("orderId")
        if (TextUtils.isEmpty(weiMoney) || TextUtils.isEmpty(payId) || TextUtils.isEmpty(test_items_id)){
            return
        }

        val url = StringBuffer(BuildConfig.MH5_URL)
        url.append("ceshi/")
        url.append(test_items_id)
        url.append("?payId=")
        url.append(payId)
        url.append("&couponMoney=")
        url.append(redPackArray)
        url.append("&orderId=")
        url.append(orderId)

        YDLRouterManager.router(
            IYDLRouterConstant.ROUTER_H5_H5,
                YDLRouterParams().putExtra(IYDLRouterConstant.EXTRA_URL, Uri.encode(url.toString())),"")
    }

    override fun test_list(params: String?) {
        if (!TextUtils.isEmpty(params)){
            val uri = Uri.parse(params)
            val tab = uri.getQueryParameter("tab")
            if (TextUtils.isEmpty(tab)){
                ARouter.getInstance().build("/ceshi/category_list").navigation()
                return
            }
            ARouter.getInstance().build("/ceshi/category_list").withString("tab",tab).navigation()
        }else{
            ARouter.getInstance().build("/ceshi/category_list").navigation()
        }
    }
}