package com.example.defenseapplication.view

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.example.defenseapplication.R
import com.example.defenseapplication.databinding.DialogAvatarSelectBinding

class AvatarSelectDialog(context: Context) {

    private val dialog: Dialog = Dialog(context)
    private val binding: DialogAvatarSelectBinding
    private var onTakePhotoListener: (() -> Unit)? = null
    private var onSelectAlbumListener: (() -> Unit)? = null

    init {
        binding = DialogAvatarSelectBinding.inflate(LayoutInflater.from(context))
        dialog.setContentView(binding.root)
        dialog.window?.apply {
            setGravity(Gravity.BOTTOM)
            setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT)
            setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            setDimAmount(0.5f)
        }

        initListeners()
    }

    private fun initListeners() {
        binding.itemTakePhoto.setOnClickListener {
            onTakePhotoListener?.invoke()
            dialog.dismiss()
        }

        binding.itemSelectAlbum.setOnClickListener {
            onSelectAlbumListener?.invoke()
            dialog.dismiss()
        }

        binding.itemCancel.setOnClickListener {
            dialog.dismiss()
        }
    }

    fun setOnTakePhotoListener(listener: () -> Unit): AvatarSelectDialog {
        this.onTakePhotoListener = listener
        return this
    }

    fun setOnSelectAlbumListener(listener: () -> Unit): AvatarSelectDialog {
        this.onSelectAlbumListener = listener
        return this
    }

    fun show() {
        dialog.show()
    }

    fun dismiss() {
        dialog.dismiss()
    }
}