package com.duben.microtribe.ui.activitys

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.text.TextUtils
import android.view.KeyEvent
import android.view.View
import android.webkit.WebView
import android.webkit.WebViewClient
import com.duben.microtribe.R
import com.duben.microtribe.manager.DownloadApkManager
import com.duben.microtribe.ui.activitys.base.BaseActivity
import com.duben.microtribe.utils.CacheUtil
import kotlinx.android.synthetic.main.activity_web.*
import kotlinx.android.synthetic.main.web_header_layout.*

/**
 * 描述：WebActivity
 * 作者：孟崔广
 * 时间：2020/3/3 10:47
 * 邮箱：mengcga@163.com
 */
class WebActivity : BaseActivity(), View.OnClickListener {

    private var url: String? = null
    private var title: String? = null

    override fun getContentViewLayoutID(): Int {
        return R.layout.activity_web
    }

    override fun initViewsAndEvents() {
        pb_web.progress = 0
        if (TextUtils.isEmpty(title)) {
            tv_activity_title.text = "推荐"
        } else {
            tv_activity_title.text = title
        }

        if (!TextUtils.isEmpty(url)
                && url!!.contains(".apk")) {
//            DownloadApkManager.downloadApk(this,url!!)
            return
        }

        if (!TextUtils.isEmpty(url)
                && url!!.contains("pgyer")) {
            val uri = Uri.parse(url)
            val intent = Intent(Intent.ACTION_VIEW, uri)
            startActivity(intent)
            finish()
            return
        }

        if (!TextUtils.isEmpty(url)) {
            browser_layout.webView?.webViewClient = object : WebViewClient() {
                override fun shouldOverrideUrlLoading(
                        view: WebView,
                        url: String
                ): Boolean {
                    if (url.contains(".apk")) {
//                        DownloadApkManager.downloadApk(this@WebActivity, url)
                        return true
                    }

                    if (url.contains("pgyer") || url.contains("weixin://wap/pay") || url.contains(
                                    "alipays://platformapi/startApp")) {
                        val uri = Uri.parse(url)
                        val intent = Intent(Intent.ACTION_VIEW, uri)
                        startActivity(intent)
                        return true
                    }
                    return super.shouldOverrideUrlLoading(view, url)
                }

            }
            browser_layout.loadUrl(url)
        } else {
            showToast("获取URL地址失败")
            finish()
        }

        tv_activity_back.setOnClickListener(this)
        iv_activity_quit.setOnClickListener(this)
    }

    override fun getBundleExtras(extras: Bundle?) {
        super.getBundleExtras(extras)
        extras?.let {
            title = it.getString(WEB_TITLE, "推荐")
            url = it.getString(WEB_URL, "")
        }

    }

    override fun onResume() {
        super.onResume()
//        DownloadApkManager.tryOnceInstallApk()
    }

    override fun onDestroy() {
//        DownloadApkManager.destroy()

        super.onDestroy()

        CacheUtil.clearAllCache()

        if (browser_layout != null) {
            browser_layout.destroyDrawingCache()
            browser_layout.clearWebView()
        }

    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK && browser_layout.canGoBack()) {
            browser_layout.goBack() // 返回前一个页面
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    override fun isApplyKitKatTranslucency() = false

    companion object {
        const val WEB_TITLE = "web_title"
        const val WEB_URL = "web_url"
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.tv_activity_back -> {
                if (browser_layout != null && browser_layout.canGoBack()) {
                    browser_layout.goBack() // 返回前一个页面
                } else {
                    finish()
                }
            }
            R.id.iv_activity_quit -> {
                finish()
            }
        }
    }
}