package com.adealink.weparty.profile.edit.dialog

import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.widget.BottomDialogFragment
import com.adealink.weparty.module.profile.data.Gender
import com.adealink.weparty.profile.R
import com.adealink.weparty.profile.databinding.DialogEditGenderBinding
import com.adealink.weparty.R as APP_R

class EditGenderDialog : BottomDialogFragment(R.layout.dialog_edit_gender) {

    private val binding by viewBinding(DialogEditGenderBinding::bind)
    private var callback: IEditGenderCallback? = null

    private var selectedGender: Gender? = null

    fun setCallback(callback: IEditGenderCallback?) {
        this.callback = callback
    }

    fun setGender(gender: Gender?) {
        selectedGender = gender
    }

    override fun initViews() {
        super.initViews()
        binding.btnMale.onClick {
            select(Gender.MALE)
        }
        binding.btnFemale.onClick {
            select(Gender.FEMALE)
        }
        binding.btnSave.onClick {
            save()
        }
        updateUI(selectedGender)
    }

    private fun updateUI(gender: Gender?) {
        when (gender) {
            Gender.MALE -> {
                binding.btnMale.setBackgroundResource(APP_R.drawable.profile_edit_gender_selected_bg)
                binding.btnFemale.setBackgroundResource(APP_R.drawable.profile_edit_gender_bg)
            }

            Gender.FEMALE -> {
                binding.btnMale.setBackgroundResource(APP_R.drawable.profile_edit_gender_bg)
                binding.btnFemale.setBackgroundResource(APP_R.drawable.profile_edit_gender_selected_bg)
            }

            else -> {
                binding.btnMale.setBackgroundResource(APP_R.drawable.profile_edit_gender_bg)
                binding.btnFemale.setBackgroundResource(APP_R.drawable.profile_edit_gender_bg)
            }
        }
    }

    private fun select(gender: Gender) {
        selectedGender = gender
        updateUI(gender)
    }

    private fun save() {
        selectedGender?.let {
            callback?.onEditGender(it)
        }
        dismiss()
    }


    interface IEditGenderCallback {
        fun onEditGender(gender: Gender)
    }

}