package com.adealink.weparty.account.register.fragment

import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.activityViewModels
import com.adealink.frame.aab.util.getCompatColor
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.util.onClick
import com.adealink.frame.util.statusBarHeight
import com.adealink.weparty.account.R
import com.adealink.weparty.account.databinding.FragmentRegisterSelectGenderBinding
import com.adealink.weparty.account.register.viewmodel.RegisterProfileViewModel
import com.adealink.weparty.account.viewModel.AccountViewModelFactory
import com.adealink.weparty.commonui.BaseFragment
import com.adealink.weparty.module.profile.data.Gender
import com.adealink.weparty.R as APP_R

class SelectGenderFragment : BaseFragment(R.layout.fragment_register_select_gender) {
    private val binding by viewBinding(FragmentRegisterSelectGenderBinding::bind)

    private val viewModel by activityViewModels<RegisterProfileViewModel> { AccountViewModelFactory() }

    override fun initViews() {
        super.initViews()
        binding.topBar.updateLayoutParams <ConstraintLayout.LayoutParams>{
            topMargin = activity.statusBarHeight()
        }
        binding.topBar.backCallback = {
            activity?.onBackPressed()
        }
        binding.clMale.onClick {
            viewModel.selectGender(Gender.MALE)
        }
        binding.clFemale.onClick {
            viewModel.selectGender(Gender.FEMALE)
        }
        binding.btnNext.onClick {
            nextStep()
        }

        viewModel.genderLD.value?.let {
            selectGender(it)
        }
    }

    override fun observeViewModel() {
        super.observeViewModel()
        viewModel.genderLD.observe(viewLifecycleOwner) {
            selectGender(it)
        }
    }

    private fun selectGender(gender: Gender) {
        when (gender) {
            Gender.UNKNOWN -> {
                binding.tvMale.setTextColor(getCompatColor(APP_R.color.color_FF1D2129))
                binding.tvMale.setBackgroundResource(R.drawable.account_register_gender_bg)
                binding.ivMale.setImageResource(R.drawable.account_male_ic)

                binding.tvFemale.setTextColor(getCompatColor(APP_R.color.color_FF1D2129))
                binding.tvFemale.setBackgroundResource(R.drawable.account_register_gender_bg)
                binding.ivFemale.setImageResource(R.drawable.account_female_ic)

                binding.btnNext.isEnabled = false
            }

            Gender.MALE -> {
                binding.tvMale.setTextColor(getCompatColor(APP_R.color.white))
                binding.tvMale.setBackgroundResource(R.drawable.account_register_gender_select_bg)
                binding.ivMale.setImageResource(R.drawable.account_male_select_ic)

                binding.tvFemale.setTextColor(getCompatColor(APP_R.color.color_FF1D2129))
                binding.tvFemale.setBackgroundResource(R.drawable.account_register_gender_bg)
                binding.ivFemale.setImageResource(R.drawable.account_female_ic)

                binding.btnNext.isEnabled = true
            }

            Gender.FEMALE -> {
                binding.tvMale.setTextColor(getCompatColor(APP_R.color.color_FF1D2129))
                binding.tvMale.setBackgroundResource(R.drawable.account_register_gender_bg)
                binding.ivMale.setImageResource(R.drawable.account_male_ic)

                binding.tvFemale.setTextColor(getCompatColor(APP_R.color.white))
                binding.tvFemale.setBackgroundResource(R.drawable.account_register_gender_select_bg)
                binding.ivFemale.setImageResource(R.drawable.account_female_select_ic)

                binding.btnNext.isEnabled = true
            }
        }
    }

    private fun nextStep() {

        viewModel.notifyGenderSelectFinish()
    }

}