package com.zj365.health.act.exception

import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
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.google.android.exoplayer2.util.ColorParser
import com.gyf.immersionbar.ImmersionBar
import com.xty.base.act.BaseVmAct
import com.xty.base.vp2.VpAdapter
import com.xty.common.Const
import com.xty.common.arouter.ARouterUrl
import com.zj365.health.R
import com.zj365.health.databinding.ActivityAllExceptionDataBinding
import com.zj365.health.fragment.bloodlipid.BloodFatExceptionFrag
import com.zj365.health.vm.ExceptionVm

@Route(path = ARouterUrl.ALL_EXCEPTION_DATA_ACT)
class AllExceptionDataActivity : BaseVmAct<ExceptionVm>() {

    val binding by lazy { ActivityAllExceptionDataBinding.inflate(layoutInflater) }

    val title by lazy { arrayOf("血压异常数据","心率异常数据","呼吸率异常数据","体温异常数据","血氧异常数据","血脂异常数据") }
    var tabName = arrayOf<String>()
    var listFragment = mutableListOf<Fragment>()
    var exceptionType = Const.xyBloodPressure
    var timeType = Const.typeHours
    var time = ""
    var id = ""
    var page = 0

    var mData = arrayListOf<String>()
    var mTime= arrayListOf<String>()

    var currentTime:String=""

    override fun initView() {
        super.initView()
        id = intent.extras?.getString("id").toString()
        timeType = intent.extras!!.getInt("timeType")
        time = intent.extras?.getString("time").toString()
        exceptionType = intent.extras!!.getInt("exceptionType")
        currentTime = intent.extras!!.getString("currentTime","")

        statusBar(binding.title.mView)
        binding.title.mTvTitle.text = title[exceptionType - 1]
        binding.title.titleBg.setBackgroundColor(ColorParser.parseCssColor("#FFFFFF"))
        binding.title.mIvBack.setOnClickListener { finish() }
        when (exceptionType) {
            //心率
            Const.xlHeartRate -> {
                tabName = arrayOf("心动过缓","心动过速")
                if (currentTime.isNotEmpty()){  //日报
                    mViewModel.getWatchAbnormalDataXL(id,timeType.toString(),currentTime,exceptionType.toString())
                }else{
                    mViewModel.getWatchAbnormalDataXL(id,timeType.toString(),time,exceptionType.toString())
                }
            }
            //血氧
            Const.xyBloodOxygen -> {
                tabName = arrayOf("供氧不足","低血氧")
                if (currentTime.isNotEmpty()) {  //日报
                    mViewModel.getWatchAbnormalDataXO(id,timeType.toString(),currentTime,exceptionType.toString())
                }else{
                    mViewModel.getWatchAbnormalDataXO(id,timeType.toString(),time,exceptionType.toString())
                }
            }
            //呼吸率
            Const.hxlBreathingRate -> {
                tabName = arrayOf("呼吸过缓","呼吸过速")
                if (currentTime.isNotEmpty()) {  //日报
                    mViewModel.getWatchAbnormalDataHXL(id,timeType.toString(),currentTime,exceptionType.toString())
                }else{
                    mViewModel.getWatchAbnormalDataHXL(id,timeType.toString(),time,exceptionType.toString())
                }
            }
            //体温
            Const.twTemperature -> {
                tabName = arrayOf("低热","中热","高热","超高热")
                if (currentTime.isNotEmpty()) {  //日报
                    mViewModel.getWatchAbnormalDataTW(id,timeType.toString(),currentTime,exceptionType.toString())
                }else {
                    mViewModel.getWatchAbnormalDataTW(id,timeType.toString(),time,exceptionType.toString())
                }
            }
            //血压
            Const.xyBloodPressure -> {
                tabName = arrayOf("低血压","一级高血压","二级高血压","三级高血压")
                if (currentTime.isNotEmpty()) {  //日报
                    mViewModel.getWatchAbnormalDataXY(id,timeType.toString(),currentTime,exceptionType.toString())
                }else{
                    mViewModel.getWatchAbnormalDataXY(id,timeType.toString(),time,exceptionType.toString())
                }
            }

            Const.bloodFatExcep ->{
                tabName =
                    arrayOf("总胆固醇", "甘油三酯", "高密度脂蛋白", "低密度脂蛋白")

                if (currentTime.isNotEmpty()) {  //日报
                    mViewModel.getWatchAbnormalDataBloodFat(id,timeType.toString(),currentTime,"10")
                }else{
                    mViewModel.getWatchAbnormalDataBloodFat(id,timeType.toString(),time,"10")
                }

            }
            else -> {}
        }
    }

