package com.yidian.shenghuoquan.newscontent.ui

import android.annotation.SuppressLint
import android.app.Activity
import android.content.ClipData
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.webkit.ValueCallback
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
import com.orhanobut.hawk.Hawk
import com.yidian.common.HawkConfig
import com.yidian.common.XRouterPathConstants.Companion.SUPPORT
import com.yidian.common.base.BaseActivity
import com.yidian.common.extensions.initTitleBar
import com.yidian.shenghuoquan.newscontent.R
import com.yidian.shenghuoquan.newscontent.databinding.ActivityProtocolBinding
import com.yidian.xarc.xbrid.XBridManager
import com.yidian.xarc.xbrid.XWebView


class ProductSupportActivity : BaseActivity<ActivityProtocolBinding>() {

  override fun createViewBinding(): ActivityProtocolBinding {
    return ActivityProtocolBinding.inflate(layoutInflater)
  }

  override fun getXPageName(): String {
    return SUPPORT
  }

  private val FILE_CHOOSER_RESULT_CODE = 666
  private var uploadMessageAboveL: ValueCallback<Array<Uri>>? = null

  private lateinit var webView: XWebView

  @SuppressLint("SetJavaScriptEnabled")
  override fun init(savedInstanceState: Bundle?) {
    super.init(savedInstanceState)
    initTitle()
    webView = XBridManager.getWebView(this, "testidenfier", viewBind.wvProtocol)
    webView.settings.javaScriptEnabled = true
    val webViewClient: WebViewClient = object : WebViewClient() {
      override fun shouldOverrideUrlLoading(view: WebView, url: String?): Boolean {
        super.shouldOverrideUrlLoading(view, url)
        view.loadUrl(url)
        return true
      }
    }
    webView.webViewClient = webViewClient
    webView.webChromeClient = object : WebChromeClient() {
      override fun onShowFileChooser(
        webView: WebView?,
        filePathCallback: ValueCallback<Array<Uri>>?,
        fileChooserParams: FileChooserParams?
      ): Boolean {
        uploadMessageAboveL = filePathCallback
        openImageChooserActivity()
        return true
      }
    }
    webView.settings.javaScriptEnabled = true
    webView.settings.domStorageEnabled = true
    val url = "https://support.qq.com/product/304954"
    val nickname = Hawk.get(HawkConfig.Nickname, "")
    val profileUrl = Hawk.get(HawkConfig.ProfileUrl, "")
    val username = Hawk.get(HawkConfig.Username, "")
    val postData = "nickname=$nickname&avatar=$profileUrl&openid=$username"
    webView.postUrl(url, postData.toByteArray())
  }

  private fun initTitle() {
    initTitleBar(viewBind.include.toolbar, viewBind.include.tvTitle, resources.getString(R.string.my_staff))
  }

  private fun openImageChooserActivity() {
    val i = Intent(Intent.ACTION_GET_CONTENT)
    i.addCategory(Intent.CATEGORY_OPENABLE)
    i.type = "image/*"
    startActivityForResult(Intent.createChooser(i, "Image Chooser"), FILE_CHOOSER_RESULT_CODE)
  }

  private fun onActivityResultAboveL(requestCode: Int, resultCode: Int, intent: Intent?) {
    if (requestCode != FILE_CHOOSER_RESULT_CODE || uploadMessageAboveL == null) return
    var results: Array<Uri>? = null
    if (resultCode == Activity.RESULT_OK) {
      if (intent != null) {
        val dataString = intent.dataString
        val clipData: ClipData? = intent.clipData
        if (clipData != null) {
          results = Array(clipData.itemCount) {
            Uri.EMPTY
          }
          for (i in 0 until clipData.itemCount) {
            val item: ClipData.Item = clipData.getItemAt(i)
            results[i] = item.uri
          }
        }
        if (dataString != null) results = arrayOf(Uri.parse(dataString))
      }
    }
    uploadMessageAboveL?.onReceiveValue(results)
    uploadMessageAboveL = null
  }

  override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == FILE_CHOOSER_RESULT_CODE) {
      if (uploadMessageAboveL != null) {
        onActivityResultAboveL(requestCode, resultCode, data)
      }
    }
  }

}
