package com.duben.microtribe.ui.widgets

import android.app.Activity
import android.app.Dialog
import android.text.Spannable
import android.view.Gravity
import android.view.View
import java.lang.ref.WeakReference

object DialogUtils {

    /**
     *
     * 普通弹框
     */
    fun showDialog(
        activity: Activity,
        content: Spannable,
        title: String = "提示",
        rightStr: String? = "确定",
        rightAction: DialogListener = object : DialogListener() {
            override fun onClick(dialog: Dialog?, v: View?) {
                dialog?.dismiss()
            }
        },
        leftStr: String? = "取消",
        leftAction: DialogListener = object : DialogListener() {
            override fun onClick(dialog: Dialog?, v: View?) {
                dialog?.dismiss()
            }
        }, gravity: Int = Gravity.CENTER
    ) {
        val weakReference = WeakReference(activity)
        val cdaa = CustomDialogAsApple(weakReference.get())
        cdaa.let {
            if (weakReference.get() == null || (weakReference.get() as Activity).isFinishing) return

            it.setTitle(title)
            it.setContent(content)
            it.setContentGravity(gravity)
            it.setLeft(leftStr)
            it.setRight(rightStr)
            it.show()
        }
        cdaa.setLeftClickListener(leftAction)
        cdaa.setRightClickListener(rightAction)

        leftAction.setDialog(cdaa)
        rightAction.setDialog(cdaa)
    }


    /**
     *
     * 普通弹框
     */
    fun showLeftDialog(
        activity: Activity,
        content: Spannable,
        title: String = "提示",
        rightStr: String? = "确定",
        rightAction: DialogListener = object : DialogListener() {
            override fun onClick(dialog: Dialog?, v: View?) {
                dialog?.dismiss()
            }
        },
        gravity: Int = Gravity.CENTER
    ) {
        val weakReference = WeakReference(activity)
        val cdaa = CustomDialogAsApple(weakReference.get())
        cdaa.let {
            if (weakReference.get() == null || (weakReference.get() as Activity).isFinishing) return

            it.setTitle(title)
            it.setContent(content)
            it.setContentGravity(gravity)
            it.setRight(rightStr)
            it.show()
        }
        cdaa.setRightClickListener(rightAction)

        rightAction.setDialog(cdaa)
    }

    /**
     *
     * 普通弹框
     */
    fun showDialog(
        activity: Activity,
        content: String? = "",
        title: String = "提示",
        rightStr: String? = "确定",
        rightAction: DialogListener = object : DialogListener() {
            override fun onClick(dialog: Dialog?, v: View?) {
                dialog?.dismiss()
            }
        },
        leftStr: String? = "取消",
        leftAction: DialogListener = object : DialogListener() {
            override fun onClick(dialog: Dialog?, v: View?) {
                dialog?.dismiss()
            }
        }, gravity: Int = Gravity.CENTER
    ) {
        val weakReference = WeakReference(activity)
        val cdaa = CustomDialogAsApple(weakReference.get())
        cdaa.let {
            if (weakReference.get() == null || (weakReference.get() as Activity).isFinishing) return

            it.setTitle(title)
            it.setContent(content)
            it.setContentGravity(gravity)
            it.setLeft(leftStr)
            it.setRight(rightStr)
            it.show()
        }
        cdaa.setLeftClickListener(leftAction)
        cdaa.setRightClickListener(rightAction)

        leftAction.setDialog(cdaa)
        rightAction.setDialog(cdaa)
    }
}