    private fun initTab() {
        binding.xTablayout.setxTabDisplayNum(tabName.size)
        tabName.forEach {
            var tab = binding.xTablayout.newTab()
            tab.text = it
            binding.xTablayout.addTab(tab)
        }
        binding.xTablayout.setOnTabSelectedListener(object : XTabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: XTabLayout.Tab?) {
                page=tab!!.position
                binding.mVp2.setCurrentItem( tab!!.position,false)
            }

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

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

        })
    }

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

    private fun initViewPager() {

        binding.mVp2.adapter = VpAdapter(listFragment, this)
        binding.mVp2.currentItem = page
        binding.mVp2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                binding.xTablayout.getTabAt(position)!!.select()
                page=position
            }
        })
        binding.mVp2.post {
            binding.mVp2.setCurrentItem(page, false)
        }
    }

    fun changeTitle(position: Int) {
        var bg: Drawable
        var backId: Int
        var txtColInt: Int
        bg = ContextCompat.getDrawable(this, R.drawable.shape_white)!!
        txtColInt = ContextCompat.getColor(this, R.color.col_515)
        ImmersionBar.with(this).statusBarDarkFont(true).init()
        binding.title.mIvReight.visibility = View.GONE
//        if (position == 5 && tabName.size == 8) {
//            bg = ContextCompat.getDrawable(this, R.drawable.shape_white)!!
//            backId = R.mipmap.icon_back_black
//            txtColInt = ContextCompat.getColor(this, R.color.col_515)
//            ImmersionBar.with(this).statusBarDarkFont(true).init()
//            binding.title.mIvReight.visibility = View.GONE
//        } else {
//            bg = ContextCompat.getDrawable(this, R.drawable.shape_bg_health_round)!!
//            backId = R.mipmap.icon_back_white
//            txtColInt = ContextCompat.getColor(this, R.color.white)
//            ImmersionBar.with(this).statusBarDarkFont(false).init()
//            //binding.title.mIvReight.visibility=View.VISIBLE
//        }
        var parentView = binding.title.mTvTitle.parent as View
        parentView.background = bg

        binding.title.mTvTitle.setTextColor(txtColInt)
        binding.title.mIvBack.apply {
//            this.setImageResource(backId)
            this.setOnClickListener {
                finish()
            }
        }


    }

    private fun setArgumentFrag(frag: Fragment, title: String = "",data : ArrayList<String>,time:ArrayList<String>): Fragment {
        var mBundle= Bundle()
        mBundle.putString("exception_title", title)
        mBundle.putStringArrayList("exception_data", data)
        mBundle.putStringArrayList("exception_time", time)
        frag.arguments = mBundle
        return frag
    }

    override fun liveObserver() {

        mViewModel.exceptionXLData.observe(this) {
            var mData1 = arrayListOf<String>()
            var mTime1 = arrayListOf<String>()
            it.data.bradycardia.asReversed().forEach {
                mData1.add(it.data.toString())
                mTime1.add(it.time)
            }
            listFragment.add(setArgumentFrag(XinlvExceptionDataFragment(),tabName[0],mData1,mTime1))
            var mData2 = arrayListOf<String>()
            var mTime2 = arrayListOf<String>()
            it.data.tachycardia.asReversed().forEach {
                mData2.add(it.data.toString())
                mTime2.add(it.time)
            }
            listFragment.add(setArgumentFrag(XinlvExceptionDataFragment(),tabName[1],mData2,mTime2))
            initTab()
            initViewPager()
        }
        mViewModel.exceptionXOData.observe(this){
            var mData1 = arrayListOf<String>()
            var mTime1 = arrayListOf<String>()
            it.data.deficiency.asReversed().forEach {
                mData1.add(it.data.toString())
                mTime1.add(it.time)
            }
            listFragment.add(setArgumentFrag(XueyangExceptionDataFragment(),tabName[0],mData1,mTime1))
            var mData2 = arrayListOf<String>()
            var mTime2 = arrayListOf<String>()
            it.data.hyoxemia.asReversed().forEach {
                mData2.add(it.data.toString())
                mTime2.add(it.time)
            }
            listFragment.add(setArgumentFrag(XueyangExceptionDataFragment(),tabName[1],mData2,mTime2))
            initTab()
            initViewPager()
        }
        mViewModel.exceptionHXLData.observe(this){
            var mData1 = arrayListOf<String>()
            var mTime1 = arrayListOf<String>()
            it.data.oligopnea.asReversed().forEach {
                mData1.add(it.data.toString())
                mTime1.add(it.time)
            }
            listFragment.add(setArgumentFrag(BreathExceptionDataFragment(),tabName[0],mData1,mTime1))
            var mData2 = arrayListOf<String>()
            var mTime2 = arrayListOf<String>()
            it.data.tachypnea.asReversed().forEach {
                mData2.add(it.data.toString())
                mTime2.add(it.time)
            }
            listFragment.add(setArgumentFrag(BreathExceptionDataFragment(),tabName[1],mData2,mTime2))
            initTab()
            initViewPager()

        }

        mViewModel.exceptionTWData.observe(this){
            var mData2 = arrayListOf<String>()
            var mTime2 = arrayListOf<String>()
            it.data.low.asReversed().forEach {
                mData2.add(it.data.toString())
                mTime2.add(it.time)
            }
            listFragment.add(setArgumentFrag(TiwenExceptionDataFragment(),tabName[0],mData2,mTime2))
            var mData1 = arrayListOf<String>()
            var mTime1 = arrayListOf<String>()
            it.data.center.asReversed().forEach {
                mData1.add(it.data.toString())
                mTime1.add(it.time)
            }
            listFragment.add(setArgumentFrag(TiwenExceptionDataFragment(),tabName[1],mData1,mTime1))

            var mData3 = arrayListOf<String>()
            var mTime3 = arrayListOf<String>()
            it.data.tall.asReversed().forEach {
                mData3.add(it.data.toString())
                mTime3.add(it.time)
            }
            listFragment.add(setArgumentFrag(TiwenExceptionDataFragment(),tabName[2],mData3,mTime3))
            var mData4 = arrayListOf<String>()
            var mTime4 = arrayListOf<String>()
            it.data.superelevation.asReversed().forEach {
                mData4.add(it.data.toString())
                mTime4.add(it.time)
            }
            listFragment.add(setArgumentFrag(TiwenExceptionDataFragment(),tabName[3],mData4,mTime4))
            initTab()
            initViewPager()

        }

        mViewModel.exceptionXYData.observe(this){
            var mData1 = arrayListOf<String>()
            var mTime1 = arrayListOf<String>()
            it.data.hypotension.asReversed().forEach {
                mData1.add("${it.high}/${it.low}")
                mTime1.add(it.time)
            }
            listFragment.add(setArgumentFrag(XueyaExceptionDataFragment(),tabName[0],mData1,mTime1))
            var mData2 = arrayListOf<String>()
            var mTime2 = arrayListOf<String>()
            it.data.one.asReversed().forEach {
                mData2.add("${it.high}/${it.low}")
                mTime2.add(it.time)
            }
            listFragment.add(setArgumentFrag(XueyaExceptionDataFragment(),tabName[1],mData2,mTime2))
            var mData3 = arrayListOf<String>()
            var mTime3 = arrayListOf<String>()
            it.data.two.asReversed().forEach {
                mData3.add("${it.high}/${it.low}")
                mTime3.add(it.time)
            }
            listFragment.add(setArgumentFrag(XueyaExceptionDataFragment(),tabName[2],mData3,mTime3))
            var mData4 = arrayListOf<String>()
            var mTime4 = arrayListOf<String>()
            it.data.three.asReversed().forEach {
                mData4.add("${it.high}/${it.low}")
                mTime4.add(it.time)
            }
            listFragment.add(setArgumentFrag(XueyaExceptionDataFragment(),tabName[3],mData4,mTime4))
            initTab()
            initViewPager()

        }

        mViewModel.exceptionBloodFatData.observe(this) {
            val mData1 = arrayListOf<String>()
            val mTime1 = arrayListOf<String>()
            it.data.tcList.asReversed().forEach {
                mData1.add("${it.data}")
                mTime1.add("${it.time}")
            }
            listFragment.add(setArgumentFrag(BloodFatExceptionFrag(), tabName[0], mData1, mTime1))
            val mData2 = arrayListOf<String>()
            val mTime2 = arrayListOf<String>()
            it.data.tgList.asReversed().forEach {
                mData2.add("${it.data}")
                mTime2.add("${it.time}")
            }
            listFragment.add(setArgumentFrag(BloodFatExceptionFrag(), tabName[1], mData2, mTime2))
            val mData3 = arrayListOf<String>()
            val mTime3 = arrayListOf<String>()
            it.data.hdlList.asReversed().forEach {
                mData3.add("${it.data}")
                mTime3.add("${it.time}")
            }
            listFragment.add(setArgumentFrag(BloodFatExceptionFrag(), tabName[2], mData3, mTime3))
            val mData4 = arrayListOf<String>()
            val mTime4 = arrayListOf<String>()
            it.data.ldlList.asReversed().forEach {
                mData4.add("${it.data}")
                mTime4.add("${it.time}")
            }
            listFragment.add(setArgumentFrag(BloodFatExceptionFrag(), tabName[3], mData4, mTime4))
            initTab()
            initViewPager()
        }

    }

    override fun setLayout() = binding.root

}