package com.dahe.gldriver.ui.mine.activity

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.dahe.gldriver.adapter.PagersAdapter
import com.dahe.gldriver.base.AppConfig
import com.dahe.gldriver.bean.CaptainBean
import com.dahe.gldriver.databinding.ActivityCarTeamBinding
import com.dahe.gldriver.net.BaseObserver
import com.dahe.gldriver.net.DataManager
import com.dahe.gldriver.net.RxHttpCallBack
import com.dahe.gldriver.ui.mine.InviteRecordActivity
import com.dahe.gldriver.ui.mine.fragment.CarsManFragment
import com.dahe.gldriver.ui.mine.fragment.DriversManFragment
import com.dahe.gldriver.ui.waybill.fragment.AllWaybillFragment
import com.dahe.gldriver.utils.SPUtils
import com.dahe.mylibrary.base.BaseActivity
import com.dahe.mylibrary.net.CommonResponseBean
import com.dahe.mylibrary.utils.ActivityUtils
import com.dahe.mylibrary.utils.ImageLoader
import com.dahe.mylibrary.utils.ToastUtils
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers
import net.lucode.hackware.magicindicator.FragmentContainerHelper
import net.lucode.hackware.magicindicator.buildins.UIUtil
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.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView


/**
 * @ClassName CarTeamActivity
 * @Author john
 * @Date 2024/2/6 16:38
 * @Description 车队长-车队管理
 */
class CarTeamActivity : BaseActivity<ActivityCarTeamBinding>() {

    private val mFragmentContainerHelper = FragmentContainerHelper()
    var mFragments = mutableListOf<Fragment>()
    val titles = mutableListOf(
        "司机管理",
        "车辆管理"
    )


    override fun initView(savedInstanceState: Bundle?) {
        setStatusHeightParams(binding.rlTop)
        setTitleBar("车队管理", true, true, "邀请记录") {
            ActivityUtils.startActivity(mContext, InviteRecordActivity::class.java)
        }
        mFragments.run {
            add(DriversManFragment().apply {
                arguments = Bundle().apply {
                    putString(AppConfig.ID, SPUtils.instance.getUserInfo(mContext)!!.captainId)
                }
            })
            add(CarsManFragment().apply {
                arguments = Bundle().apply {
                    putString(AppConfig.ID, SPUtils.instance.getUserInfo(mContext)!!.captainId)
                }
            })
        }

        var pagerAdapter = PagersAdapter(this, mFragments)


        mFragmentContainerHelper.handlePageSelected(0, true)

        binding.viewPager.adapter = pagerAdapter
        binding.viewPager.currentItem = 0
        binding.viewPager.registerOnPageChangeCallback(object : OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                mFragmentContainerHelper.handlePageSelected(position, true)
            }
        });

        var commonNavigator = CommonNavigator(mContext)
        commonNavigator.run {
            isAdjustMode = true
            adapter = object : CommonNavigatorAdapter() {
                override fun getCount(): Int {
                    return titles.size
                }

                override fun getTitleView(context: Context?, index: Int): IPagerTitleView {
                    val colorTransitionPagerTitleView = ColorTransitionPagerTitleView(context)
                    colorTransitionPagerTitleView.normalColor = Color.BLACK
                    colorTransitionPagerTitleView.selectedColor = Color.RED
                    colorTransitionPagerTitleView.text = titles[index]
                    colorTransitionPagerTitleView.setOnClickListener {
                        mFragmentContainerHelper.handlePageSelected(index)
                        switchPages(index)
                    }
                    return colorTransitionPagerTitleView
                }

                override fun getIndicator(context: Context?): IPagerIndicator {
                    val indicator = LinePagerIndicator(context)
                    indicator.mode = LinePagerIndicator.MODE_WRAP_CONTENT
                    indicator.yOffset =
                        UIUtil.dip2px(context, 3.0).toFloat()
                    indicator.setColors(Color.RED)
                    return indicator
                }

            }
        }

        binding.magicIndicator.navigator = commonNavigator
        mFragmentContainerHelper.attachMagicIndicator(binding.magicIndicator)
    }

    override fun initDate() {
        DataManager.getInstance().getCaptainById(SPUtils.instance.getUserInfo(mContext)!!.captainId)
            .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
            .subscribe(BaseObserver(mContext, object : RxHttpCallBack<CaptainBean>() {
                override fun onSuccess(t: CommonResponseBean<CaptainBean>) {
                    super.onSuccess(t)
                    val data = t.data
                    binding.run {
                        tvCapName.text = data.captainName
                        tvStatu.text = if (data.isRealname == "0") "未实名" else "已实名"
                        ImageLoader.getInstance().loadCircleImage(mContext, data.avatar, ivHead)
                    }
                }
            }))
    }

    private fun switchPages(index: Int) {
        binding.viewPager.currentItem = index
    }

//    private fun switchPages(index: Int) {
//        val fragmentManager = supportFragmentManager
//        val fragmentTransaction = fragmentManager.beginTransaction()
//        var fragment: Fragment
//        var i = 0
//        val j: Int = mFragments.size
//        while (i < j) {
//            if (i == index) {
//                i++
//                continue
//            }
//            fragment = mFragments[i]
//            if (fragment.isAdded) {
//                fragmentTransaction.hide(fragment)
//            }
//            i++
//        }
//        fragment = mFragments[index]
//        if (fragment.isAdded) {
//            fragmentTransaction.show(fragment)
//        } else {
//            fragmentTransaction.add(R.id.fragmentContainer, fragment)
//        }
//        fragmentTransaction.commitAllowingStateLoss()
//    }
}