package com.yidian.yac.newscontent.ui

import android.os.Bundle
import android.util.Log
import android.widget.FrameLayout
import cn.sharesdk.framework.Platform
import cn.sharesdk.framework.PlatformActionListener
import cn.sharesdk.onekeyshare.OnekeyShare
import com.google.gson.Gson
import com.yidian.common.XRouterPathConstants.Companion.NEWS_CONTENT
import com.yidian.common.base.BaseActivity
import com.yidian.common.utils.XLogUtil
import com.yidian.yac.newscontent.R
import com.yidian.yac.newscontent.bean.xbrid.XBridTestBean
import com.yidian.yac.newscontent.ui.webview.YiDianWebView
import com.yidian.xarc.xbrid.XBridManager
import com.yidian.xarc.xbrid.XWebView
import com.yidian.xpage.XPageViewProtocol
import kotlinx.android.synthetic.main.activity_news_content.*
import org.json.JSONObject
import java.util.HashMap


class NewsContentActivity: BaseActivity(), XPageViewProtocol {
    private lateinit var mWebView: XWebView

    override val contentViewId: Int
        get() = R.layout.activity_news_content

    override fun getXPageName(): String {
        return NEWS_CONTENT
    }

    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
        setWebView()
        setClickListener()
    }

    private fun setWebView(){
        val ydWebView = YiDianWebView(this)
        mWebView = XBridManager.getWebView(this, "testidenfier", ydWebView)
        val params = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)
        fly_webContainer.addView(mWebView, params)
        mWebView.isVerticalScrollBarEnabled = false
        mWebView.loadUrl("xbrid_paike_home?a=b&c=d")

        
    }

    private fun setClickListener(){
        btn_trans.setOnClickListener{
            val param = XBridTestBean("aaaaa", "22222")
            val gson = Gson()
            val params = gson.toJson(param)
            XLogUtil.e("zhb-yd", "传递参数：$params")
            XBridManager.postEvent("xsky-event-test", params, mWebView)
        }

        btn_share.setOnClickListener{
            val oks = OnekeyShare()
//            oks.setTitle("分享")
//            oks.setTitleUrl("http://sharesdk.cn")
            oks.text = "我是分享文本"
            oks.setImageUrl("https://hmls.hfbank.com.cn/hfapp-api/9.png")
//            oks.setUrl("http://sharesdk.cn")

            oks.callback = object : PlatformActionListener{
                override fun onComplete(platform: Platform, i: Int, hashMap: HashMap<String, Any>?) {
                    platform.name
                    XLogUtil.d("zhb-yd", "onComplete ------> 分享成功")
                    platform.isClientValid
                }

                override fun onCancel(platform: Platform, i: Int) {
                    XLogUtil.d("zhb-yd", "onCancel ------> 分享取消")
                }

                override fun onError(platform: Platform, i: Int, throwable: Throwable) {
                    XLogUtil.d("zhb-yd", "onError ------> 分享失败" + throwable.message)
                    throwable.message
                    throwable.printStackTrace()
                }

            }


            oks.show(this)
        }
    }



    override fun onDestroy() {
        super.onDestroy()
        XBridManager.backWebView(mWebView)
    }
}