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.ext.isViewBindingValid
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.util.onClick
import com.adealink.weparty.account.R
import com.adealink.weparty.account.databinding.FragmentRegisterSelectGenderBinding
import com.adealink.weparty.account.register.Page
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.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.module.account.AccountModule
import com.adealink.weparty.module.profile.data.Gender
import com.adealink.weparty.util.applyImmersive
import com.opensource.svgaplayer.SVGACallback
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() }

    /**
     * 上一个性别
     */
    private var lastGender = Gender.MALE

    override fun initViews() {
        super.initViews()
        activity.applyImmersive { top ->
            binding.topBar.updateLayoutParams<ConstraintLayout.LayoutParams> {
                topMargin = top
            }
        }
        binding.topBar.backCallback = {
            AccountModule.logout(true)
        }
        binding.clMale.onClick {
            viewModel.setGender(Gender.MALE)
        }
        binding.clFemale.onClick {
            viewModel.setGender(Gender.FEMALE)
        }
        binding.btnNext.onClick {
            nextStep()
        }
        binding.ivMaleAnim.callback = object : SVGACallback {
            override fun onStart() {
                if (isViewBindingValid()) {
                    binding.ivMale.gone()
                }
            }
        }
        binding.ivFemaleAnim.callback = object : SVGACallback {
            override fun onStart() {
                if (isViewBindingValid()) {
                    binding.ivFemale.gone()
                }
            }
        }

        val editUserInfo = viewModel.getEditUserInfo()
        lastGender = editUserInfo.gender ?: Gender.UNKNOWN
        selectGender(lastGender)
    }

    override fun observeViewModel() {
        super.observeViewModel()
        viewModel.editUserInfoLD.observe(viewLifecycleOwner) {
            selectGender(it.gender ?: Gender.UNKNOWN)
        }
    }

    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.tvFemale.setTextColor(getCompatColor(APP_R.color.color_FF1D2129))
                binding.tvFemale.setBackgroundResource(R.drawable.account_register_gender_bg)

                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.ivMaleAnim.show()
                binding.ivMaleAnim.startAnimation()

                binding.tvFemale.setTextColor(getCompatColor(APP_R.color.color_FF1D2129))
                binding.tvFemale.setBackgroundResource(R.drawable.account_register_gender_bg)
                binding.ivFemale.show()
                binding.ivFemaleAnim.gone()

                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.show()
                binding.ivMaleAnim.gone()

                binding.tvFemale.setTextColor(getCompatColor(APP_R.color.white))
                binding.tvFemale.setBackgroundResource(R.drawable.account_register_gender_select_bg)
                binding.ivFemaleAnim.show()
                binding.ivFemaleAnim.startAnimation()

                binding.btnNext.isEnabled = true
            }
        }
    }

    private fun nextStep() {
        val editUserInfo = viewModel.getEditUserInfo()
        if (lastGender != editUserInfo.gender) {
            //更换了性别,重置名字和头像
            editUserInfo.nickname = null
            editUserInfo.avatar = null
        }
        viewModel.goPage(Page.CompleteUserInfo)
    }

}