package com.secspace.lib.common.function

import android.app.AlertDialog
import android.content.Context
import android.content.DialogInterface
import android.os.Build
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.Button
import com.secspace.lib.common.R
import com.secspace.lib.common.function.ActivationHelper.ActivationListener
import com.secspace.lib.common.function.ActivationHelper.active
import com.secspace.lib.common.utils.IntentUtil

class BuyCodeDialog private constructor(val mContext: Context) : DialogInterface.OnDismissListener, View.OnClickListener {
    private var mDialog: AlertDialog? = null
    private var btnAccept: Button? = null
    private var closeAccept: Button? = null
    private var layout: View? = null
    fun show() {
        if (mDialog == null) {
            initView()
            mDialog = AlertDialog.Builder(mContext)
                    .setView(layout)
                    .setCancelable(false)
                    .create()
            val window = mDialog?.window
            window?.setBackgroundDrawableResource(R.drawable.dialog_corner_bg)
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//                window?.setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY)
//            } else {
//                window?.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT)
//            }
        }
        if (!mDialog!!.isShowing) {
            try {
                mDialog!!.show()
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

    private fun initView() {
        val inflater = LayoutInflater.from(mContext)
        layout = inflater.inflate(R.layout.buycode_layout, null)
        btnAccept = layout?.findViewById(R.id.btn_accept)
        btnAccept?.setOnClickListener(this)
        closeAccept = layout?.findViewById(R.id.btn_exit)
        closeAccept?.setOnClickListener(this)

    }

    fun dismiss() {
        if (mDialog != null && mDialog!!.isShowing) {
            mDialog!!.dismiss()
        }
    }

    override fun onDismiss(dialog: DialogInterface) {
        mDialog = null
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.btn_accept -> {
                dismiss()
                WorkManager.removeForbidden(mContext)
                active(mContext, object : ActivationListener {
                    override fun activationFailed() {}
                    override fun activated() {
                        IntentUtil.startEduLauncher(mContext)
                    }
                }, true)
            }
            R.id.btn_exit -> {
                dismiss()
                WorkManager.removeForbidden(mContext)
            }
        }
    }

    companion object {
        @Volatile
        private var instance: BuyCodeDialog? = null

        fun getInstance(context: Context): BuyCodeDialog {
            if (instance == null) {
                synchronized(BuyCodeDialog::class) {
                    if (instance == null) {
                        instance = BuyCodeDialog(context)
                    }
                }
            }
            return instance!!
        }
    }
}