package com.zj365.mime.act

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.GsonUtils
import com.xty.base.act.BaseListAct
import com.xty.common.BuildConfig
import com.xty.common.WxUtils
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.event.HealthDataChangeEvent
import com.xty.common.event.IntegralEvent
import com.xty.common.util.CommonUtils
import com.xty.common.util.ViewScreenshotUtils
import com.zj365.mime.R
import com.zj365.mime.adapter.MyIntegralAdapter
import com.zj365.mime.databinding.ActMyIntegralBinding
import com.zj365.mime.vm.ScoreDetailVm
import com.zj365.mime.weight.InviteFriendShareDialog
import com.xty.network.model.SettingBean
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 我的积分
 */
@Route(path = ARouterUrl.MY_INTEGRAL)
class MyIntegralAct : BaseListAct<ScoreDetailVm>() {

    private val binding by lazy { ActMyIntegralBinding.inflate(layoutInflater) }
    override fun setLayout(): View = binding.root

    private val adapter by lazy { MyIntegralAdapter() }

    var user: SettingBean.User? = null

    override fun initData() {
        super.initData()
        user = intent.extras!!.getSerializable("user") as SettingBean.User
    }

    override fun initView() {
        super.initView()
        binding.title.mTvTitle.text = getString(R.string.my_score)
        binding.title.mIvBack.setOnClickListener { finish() }

        //点击跳转积分明细
        binding.tvIntegralDetail.setOnClickListener {
            RouteManager.goAct(ARouterUrl.SCORE)
        }

        //点击跳转积分规则
        binding.mRule.setOnClickListener {
            RouteManager.goAct(ARouterUrl.SCORE_RULE)
        }
    }

    override fun initAdapter() {
        setRecycleRefresh(binding.rvIntegralList, binding.mRefresh)
        binding.rvIntegralList.layoutManager = LinearLayoutManager(this)
        binding.rvIntegralList.adapter = adapter

        adapter.addChildClickViewIds(R.id.tvIntegralStatus)
        adapter.setOnItemChildClickListener { _, view, position ->
            val myIntegralBean = adapter.data[position]
            if (view.id == R.id.tvIntegralStatus) {
                when (myIntegralBean.type) {
                    15 -> {//添加饮食记录
                        RouteManager.goAct(ARouterUrl.DATAMANAGE_YSHI_UPLOADING_ACT)
                    }

                    4 -> {//日报分享
                        mViewModel.getAllInfo(CommonUtils.getMyUserId().toString())
                    }

                    16 -> {//邀请好友
                        user?.let {
                            InviteFriendShareDialog(
                                this@MyIntegralAct,
                                1,
                                it,
                                null
                            ).showDialog()
                        }
                    }

                    7 -> {//完善个人资料
                        RouteManager.goAct(ARouterUrl.BASE_INFO_CHANGE)
                    }

                    else -> {
                        bundle.clear()
                        bundle.putString("data", GsonUtils.toJson(myIntegralBean))
                        RouteManager.goAct(ARouterUrl.ADD_INTEGRAL, bundle)
                    }
                }
            }
        }
    }

    override fun loadData() {
        mViewModel.getUserIntegralInfoRequest(CommonUtils.getMyUserId().toString())
    }

    override fun liveObserver() {
        mViewModel.getUserIntegralInfoData.observe(this) {
            val data = it.data
            setDate(adapter, data)

            mViewModel.getScore(1)
        }

        mViewModel.scoreLive.observe(this) {
            binding.tvInAllIntegral.text = if (it.data.num.isNullOrEmpty()) {
                "0"
            } else {
                it.data.num!!
            }
        }

        //获取首页所有信息返回
        mViewModel.allData.observe(this) {
            it.data.let { bean ->
                bundle.clear()
                bundle.putString("id", CommonUtils.getMyUserId().toString())
                bundle.putString("titleName", "日报")
                bundle.putBoolean("isDemonstrate", bean.demonstrate == 1)
                RouteManager.goAct(ARouterUrl.HEALTH_SKY_HOUR_REPORT, bundle)
            }
        }
    }

    /**
     * 数据上传刷新
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun refreshList(event: IntegralEvent) {
        loadData()
    }

    /**
     * 饮食上传刷新
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun healthDataChange(event: HealthDataChangeEvent) {
        loadData()
    }
}