package com.adealink.weparty.profile.dynamic

import androidx.activity.viewModels
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.router.Router
import com.adealink.frame.router.annotation.BindExtra
import com.adealink.frame.router.annotation.RouterUri
import com.adealink.weparty.commonui.BaseActivity
import com.adealink.weparty.commonui.toast.util.showFailedToast
import com.adealink.weparty.module.profile.Profile
import com.adealink.weparty.module.profile.data.UserInfo
import com.adealink.weparty.profile.databinding.ActivityDynamicBinding
import com.adealink.weparty.profile.dynamic.data.DynamicDetailData
import com.adealink.weparty.profile.dynamic.fragment.photo.PhotoDynamicFragment
import com.adealink.weparty.profile.dynamic.fragment.video.VideoDynamicFragment
import com.adealink.weparty.profile.dynamic.viewmodel.DynamicViewModel

@RouterUri(
    path = [
        Profile.Dynamic.PATH
    ],
    desc = "动态详情"
)
class DynamicDetailActivity : BaseActivity() {

    @BindExtra(Profile.Dynamic.EXTRA_DYNAMIC_ID)
    var dynamicId: String? = null

    @BindExtra(Profile.Dynamic.EXTRA_DYNAMIC_DATA)
    var dynamicData: DynamicDetailData? = null

    @BindExtra(Profile.Dynamic.EXTRA_DYNAMIC_USERINFO)
    var dynamicUserInfo: UserInfo? = null

    private val binding by viewBinding(ActivityDynamicBinding::inflate)

    private val dynamicViewModel by viewModels<DynamicViewModel>()

    override fun onBeforeCreate() {
        super.onBeforeCreate()
        Router.bind(this)
    }

    override fun initViews() {
        super.initViews()
        setContentView(binding.root)

    }

    override fun loadData() {
        super.loadData()
        val dynamicId = dynamicId
        val dynamicData = dynamicData
        if (dynamicData != null) {
            dynamicViewModel.setDynamic(dynamicData)
            inflateDynamicPage(dynamicData)
        } else if (!dynamicId.isNullOrEmpty()) {
            dynamicViewModel.loadDynamic(dynamicId).observe(this) { rlt ->
                showFailedToast(rlt)
            }
        }
    }

    override fun observeViewModel() {
        super.observeViewModel()
        dynamicViewModel.dynamicDetailLD.observe(this) { dynamicData ->
            inflateDynamicPage(dynamicData)
        }
    }

    private fun inflateDynamicPage(dynamicData: DynamicDetailData) {
        if (dynamicData.isVideo()) {
            inflateVideoFragment()
        } else {
            inflatePhotoFragment()
        }
    }

    private fun inflatePhotoFragment() {
        var fragment =
            supportFragmentManager.findFragmentByTag(PhotoDynamicFragment.TAG) as? PhotoDynamicFragment
        if (fragment?.isAdded == true) {
            return
        }

        fragment = fragment ?: PhotoDynamicFragment()
        supportFragmentManager.beginTransaction()
            .replace(binding.flContent.id, fragment, PhotoDynamicFragment.TAG)
            .commitAllowingStateLoss()
    }

    private fun inflateVideoFragment() {
        var fragment =
            supportFragmentManager.findFragmentByTag(VideoDynamicFragment.TAG) as? VideoDynamicFragment
        if (fragment?.isAdded == true) {
            return
        }

        fragment = fragment ?: VideoDynamicFragment()
        supportFragmentManager.beginTransaction()
            .replace(binding.flContent.id, fragment, VideoDynamicFragment.TAG)
            .commitAllowingStateLoss()
    }

}