package com.zj365.dc.fragment

import android.content.Intent
import android.graphics.Color
import android.text.TextUtils
import android.view.View
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.xty.base.fragment.BaseFragList
import com.xty.common.Const
import com.xty.common.MMkvHelper
import com.xty.common.R
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.event.LoginOrRegisterEvent
import com.xty.common.event.LoginOutEvent
import com.xty.common.event.MessageReadEvent
import com.xty.network.model.SystemMsgBean
import com.zj365.dc.adapter.HomeMessageAdapter
import com.zj365.dc.databinding.FragMessageBinding
import com.zj365.dc.vm.MessageVm
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class MessageFragment : BaseFragList<MessageVm>()  {

    val binding by lazy { FragMessageBinding.inflate(layoutInflater) }

    val adapter by lazy { HomeMessageAdapter() }

    var msgList = ArrayList<SystemMsgBean>()
    override fun initAdapter() {
        binding.mRecycle.layoutManager= LinearLayoutManager(requireContext())
        binding.mRecycle.adapter=adapter
        adapter.setOnItemClickListener { adapter, view, position ->
            var bean = adapter.getItem(position) as SystemMsgBean
            bundle.clear()
            bundle.putInt("msgType",bean.type)
            bundle.putString("msgTitle",bean.name)
            RouteManager.goAct(ARouterUrl.MESSAGE_TYPE,bundle)
        }
    }

    override fun initData() {
        super.initData()

    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text="消息"
        binding.title.mTvRight.text="全部已读"
        binding.title.mTvRight.setTextColor(Color.parseColor("#02C191"))
        binding.title.mTvRight.visibility = View.VISIBLE
        binding.title.mIvBack.visibility = View.INVISIBLE
        binding.title.mTvRight.setOnClickListener {
            mViewModel.setAllRead()
        }
        initAdapter()
        setRecycleRefresh(binding.mRecycle,binding.mRefresh)
    //    initTest()
    }


    /*fun initTest(){
        var titles = ArrayList<String>()
        titles.add("胆经")
        titles.add("肝经")
        titles.add("肺经")
        titles.add("大肠经")
        titles.add("胃经")
        titles.add("脾经")

        titles.add("心经")
        titles.add("小肠经")
        titles.add("膀胱经")
        titles.add("肾经")
        titles.add("心包经")
        titles.add("三焦经")

        var img = ArrayList<Int>()

        img.add(R.mipmap.ic_heart_1)
        img.add(R.mipmap.ic_liver_1)
        img.add(R.mipmap.ic_spleen_1)
        img.add(R.mipmap.ic_lung_1)
        img.add(R.mipmap.ic_kidney_1)
        img.add(R.mipmap.ic_large_intestine_1)

        img.add(R.mipmap.ic_small_intestine_1)
        img.add(R.mipmap.ic_stomach_1)
        img.add(R.mipmap.ic_lymph_1)
        img.add(R.mipmap.ic_gallbladder_1)
        img.add(R.mipmap.ic_bladder_1)
        img.add(R.mipmap.ic_pericardium_1)

        binding.clBody.healthBody.setMenuItemIconsAndTexts(img,titles)
      //  binding..healthBody.setMenuItemIconsAndTexts(img, titles)

    }*/

    override fun refresh() {
        val userId = if (MMkvHelper.getLong(Const.USER_ID).toString() == "0") {
            ""
        } else {
            MMkvHelper.getLong(Const.USER_ID).toString()


        }
        if(!TextUtils.isEmpty(userId)){
            mViewModel.getSystemMsg()
        }

    }

    override fun setViewModel()= MessageVm()

    override fun observer() {
        mViewModel.msgData.observe(this){
            adapter.setNewInstance(it.data.list)
            val intent = Intent()
            intent.action = Const.CONVERSATION_UNREAD_COUNT_ACTION
            intent.putExtra(Const.UNREAD_COUNT_EXTRA, it.data.unReadCount)
            LocalBroadcastManager.getInstance(requireContext())
                .sendBroadcast(intent)
        }
        mViewModel.msgAllRead.observe(this){
            refresh()
        }

    }

    override fun setLayout()=binding.root

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun loginOrRegisterChange(event: LoginOrRegisterEvent){
        refresh()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun loginOutChange(event: LoginOutEvent){
   //     refresh()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun readMsg(event:MessageReadEvent){
        refresh()
    }

}