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.ProfileModule
import com.adealink.weparty.module.profile.data.Gender
import com.adealink.weparty.profile.R
import com.adealink.weparty.profile.databinding.DialogEditGenderBinding

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
    }

    override fun initViews() {
        super.initViews()
        binding.btnMale.onClick {
            select(Gender.MALE)
        }
        binding.btnFemale.onClick {
            select(Gender.FEMALE)
        }
        binding.btnSave.onClick {
            save()
        }
        updateUI(Gender.map(ProfileModule.getMyUserInfo()?.gender))
    }

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

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

            Gender.UNKNOWN -> {

            }
        }
    }

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

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


    interface IEditGenderCallback {
        fun onEditGender(gender: Gender)
    }

}