package com.swago.room.ranklist

import android.content.Context
import android.graphics.Color
import androidx.activity.viewModels
import androidx.core.content.ContextCompat
import androidx.fragment.app.viewModels
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.swago.baseswago.GradientIndicator
import com.swago.baseswago.SwagoAdapter
import com.swago.baseswago.activity.BaseXActivity
import com.swago.baseswago.constant.ARouteConstant
import com.swago.baseswago.util.AppContext
import com.swago.baseswago.util.DpPxUtil
import com.swago.baseswago.util.UserInfo
import com.swago.room.R
import com.swago.room.databinding.ActivityRankListBinding
import com.swago.room.vm.RoomOtherVm
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView

/**
 *@date 2021/12/1 20:06
 *description:
 */
@Route(path = ARouteConstant.Room.rankList)
class RankListActivity : BaseXActivity<ActivityRankListBinding>() {

    private val roomOtherVm by viewModels<RoomOtherVm>()

    @Autowired
    @JvmField
    var roomId = ""


    private val listTitles = arrayListOf(
        AppContext.getContext().resources.getString(R.string.daily),
        AppContext.getContext().resources.getString(R.string.weekly),
        AppContext.getContext().resources.getString(R.string.monthly)
    )


    override fun loadData() {
        roomOtherVm.getRankList(roomId, 1, 1)
    }

    override fun initOther() {
        ARouter.getInstance().inject(this)

        setBackView(binding.toolbar.ivBack)
        binding.toolbar.ivBack.setImageResource(R.mipmap.back_white)
        binding.toolbar.tvTitle.text =
            AppContext.getContext().resources.getString(R.string.fans_list)
        binding.toolbar.tvTitle.setTextColor(ContextCompat.getColor(this, R.color._ffffff))
        binding.viewPager.offscreenPageLimit = 3
        val listFragments = arrayListOf(
            RankListFragment.newInstance(roomId, 1),
            RankListFragment.newInstance(roomId, 2),
            RankListFragment.newInstance(roomId, 3)
        )
        val viewPagerAdapter = SwagoAdapter(listFragments, supportFragmentManager, listTitles)
        binding.viewPager.adapter = viewPagerAdapter

        val commonNavigator = CommonNavigator(this)
        commonNavigator.isAdjustMode = true
        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            override fun getCount(): Int {
                return listTitles.size
            }

            override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                val simplePagerTitleView = SimplePagerTitleView(context)
                simplePagerTitleView.text = listTitles[index]
                val padding = DpPxUtil.dip2px(15f).toFloat()
                simplePagerTitleView.textSize = 16f
                simplePagerTitleView.setPadding(padding.toInt(), 0, padding.toInt(), 0)
                simplePagerTitleView.normalColor = Color.parseColor("#868686")
                simplePagerTitleView.selectedColor = Color.parseColor("#000000")
                simplePagerTitleView.setOnClickListener {
                    binding.viewPager.currentItem = index
                }
                return simplePagerTitleView
            }

            override fun getIndicator(context: Context?): IPagerIndicator {
                val indicator = GradientIndicator(context)
                indicator.mode = GradientIndicator.MODE_WRAP_CONTENT
                indicator.lineWidth = DpPxUtil.dip2px(72f).toFloat()
                indicator.xOffset = DpPxUtil.dip2px(5f).toFloat()
                indicator.yOffset = DpPxUtil.dip2px(17f).toFloat()
                indicator.lineHeight = DpPxUtil.dip2px(8f).toFloat()
                indicator.roundRadius = DpPxUtil.dip2px(2f).toFloat()
                return indicator
            }

        }

        binding.magicIndicator.navigator = commonNavigator
        ViewPagerHelper.bind(binding.magicIndicator, binding.viewPager)
    }

    private val diamondDrawable by lazy {
        AppContext.getContext().resources.getDrawable(R.mipmap.icon_diamond)
    }

    private val coinDrawable by lazy {
        AppContext.getContext().resources.getDrawable(R.mipmap.icon_room_coin)
    }

    override fun initLiveData() {
        roomOtherVm.totalLiveData.observe(this) {
            binding.tvIncome.text = it
            UserInfo.getUserInfo()?.let {
                when (it.user_type) {
                    0 -> {
                        binding.tvIncome.setCompoundDrawablesWithIntrinsicBounds(
                            null,
                            coinDrawable,
                            null,
                            null
                        )
                    }

                    1 -> {
                        binding.tvIncome.setCompoundDrawablesWithIntrinsicBounds(
                            diamondDrawable,
                            null,
                            null,
                            null
                        )
                    }
                    else -> {
                        binding.tvIncome.setCompoundDrawablesWithIntrinsicBounds(
                            coinDrawable,
                            null,
                            null,
                            null
                        )
                    }
                }
            }
        }
    }
}