package com.adealink.weparty.playmate.detail

import androidx.activity.viewModels
import com.adealink.frame.aab.util.getCompatDimension
import com.adealink.frame.base.fastLazy
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.frame.util.naviBarHeight
import com.adealink.weparty.commonui.BaseActivity
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.commonui.toast.util.showFailedToast
import com.adealink.weparty.module.order.OrderModule
import com.adealink.weparty.module.playmate.Playmate
import com.adealink.weparty.module.playmate.event.PlaymateStatEvent
import com.adealink.weparty.module.profile.ProfileModule
import com.adealink.weparty.playmate.R
import com.adealink.weparty.playmate.databinding.ActivityPlaymateDetailBinding
import com.adealink.weparty.playmate.detail.comp.PlaymateDetailBottomComp
import com.adealink.weparty.playmate.detail.comp.PlaymateDetailEventComp
import com.adealink.weparty.playmate.detail.comp.PlaymateDetailHeaderComp
import com.adealink.weparty.playmate.detail.comp.PlaymateToolBarViewComp
import com.adealink.weparty.playmate.detail.viewmodel.PlaymateDetailViewModel
import com.adealink.weparty.playmate.stat.ViewPlaymateEvent
import com.adealink.weparty.playmate.viewmodel.PlaymateViewModelFactory

@RouterUri(
    path = [Playmate.Detail.PATH],
    desc = "陪玩详情"
)
class PlaymateDetailActivity : BaseActivity() {

    @BindExtra(Playmate.Detail.EXTRA_PLAYMATE_ID)
    var playmateID: String? = null

    @BindExtra(Playmate.Detail.EXTRA_PLAYMATE_NAME)
    var playmateName: String? = null

    private val binding by viewBinding(ActivityPlaymateDetailBinding::inflate)
    private val detailViewModel by viewModels<PlaymateDetailViewModel> { PlaymateViewModelFactory() }
    private val followViewModel by fastLazy { ProfileModule.getFollowViewModel(this) }
    private val blackViewModel by fastLazy { ProfileModule.getBlackViewModel(this) }
    private val detailFragment by fastLazy { PlaymateDetailFragment() }

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

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

    override fun initComponents() {
        super.initComponents()
        PlaymateToolBarViewComp(this, binding).attach()
        PlaymateDetailHeaderComp(this, binding.headerLayout).attach()
        PlaymateDetailBottomComp(this, binding.vBottom).attach()
        PlaymateDetailEventComp(this).attach()
    }

    override fun loadData() {
        super.loadData()
        OrderModule.queryDiscountInfo()
        detailViewModel.setPlaymate(playmateID, playmateName)
        detailViewModel.loadPlaymateDetail(playmateID)
    }

    override fun observeViewModel() {
        super.observeViewModel()
        detailViewModel.detailRltLD.observe(this) { rlt ->
            showFailedToast(rlt)
        }
        detailViewModel.detailLD.observe(this) { detail ->
            detail ?: return@observe
            if (detail.uid != ProfileModule.getMyUid()) {
                blackViewModel?.isBlack(detail.uid)
                followViewModel?.isFollow(detail.uid)
            }
            PlaymateStatEvent(PlaymateStatEvent.Action.VISIT).apply {
                id to detail.uid
            }.send()
            ViewPlaymateEvent().apply {
                userId to detail.uid
            }.send()
        }

    }

    private fun inflateDetailFragment() {
        if (detailFragment.isAdded) {
            return
        }
        detailFragment.setBottomMargin(naviBarHeight() + getCompatDimension(R.dimen.playmate_detail_bottom_bar_height).toInt() + 48.dp())
        supportFragmentManager.beginTransaction()
            .replace(binding.flContent.id, detailFragment, Playmate.Detail.PATH)
            .commitAllowingStateLoss()
    }
}