package com.example.defenseapplication.view

import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.fragment.app.DialogFragment
import com.example.defenseapplication.R
import com.example.defenseapplication.databinding.DialogCustomBinding

class CustomDialogFragmentText : DialogFragment() {

    private lateinit var binding: DialogCustomBinding
    private var title: String = ""
    private var message: String = ""
    private var inputHint: String = ""
    private var inputText: String = ""
    private var negativeText: String = ""
    private var positiveText: String = ""
    private var showInput: Boolean = true
    private var onConfirmListener: ((String) -> Unit)? = null
    private var onDismissListener: (() -> Unit)? = null

    fun setTitle(title: String): CustomDialogFragmentText {
        this.title = title
        return this
    }

    fun setMessage(message: String): CustomDialogFragmentText {
        this.message = message
        return this
    }

    fun setInputHint(hint: String): CustomDialogFragmentText {
        this.inputHint = hint
        return this
    }

    fun setInputText(text: String): CustomDialogFragmentText {
        this.inputText = text
        return this
    }

    fun setNegativeText(text: String): CustomDialogFragmentText {
        this.negativeText = text
        return this
    }

    fun setPositiveText(text: String): CustomDialogFragmentText {
        this.positiveText = text
        return this
    }

    fun setShowInput(show: Boolean): CustomDialogFragmentText {
        this.showInput = show
        return this
    }

    fun setOnConfirmListener(listener: (String) -> Unit): CustomDialogFragmentText {
        this.onConfirmListener = listener
        return this
    }

    fun setOnDismissListener(listener: () -> Unit): CustomDialogFragmentText {
        this.onDismissListener = listener
        return this
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)
        dialog.window?.let {
            val params = it.attributes
            params.gravity = Gravity.CENTER
            params.width = WindowManager.LayoutParams.MATCH_PARENT
            params.height = WindowManager.LayoutParams.WRAP_CONTENT
            it.attributes = params
            it.setBackgroundDrawableResource(R.drawable.round_bg)
        }
        return dialog
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = DialogCustomBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (negativeText.isEmpty()) negativeText = getString(R.string.cancel)
        if (positiveText.isEmpty()) positiveText = getString(R.string.confirm)
        binding.tvTitle.text = title
        binding.tvMessage.text = message
        binding.etInput.hint = inputHint
        binding.etInput.setText(inputText)
        binding.btnNegative.text = negativeText
        binding.btnPositive.text = positiveText

        binding.etInput.visibility = if (showInput) View.VISIBLE else View.GONE

        binding.btnNegative.setOnClickListener {
            dismiss()
        }

        binding.btnPositive.setOnClickListener {
            val inputText = binding.etInput.text.toString()
            onConfirmListener?.invoke(inputText)
            dismiss()
        }
    }

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        onDismissListener?.invoke()
        onConfirmListener = null
        onDismissListener = null
    }
}