package com.zj365.health.act

import android.annotation.SuppressLint
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.facade.annotation.Route
import com.androidkun.xtablayout.XTabLayout
import com.xty.base.act.BaseVmAct
import com.xty.base.vm.BaseVm
import com.xty.base.vp2.VpAdapter
import com.xty.common.arouter.ARouterUrl
import com.xty.common.util.DeviceFunctionUtils
import com.xty.network.model.DeviceFunctionBean
import com.zj365.health.R
import com.zj365.health.databinding.ActComparePageBinding
import com.zj365.health.fragment.BreathCompareDataFrag
import com.zj365.health.fragment.SleepCompareDataFrag
import com.zj365.health.fragment.TempCompareDataFrag
import com.zj365.health.fragment.UricAcidCompareDataFrag
import com.zj365.health.fragment.XYangCompareDataFrag
import com.zj365.health.fragment.XlCompareDataFrag
import com.zj365.health.fragment.XueTangCompareDataFrag
import com.zj365.health.fragment.XueYaCompareDataFrag
import com.zj365.health.fragment.bloodlipid.BloodFatCompareDataFrag
import com.zj365.health.vm.ComparePageDataVm

/**
 *  数据对比
 */
@Route(path = ARouterUrl.COMPARE_PAGE_DATA_ACT)
class ComparePageDataAct  : BaseVmAct<ComparePageDataVm>() {
    /**
     * userid
     */
   var id: String =""
    val title by lazy { arrayOf(  "心率数据对比", "血氧数据对比",  "呼吸率数据对比","体温数据对比","睡眠数据对比", "血压数据对比" ,"血糖数据对比","尿酸数据对比","血脂数据对比" ) }
    val compareArray: Array<String> by lazy { resources.getStringArray(R.array.compare_array) }
    var listFragment = mutableListOf<Fragment>()
    var functionList = ArrayList<DeviceFunctionBean>()
    /**
     */
    val binding by lazy{ ActComparePageBinding.inflate(layoutInflater)}

    override fun setLayout()=binding.root
    override fun initData() {
        super.initData()
        if (intent.extras!!.containsKey("id")) {
            id = intent.extras!!.getString("id").toString()
        }

        if(intent.extras!!.containsKey("functionList")){
            functionList = intent.extras!!.getParcelableArrayList<DeviceFunctionBean>("functionList")!!
        }
    }
    private fun initVp2(functionList :MutableList<DeviceFunctionBean>) {
        listFragment.clear()
        functionList.forEach {
            if (DeviceFunctionUtils.hasFunction(it,"hr")){
                listFragment.add(setArgumentFrag(XlCompareDataFrag(), id))
            }else if(DeviceFunctionUtils.hasFunction(it,"so")){
                listFragment.add(setArgumentFrag(XYangCompareDataFrag(), id))
            }else if(DeviceFunctionUtils.hasFunction(it,"resp")){
                listFragment.add(setArgumentFrag(BreathCompareDataFrag (),id))
            }else if(DeviceFunctionUtils.hasFunction(it,"temp")){
                listFragment.add(setArgumentFrag(TempCompareDataFrag(),id))
            }else if(DeviceFunctionUtils.hasFunction(it,"sleep")){
                listFragment.add(setArgumentFrag(SleepCompareDataFrag (),id))

            }else if(DeviceFunctionUtils.hasFunction(it,"dbp")){
                listFragment.add(setArgumentFrag(XueYaCompareDataFrag (),id))

            }else if(DeviceFunctionUtils.hasFunction(it,"sugar")){
                listFragment.add(setArgumentFrag(XueTangCompareDataFrag(),id))

            }else if(DeviceFunctionUtils.hasFunction(it,"uric")){
                listFragment.add(setArgumentFrag(UricAcidCompareDataFrag(),id))

            }else if(DeviceFunctionUtils.hasFunction(it,"bloodFat")){
                listFragment.add(setArgumentFrag(BloodFatCompareDataFrag(),id))
            }
        }

        binding.mVp2.adapter = VpAdapter(listFragment, this)
        binding.mVp2.isUserInputEnabled = false
        binding.mVp2.offscreenPageLimit =1
        binding.mVp2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
//                changeTitle(position)
            }
        })
    }

    @SuppressLint("CommitTransaction", "SuspiciousIndentation")
    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
//        binding.title.mTvTitle.text = resources.getString(R.string.compare_data)
     binding.title.mTvTitle.text = "健康数据对比"
        binding.title.mIvReight.visibility = View.GONE
        binding.title.mIvBack.apply {
            this.setOnClickListener {
                finish()
            }
        }
        if(functionList.size == 0){
            mViewModel.getDeviceFunctionList(id)
        }else{
            initTab(functionList)
            initVp2(functionList)
        }

    }
    private fun initTab(functionList :MutableList<DeviceFunctionBean>) {
        functionList.forEach {
            if(it.type == "ecg" || it.type == "step"){
                return@forEach
            }
            if (it.status == 1){
                val tab = binding.xTablayout.newTab()
                tab.text = it.name
                binding.xTablayout.addTab(tab)
            }

        }
        binding.xTablayout.setOnTabSelectedListener(object : XTabLayout.OnTabSelectedListener{
            override fun onTabSelected(tab: XTabLayout.Tab?) {
                binding.mVp2.currentItem = tab!!.position
            }

            override fun onTabUnselected(tab: XTabLayout.Tab?) {
            }

            override fun onTabReselected(tab: XTabLayout.Tab?) {
            }

        })
    }

    override fun liveObserver() {
        mViewModel.functionLiveData.observe(this){
            it.data.let {
                functionList.addAll(it)
                initTab(functionList)
                initVp2(functionList)
            }
        }
    }

    fun changeTitle(position: Int) {
        binding.title.mTvTitle.text = title[position]
    }


    private fun setArgumentFrag(frag: Fragment, id: String = ""): Fragment {
        bundle.clear()
        bundle.putString("id", id)
        frag.arguments = bundle
        return frag
    }

}