package com.swago.baseswago.dialog

import android.os.Bundle
import android.view.Gravity
import android.view.View
import com.swago.baseswago.databinding.DialogChoiceBinding
import com.swago.baseswago.util.NoDoubleClickListener

class ChoiceDialogFragment : BaseXDFragment<DialogChoiceBinding>() {


    var cancelFun:(() -> Unit)? = null
    var sureFun:(() -> Unit)? = null

    init {
        setCanCancel(false)
        setGravity(Gravity.CENTER)
        setDimAmount(0.5f)
    }

    companion object{
        fun newInstance(content:String,title:String="",lefChoiceText:String="",rightChoiceText:String=""): ChoiceDialogFragment {
            val args = Bundle()
            args.putString("content",content)
            args.putString("title",title)
            args.putString("lefChoiceText",lefChoiceText)
            args.putString("rightChoiceText",rightChoiceText)
            val fragment = ChoiceDialogFragment()
            fragment.arguments = args
            return fragment
        }
    }


    override fun initOther() {
        arguments?.let {
            val content = it.getString("content")
            val title = it.getString("title")
            val lefChoiceText = it.getString("lefChoiceText")
            val rightChoiceText = it.getString("rightChoiceText")

            binding.tvContent.text = content
            if (title.isNullOrEmpty()){
                binding.tvTitle.visibility = View.GONE
            }else{
                binding.tvTitle.text = title
            }
            if (!lefChoiceText.isNullOrEmpty()){
                binding.tvCancel.text = lefChoiceText
            }
            if (!rightChoiceText.isNullOrEmpty()){
                binding.tvCancel.text = rightChoiceText
            }

            binding.tvCancel.setOnClickListener(object:NoDoubleClickListener(){
                override fun onClick() {
                    cancelFun?.invoke()
                    dismissAllowingStateLoss()
                }
            })

            binding.tvSure.setOnClickListener(object:NoDoubleClickListener(){
                override fun onClick() {
                    sureFun?.invoke()
                    dismissAllowingStateLoss()
                }
            })
        }
    }

    override fun initLiveData() {

    }
}