package com.adealink.weparty.joinus.step2.comp

import androidx.lifecycle.LifecycleOwner
import com.adealink.frame.mvvm.view.ViewComponent
import com.adealink.frame.mvvm.viewmodel.viewModels
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.BaseViewComponent
import com.adealink.weparty.joinus.databinding.LayoutJoinUsStep2EditGenderBinding
import com.adealink.weparty.joinus.step2.viewmodel.Step2ViewModel
import com.adealink.weparty.joinus.viewmodel.JoinUsViewModelFactory
import com.adealink.weparty.module.profile.data.Gender
import com.adealink.weparty.R as APP_R

class EditGenderComp(
    lifecycleOwner: LifecycleOwner,
    private val binding: LayoutJoinUsStep2EditGenderBinding
) : BaseViewComponent(lifecycleOwner) {

    companion object {
        private const val TAG = "EditGenderComp"
    }

    private val editViewModel by viewModels<Step2ViewModel> { JoinUsViewModelFactory() }

    override fun initViews() {
        binding.btnMale.onClick {
            editViewModel.setGender(Gender.MALE)
        }
        binding.btnFemale.onClick {
            editViewModel.setGender(Gender.FEMALE)
        }
    }

    override fun observeViewModel() {
        editViewModel.editUserInfoLD.observe(viewLifecycleOwner) {
            updateUI(it?.gender)
        }
    }

    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)
            }
        }
    }

}