package com.ccwangluo.accelerator.ui.dialog

import android.content.Context
import android.graphics.Color
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.TextView
import com.ccwangluo.accelerator.R
import com.ccwangluo.accelerator.ui.web.AndroidActivityInterface
import com.ccwangluo.accelerator.ui.web.AndroidInterface
import com.just.agentweb.core.AgentWeb
import com.just.agentweb.core.client.DefaultWebClient
import com.xuexiang.xpage.base.XPageActivity
import com.xuexiang.xui.widget.dialog.materialdialog.CustomMaterialDialog
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog

class PrivacyDialog(
    var xPageActivity: XPageActivity,
    var privacy_url: String,
    var callback: (Boolean) -> Unit
) : CustomMaterialDialog(xPageActivity) {


    override fun getDialogBuilder(context: Context?): MaterialDialog.Builder {
        return MaterialDialog.Builder(context!!)
            .customView(R.layout.dialog_privacy, false);
    }


    override fun initViews(context: Context?) {
        val disagree = findViewById<TextView>(R.id.privacy_disagree)
        val agree = findViewById<TextView>(R.id.privacy_agree)
        disagree.setOnClickListener {
            callback(false);
            dialog.dismiss()
        }

        agree.setOnClickListener {
            callback(true)
            dialog.dismiss()
        }
    }

    init {
        val container = findViewById<FrameLayout>(R.id.privacy_webview)
        val agentWeb =
            createAgentWeb(xPageActivity, container, privacy_url)
        agentWeb.jsInterfaceHolder.addJavaObject("android", AndroidActivityInterface(xPageActivity, agentWeb!!))
        mDialog.setCancelable(false)
    }

    fun createAgentWeb(xPageActivity: XPageActivity, viewGroup: ViewGroup, url: String): AgentWeb {
        return AgentWeb.with(xPageActivity)
            .setAgentWebParent(
                viewGroup!!,
                -1,
                FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
                )
            )
            .useDefaultIndicator(Color.parseColor("#3CA4FD"), 3)
            .setOpenOtherPageWays(DefaultWebClient.OpenOtherPageWays.ASK)
            .createAgentWeb()
            .ready()
            .go(url)
    }
}