package com.zj365.mime.act

import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.xty.base.act.BaseListAct
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.getImageUrl
import com.zj365.mime.R
import com.zj365.mime.adapter.MessageAdapter
import com.zj365.mime.databinding.ActMessageBinding
import com.zj365.mime.vm.MessageVm
import com.zj365.mime.weight.MessagePop
import com.xty.network.model.MessageBean

/**
 * 我的消息
 */
@Route(path = ARouterUrl.MESSAGE)
class MessageAct : BaseListAct<MessageVm>() {
    val binding by lazy { ActMessageBinding.inflate(layoutInflater) }
    val adapter by lazy { MessageAdapter() }
    var clickBean: MessageBean.SysMsg? = null
    var clickPosition = 0
    val array by lazy { resources.getStringArray(R.array.msg_array) }
    var nowPosition = 1
    val popWindow by lazy {
        MessagePop(this) {
            if (it != nowPosition) {
                page = 1
                nowPosition = it
//                binding.mSpinner.text = array[nowPosition]
                loadData()
            }
        }
    }

    override fun setLayout() = binding.root

    override fun initData() {
        super.initData()
        isDarkBar = true
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text = getString(R.string.sys_message)

        setRecycleRefresh(binding.mRecycle, binding.mRefresh)
        binding.title.mIvBack.setOnClickListener {
            finish()
        }
//        binding.mSpinner.setOnClickListener {
//            popWindow.showAction(it)
//        }

//        binding.mSpinner.text = array[nowPosition]
    }

    override fun initAdapter() {
        binding.mRecycle.layoutManager = LinearLayoutManager(this)
        binding.mRecycle.adapter = adapter
        adapter.addChildClickViewIds(R.id.mContentView, R.id.mDelete)
        adapter.setOnItemChildClickListener { adapter, view, position ->
            var bean = adapter.getItem(position)
            when (view.id) {
                R.id.mContentView -> {
                    if (bean is MessageBean.Msg) {
                        bundle.putString("title", bean.realname)
                        when (bean.isMe) {
                            1 -> {
                                bundle.putString("toUserId", bean.to_user_id)
                                bundle.putString("fromUserId", bean.from_user_id)
                            }
                            2 -> {
                                bundle.putString("fromUserId", bean.to_user_id)
                                bundle.putString("toUserId", bean.from_user_id)
                            }

                        }

                        bundle.putString("chatImage", getImageUrl(bean.avatar))
                        RouteManager.goAct(ARouterUrl.CHAT_MSG, bundle)
                    } else {
                        val data = bean as MessageBean.SysMsg
                        if (data.notifyStatus == 0) {
                            clickBean = data
                            mViewModel.readMsg(data.id, data.notifyId)
                            clickPosition = position
                        } else {
                            bundle.clear()
                            bundle.putSerializable("bean", data)
                            RouteManager.goAct(ARouterUrl.MESSAGE_DETAIL_NEW, bundle)
                        }
                    }
                }
                R.id.mDelete -> {
                    val sysBean = bean as MessageBean.SysMsg
                    mViewModel.deleteMsg(sysBean.notifyId)
                }
            }
        }

    }

    override fun liveObserver() {
        mViewModel.readLive.observe(this) {
            (adapter.data[clickPosition] as MessageBean.SysMsg).notifyStatus = 1
            adapter.notifyItemChanged(clickPosition)
            bundle.clear()
            bundle.putSerializable("bean", clickBean)
            RouteManager.goAct(ARouterUrl.MESSAGE_DETAIL_NEW, bundle)
        }
        mViewModel.messageLive.observe(this) {
           /* when (nowPosition) {
                3 -> {
                    val list = mutableListOf<Any>()
                    list.add(it.data.message)
                    setDate(adapter, list)
                }
                0 -> {
                    val list = mutableListOf<Any>()
                    list.add(it.data.message)
                    it?.data?.let {
                        it.systemMessage.let { it1 -> list.addAll(it1) }
                    }
                    setDate(adapter, list)
                }
                else -> {
                    setDate(adapter, it.data.systemMessage as MutableList<Any>)
                }
            }*/
        }
        mViewModel.deleteLive.observe(this) {
            mViewModel.getMessageList(page, 0)
        }
    }

    override fun loadData() {
        mViewModel.getMessageList(page, 0)
    }


}