package com.yidian.yac.newscontent.ui

import android.content.Intent
import android.os.Bundle
import android.view.Gravity
import android.view.MenuItem
import android.widget.Toast
import androidx.core.view.forEach
import androidx.core.view.get
import androidx.fragment.app.Fragment
import com.google.android.material.bottomnavigation.LabelVisibilityMode
import com.yidian.common.ParcelizeMap
import com.yidian.common.XRouterPathConstants
import com.yidian.common.XRouterPathConstants.Companion.NEWS_MAIN
import com.yidian.common.base.BaseActivity
import com.yidian.common.services.FragmentFactory
import com.yidian.common.utils.FileDownloadUtil
import com.yidian.xarc.xevent.XEventManager
import com.yidian.yac.newscontent.http.ApiService
import com.yidian.yac.newscontent.http.httpbean.AppInitBean
import com.yidian.xpage.XPageViewProtocol
import com.yidian.yac.core.zap.Zap
import com.yidian.yac.newscontent.R
import com.yidian.yac.newscontent.adapter.ViewPagerAdapter
import com.yidian.yac.newscontent.utils.BadgeUtils
import com.yidian.yac.newscontent.utils.badgeStyle
import com.yidian.yac.newscontent.utils.item
import kotlinx.android.synthetic.main.activity_main.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


class MainActivity : BaseActivity(), XPageViewProtocol {

    private var fragmentList = mutableListOf<Fragment>()
    private lateinit var userInfoFragment: Fragment
    private lateinit var currentFragment: Fragment
    override val contentViewId: Int
        get() = R.layout.activity_main
    var menus = arrayListOf<String>("广场", "求职招聘", "燕郊圈", "消息", "我")
    var menuIcons = arrayListOf<Int>(
        R.drawable.ic_launcher_foreground,
        R.mipmap.ic_launcher_round,
        R.mipmap.ic_launcher_round,
        R.mipmap.ic_launcher_round,
        R.mipmap.ic_launcher_round
    )

    override fun getXPageName(): String {
        return NEWS_MAIN
    }

    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
//        XEventManager.register(this)
        userInfoFragment = Zap.of<FragmentFactory>(XRouterPathConstants.USER_INFO)
            ?.getFragment(XRouterPathConstants.USER_INFO)!!
        initFragment()
        initView()
        initListener()
        ApiService.appInit(
            AppInitBean.Request(
                "build;version;user;navi;counts;itemsnew;resnew;task",
                "{}",
                "0"
            )
        )
//        FileDownloadUtil.instance.startDownload("http://pkg.appresource.net/version/1564/app1564343/2.92010/app1564343.apk?v=2.92010")
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        val paramsMap = intent?.getParcelableExtra<ParcelizeMap>("paramsMap")
        if (paramsMap != null) {
            val name: String? = paramsMap.map?.get("name")
            Toast.makeText(this, name ?: "", Toast.LENGTH_SHORT).show()
        }
    }

    private fun initFragment() {
        fragmentList.add(HomeFragment.newInstance())
        fragmentList.add(ExploreFragment())
        fragmentList.add(GroupFragment.newInstance())
        fragmentList.add(ExploreFragment())
        fragmentList.add(userInfoFragment)
    }

    private fun initView() {
        viewPager.offscreenPageLimit = fragmentList.size
        viewPager.adapter = ViewPagerAdapter(
            supportFragmentManager, fragmentList
        )
        navView.itemIconTintList = null
        navView.labelVisibilityMode = LabelVisibilityMode.LABEL_VISIBILITY_LABELED;//1 不开动画 0 动画
        menus.forEachIndexed { index, title ->
            navView.menu.add(0, index, 0, title)
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM)
            navView.menu.getItem(index).setIcon(menuIcons[index])
        }
        //设置默认选中
        navView.menu[0].isChecked = true
        addBadgeView()

    }

    private fun initListener() {
        navView.setOnNavigationItemSelectedListener { menuItem ->
            navView.menu.forEach {
                if (it.itemId == menuItem.itemId) {
                    viewPager.setCurrentItem(menuItem.itemId, false)
                    it.setIcon(R.drawable.ic_launcher_foreground)
                } else {
                    it.setIcon(R.mipmap.ic_launcher_round)
                }
            }
            true
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun onEvent() {


    }

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

    private fun addBadgeView() {
        navView.badgeStyle(1, null, android.R.color.holo_red_dark)
        navView.item(1).number = 55
    }

    override fun onDestroy() {
        super.onDestroy()
        XEventManager.unRegister(this)
    }

}