package com.lukouguoji.aerologic

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.lukouguoji.aerologic.page.message.details.MessageDetailsActivity
import com.lukouguoji.aerologic.page.message.list.MessageListActivity
import com.lukouguoji.aerologic.ui.fragment.HomeFragment
import com.lukouguoji.aerologic.ui.fragment.MineFragment
import com.lukouguoji.module_base.BaseActivity
import com.lukouguoji.module_base.MyApplication.Companion.context
import com.lukouguoji.module_base.bean.SocketEventBean
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.common.ConstantEvent
import com.lukouguoji.module_base.db.perference.SharedPreferenceUtil
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

@Route(path = "/app/MainActivity")
class MainActivity : BaseActivity() {
    private var lastIndex = 0
    private var mFragments = mutableListOf<Fragment>()
    private lateinit var bottomNavigationView: BottomNavigationView
    private lateinit var userName: TextView

    private var rlMessage: RelativeLayout? = null
    private var tvMessage: TextView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        rlMessage = findViewById(R.id.rl_message)
        tvMessage = findViewById(R.id.tv_message)

        initData()
        var toolbar: Toolbar = findViewById(R.id.main_toolbar)
        setSupportActionBar(toolbar)
        // 给左上角图标的左边加上一个返回的图标 。对应ActionBar.DISPLAY_HOME_AS_UP
        supportActionBar!!.setDisplayHomeAsUpEnabled(false)
        //设置actionBar的标题是否显示，对应ActionBar.DISPLAY_SHOW_TITLE。
        supportActionBar!!.setDisplayShowTitleEnabled(false)

        var imageView: ImageView = findViewById(R.id.mine)
        imageView.setOnClickListener {
            ARouter.getInstance().build(ARouterConstants.ACTIVITY_URL_MINE).navigation()
        }

        userName = findViewById(R.id.userName)
        userName.text = SharedPreferenceUtil.getString(Constant.Share.userName)

        FlowBus.with<Any>(ConstantEvent.LOGOUT)
            .observe(this) {
                finish()
            }

        FlowBus.with<SocketEventBean>(ConstantEvent.SOCKET_EVENT_CLICK)
            .observe(this) {
                MessageDetailsActivity.start(this, it.toMessageBean())
            }
        FlowBus.with<SocketEventBean>(ConstantEvent.SOCKET_EVENT)
            .observe(this) {
                queryUnHandleMessageCount()
            }

        rlMessage?.setOnClickListener {
            MessageListActivity.start(it.context)
        }

        WebSocketUtil.connect()
    }

    private fun initData() {
        mFragments = ArrayList()
        mFragments.add(HomeFragment())
        mFragments.add(MineFragment())
    }

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

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

    /**
     * onActivityResult 回调
     */
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (resultCode == RESULT_OK) {
            when (requestCode) {
                Constant.RequestCode.home_left_hang_ban_query -> {
                    val fragment =
                        supportFragmentManager.findFragmentById(R.id.home_fragment) as HomeFragment
                    val layoutManager = LinearLayoutManager(context)
                    val adapt = fragment.HomeLeftAdapt(fragment.initLeftMenuData())
                    val recyclerView = findViewById<RecyclerView>(R.id.left_menu_list)
                    recyclerView?.layoutManager = layoutManager
                    recyclerView?.adapter = adapt
                }
            }
        }
    }
}