package com.develop.auth.ui

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
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.event.TokenInvalidateEvent
import com.develop.food.base.ext.background_drawable
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.GlobalToast
import com.develop.food.base.utils.MmkvUtils
import com.develop.food.base.utils.isNightTheme
import org.greenrobot.eventbus.Subscribe

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

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

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

    private val viewModel by viewModels<MemberViewModel>()
    private val userIconSelectDialog by lazy {
        UserIconSelectDialog()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ARouter.getInstance().inject(this)
        viewModel.enterFromHome = enterFromHome
        binding.ivUserIcon.setOnClickListener {
            userIconSelectDialog.showDialog(supportFragmentManager, "UserIconDialog")
        }
        binding.tvIconSelect.setOnClickListener {
            userIconSelectDialog.showDialog(supportFragmentManager, "UserIconDialog")
        }
        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 (isTokenOut) {
            addFragment(
                R.id.fl_container,
                MemberLoginFragment()
            )
        } else {
            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.background_drawable = icon
        }

        if (isNightTheme()) {
            val param = binding.ivBanner.layoutParams as FrameLayout.LayoutParams
            param.width = FrameLayout.LayoutParams.MATCH_PARENT
            param.height = FrameLayout.LayoutParams.MATCH_PARENT
            binding.ivBanner.layoutParams = param
        }
    }

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

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

    @Subscribe
    fun onTokenInvalidateEvent(event: TokenInvalidateEvent) {
        GlobalToast.showToast(getString(R.string.login_expire_tips))
        supportFragmentManager
            .beginTransaction()
            .replace(R.id.fl_container, MemberLoginFragment())
            .commitAllowingStateLoss()
    }

}