package com.sixin.module_p.ui.enter

import android.content.Context.RECEIVER_EXPORTED
import android.content.IntentFilter
import android.os.Build
import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.lukouguoji.module_base.base.CustomVP2Adapter
import com.lukouguoji.module_base.bean.SocketEventBean
import com.lukouguoji.module_base.common.ConstantEvent
import com.lukouguoji.module_base.http.net.NetApply
import com.lukouguoji.module_base.impl.FlowBus
import com.lukouguoji.module_base.impl.observe
import com.lukouguoji.module_base.ktx.launchCollect
import com.lukouguoji.module_base.ktx.noNull
import com.lukouguoji.module_base.router.ARouterConstants
import com.lukouguoji.module_base.util.WebSocketUtil
import com.sixin.module_p.R
import com.sixin.module_p.base.BasePdaActivity
import com.sixin.module_p.databinding.ActivityPdaenterBinding
import com.sixin.module_p.helper.PDAHelper
import com.sixin.module_p.service.ScanCodeBroadcastReceiver
import com.sixin.module_p.ui.user.message.PdaMessageListActivity
import me.jessyan.autosize.internal.CustomAdapt

@Route(path = ARouterConstants.ACTIVITY_URL_PDA_ENTER)
class PDAEnterActivity : BasePdaActivity<ActivityPdaenterBinding, PDAEnterViewModel>(),
    CustomAdapt {

    private val receiver by lazy {
        ScanCodeBroadcastReceiver()
    }

    override fun layoutId() = R.layout.activity_pdaenter

    override fun viewModelClass(): Class<PDAEnterViewModel> {
        return PDAEnterViewModel::class.java
    }

    override fun initOnCreate(savedInstanceState: Bundle?) {
        binding.viewModel = viewModel

        binding.vp.adapter =
            CustomVP2Adapter(viewModel.fragmentList, supportFragmentManager, lifecycle)
        binding.vp.isUserInputEnabled = false
        binding.vp.offscreenPageLimit = 4

        viewModel.index.observe(this) {
            binding.vp.setCurrentItem(it, false)
        }

        FlowBus.with<Any>(ConstantEvent.LOGOUT)
            .observe(this) {
                finish()
            }
        FlowBus.with<SocketEventBean>(ConstantEvent.SOCKET_EVENT_CLICK)
            .observe(this) {
                PdaMessageListActivity.start(this)
            }
        FlowBus.with<SocketEventBean>(ConstantEvent.SOCKET_EVENT)
            .observe(this) {
                queryUnHandleMessageCount()
            }

        // 注册广播
        val filter = IntentFilter()
        filter.addAction(ScanCodeBroadcastReceiver.ACTION)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            registerReceiver(receiver, filter, RECEIVER_EXPORTED)
        } else {
            registerReceiver(receiver, filter)
        }

        // 连接socket
        WebSocketUtil.connect()
    }

    override fun onResume() {
        super.onResume()
        queryUnHandleMessageCount()
    }

    private fun queryUnHandleMessageCount() {
        launchCollect({
            NetApply.api.simplePost("msg/msgStateQuery")
        }) {
            onSuccess = {
                val unread = it.data?.unread.noNull("0")
                binding.tvMessage.text = unread
                binding.tvMessage.visibility = if (unread != "0") View.VISIBLE else View.GONE
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(receiver)
    }


    override fun isBaseOnWidth() = true

    override fun getSizeInDp(): Float = PDAHelper.PDA_WIDTH

}