package com.adealink.weparty.module.profile.widget

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout
import com.adealink.weparty.R
import com.adealink.weparty.databinding.LayoutGenderViewBinding
import com.adealink.weparty.module.profile.data.Gender

@SuppressLint("SetTextI18n")
class GenderView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {

    private val binding = LayoutGenderViewBinding.inflate(LayoutInflater.from(context), this)

    init {
        initView()
    }

    private fun initView() {
        if (isInEditMode) {
            binding.root.setBackgroundResource(R.drawable.common_gender_female_bg)
            binding.ivGender.setImageResource(R.drawable.common_gender_female_tag_ic)
            binding.tvAge.text = "20"
            return
        }
        binding.root.setBackgroundResource(R.drawable.common_gender_male_bg)
        binding.ivGender.setImageResource(R.drawable.common_gender_male_tag_ic)
    }

    fun setGender(gender: Int?) {
        setGender(Gender.map(gender))
    }

    fun setGender(gender: Gender) {
        when (gender) {
            Gender.MALE -> {
                binding.root.setBackgroundResource(R.drawable.common_gender_male_bg)
                binding.ivGender.setImageResource(R.drawable.common_gender_male_tag_ic)
            }

            Gender.FEMALE,
            Gender.UNKNOWN -> {
                binding.root.setBackgroundResource(R.drawable.common_gender_female_bg)
                binding.ivGender.setImageResource(R.drawable.common_gender_female_tag_ic)
            }
        }
    }

    fun setAge(age: Int?) {
        if (age == null || age <= 0) {
            binding.tvAge.text = null
        } else {
            binding.tvAge.text = age.toString()
        }
    }

}