package com.ccwangluo.accelerator.ui.view
import android.content.Context
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.ccwangluo.cc_quickly.R
import com.just.agentweb.core.AgentWeb
import com.just.agentweb.core.client.DefaultWebClient
import com.xuexiang.xui.widget.dialog.materialdialog.CustomMaterialDialog
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog

class PrivacyDialog(
    var fragment: Fragment,
    var privacy_url: String,
    var callback: (Boolean) -> Unit
) : CustomMaterialDialog(fragment.context) {


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


    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(fragment, container, privacy_url)
    }

    fun createAgentWeb(fragment: Fragment?, viewGroup: ViewGroup, url: String): AgentWeb {
        return AgentWeb.with(fragment!!)
            .setAgentWebParent(
                viewGroup!!,
                -1,
                FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
                )
            )
            .useDefaultIndicator(-1, 3)
            .setOpenOtherPageWays(DefaultWebClient.OpenOtherPageWays.ASK)
            .createAgentWeb()
            .ready()
            .go(url)
    }
}