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

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.dahe.gldriver.R
import com.dahe.gldriver.adapter.PagersAdapter
import com.dahe.gldriver.base.AppConfig
import com.dahe.gldriver.bean.RateBean
import com.dahe.gldriver.databinding.ActivityRateManBinding
import com.dahe.gldriver.net.BaseObserver
import com.dahe.gldriver.net.DataManager
import com.dahe.gldriver.net.RxHttpCallBack
import com.dahe.gldriver.ui.mine.fragment.CarsManFragment
import com.dahe.gldriver.ui.mine.fragment.DriversManFragment
import com.dahe.gldriver.ui.mine.fragment.RateFragment
import com.dahe.gldriver.utils.SPUtils
import com.dahe.mylibrary.base.BaseActivity
import com.dahe.mylibrary.net.CommonResponseBean
import com.dahe.mylibrary.utils.ImageLoader
import com.dahe.mylibrary.utils.PhoneFormatCheckUtils
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 RateManActivity
 * @Author john
 * @Date 2024/3/18 08:51
 * @Description TODO
 */
class RateManActivity : BaseActivity<ActivityRateManBinding>() {

    private val mFragmentContainerHelper = FragmentContainerHelper()
    var mFragments = mutableListOf<Fragment>()
    val titles = mutableListOf(
        "收到的评价", "发表的评价"
    )


    override fun initView(savedInstanceState: Bundle?) {
        setStatusBarColor(R.color.white)
        setTitleBar("评价管理", true)

        mFragments.run {
            add(RateFragment().apply {
                arguments = Bundle().apply {
                    putInt(AppConfig.RATE_TYPE, 0)
                }
            })
            add(RateFragment().apply {
                arguments = Bundle().apply {
                    putInt(AppConfig.RATE_TYPE, 1)
                }
            })
        }

        var pagerAdapter = PagersAdapter(this, mFragments)


        mFragmentContainerHelper.handlePageSelected(0, true)

        binding.viewPager.adapter = pagerAdapter
        binding.viewPager.currentItem = 0
        binding.viewPager.registerOnPageChangeCallback(object : ViewPager2.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() {
        val userInfo = SPUtils.instance.getUserInfo(mContext)
        if (userInfo != null) {
            binding.run {
                tvName.text = userInfo.idcardName
                ImageLoader.getInstance()
                    .loadCircleImage(mContext, userInfo.avatar, ivHead, R.drawable.head_defaut)
            }
        }
        DataManager.getInstance().driverRate().subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(BaseObserver(mContext, object : RxHttpCallBack<RateBean>() {
                override fun onSuccess(t: CommonResponseBean<RateBean>) {
                    super.onSuccess(t)
                    t.data?.let {
                        binding.run {
                            tvRating.text = if (it.positiveReviewRate.isNullOrEmpty()) "0%" else """${(it.positiveReviewRate.toInt() * 100)}%"""
                            pbOne.progress =
                                if (it.numberOfEvaluations1.isNullOrEmpty()) 0 else (it.numberOfEvaluations1.toInt() * 100 / it.totalNumberOfOrders.toInt())

                            pbTwo.progress =
                                if (it.numberOfEvaluations2.isNullOrEmpty()) 0 else (it.numberOfEvaluations2.toInt() * 100 / it.totalNumberOfOrders.toInt())

                            pbThree.progress =
                                if (it.numberOfEvaluations3.isNullOrEmpty()) 0 else (it.numberOfEvaluations3.toInt() * 100 / it.totalNumberOfOrders.toInt())
                        }
                    }
                }
            }))


    }

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