package com.develop.auth.ui

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.activity.viewModels
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.develop.auth.R
import com.develop.auth.databinding.ActivityMemberAuthBinding
import com.develop.food.base.common.LOGIN_TAG
import com.develop.food.base.router.Screens
import com.develop.food.base.router.navigateTo
import com.develop.food.base.ui.BaseActivity
import com.develop.food.base.utils.MmkvUtils

@Route(path = Screens.Auth.MEMBER)
class MemberAuthActivity : BaseActivity<ActivityMemberAuthBinding>() {

    @JvmField
    @Autowired(name = "enter_from_home")
    var enterFromHome: Boolean = false

    private val viewModel by viewModels<MemberViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ARouter.getInstance().inject(this)
        viewModel.enterFromHome = enterFromHome
        binding.ivUserIcon.setOnClickListener {
            supportFragmentManager
                .beginTransaction()
                .add(UserIconSelectDialog(), "UserIconDialog")
                .commitAllowingStateLoss()
        }
        binding.tvIconSelect.setOnClickListener {
            supportFragmentManager
                .beginTransaction()
                .add(UserIconSelectDialog(), "UserIconDialog")
                .commitAllowingStateLoss()
        }
        supportFragmentManager.registerFragmentLifecycleCallbacks(
            object : FragmentManager.FragmentLifecycleCallbacks() {
                override fun onFragmentAttached(
                    fm: FragmentManager,
                    f: Fragment,
                    context: Context
                ) {
                    if (f is MemberProfileFragment) {
                        binding.clUserIcon.visibility = View.VISIBLE
                    }
                }

                override fun onFragmentDetached(fm: FragmentManager, f: Fragment) {
                    if (f is MemberProfileFragment) {
                        binding.clUserIcon.visibility = View.GONE
                    }
                }
            }, false
        )
        binding.tvSkip.setOnClickListener {
            navigateTo(Screens.Main.ENTRANCE_CHOSEN)
            finish()
        }
        if (MmkvUtils.getBool(LOGIN_TAG)) {
            addFragment(
                R.id.fl_container,
                MemberProfileFragment()
            )
        }else {
            addFragment(
                R.id.fl_container,
                MemberLaunchFragment()
            )
        }
        viewModel.chooseAvatar.observe(this) {
            val index = it ?: 0
            val icon = if (index == 0) {
                R.drawable.ic_icon1
            }else if (index == 1) {
                R.drawable.ic_icon2
            }else if (index == 2) {
                R.drawable.ic_icon3
            }else if (index == 3) {
                R.drawable.ic_icon4
            }else if (index == 4) {
                R.drawable.ic_icon5
            }else {
                R.drawable.ic_icon6
            }
            binding.ivUserIcon.setImageResource(icon)
        }
    }

    fun getRootView(): View {
        return binding.flRoot
    }

    override fun createViewBinding(inflater: LayoutInflater): ActivityMemberAuthBinding {
        return ActivityMemberAuthBinding.inflate(inflater)
    }
}