package com.zj365.health.fragment

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.DashPathEffect
import android.graphics.Matrix
import android.graphics.Typeface
import android.os.Build
import android.os.Bundle
import android.text.SpannableString
import android.text.Spanned
import android.text.style.AbsoluteSizeSpan
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.text.isDigitsOnly
import androidx.fragment.app.Fragment
import com.bigkoo.pickerview.adapter.ArrayWheelAdapter
import com.blankj.utilcode.util.TimeUtils
import com.contrarywind.view.WheelView
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.components.YAxis
import com.github.mikephil.charting.data.BarData
import com.github.mikephil.charting.data.BarDataSet
import com.github.mikephil.charting.data.BarEntry
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.data.LineData
import com.github.mikephil.charting.data.LineDataSet
import com.github.mikephil.charting.formatter.IFillFormatter
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.tamsiree.rxkit.RxTimeTool
import com.xty.base.fragment.BaseVmFrag
import com.xty.base.utils.OrganStatusCalcUtil
import com.xty.common.Const
import com.xty.common.LogUtils
import com.xty.common.TimeSelect
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.weight.tablayout.TabEntity
import com.xty.common.weight.tablayout.listener.CustomTabEntity
import com.xty.common.weight.tablayout.listener.OnTabSelectListener
import com.zj365.health.R
import com.zj365.health.act.HealthMainAct
import com.zj365.health.act.exception.BaseExceptionDataAct
import com.zj365.health.databinding.FragTemperBinding
import com.zj365.health.vm.TempVm
import com.zj365.health.weight.HealthDialog
import com.zj365.health.weight.IMarkView
import com.xty.network.model.TempBean
import java.lang.Float.max
import java.lang.Float.min
import java.text.SimpleDateFormat
import java.util.*

/**
 * 体温
 */
class TemFrag : BaseVmFrag<TempVm>() {

    val binding by lazy { FragTemperBinding.inflate(layoutInflater) }
    override fun setLayout(): View = binding.root
    override fun setViewModel() = TempVm()

    var isDemonstrate: Boolean = false
    var lineValue = arrayListOf<Entry>()  //曲线数据
    var barValue = arrayListOf<BarEntry>()  //柱状数据
    val fomartStr by lazy { arrayOf("HH:mm", "HH:mm", "MM-dd", "MM-dd", "yy-MM") }

    val weekStr by lazy { resources.getStringArray(R.array.week_day) }
    var type = 1

    //    val timeSelect by lazy {
//        TimeSelect(requireContext()) {
//            type=0
//            mViewModel.getTempData(type, binding.mTvTime.text.toString(),id)
//        }
//    }
    var tip = "手表采集的体温数值为手表接触的皮肤表面温度，因此受外界环境温度影响较大。如显示体温异常，请首先排除烤火、暴晒、沐浴、气温寒冷等因素。"
    val tipDialog by lazy {
        HealthDialog(requireContext(), true, tip) {
        }
    }
    private val icons = arrayOf(
        R.mipmap.shuzhuangtu,R.mipmap.ic_shuzhuangtu_yixuan, R.mipmap.ic_zhexiant,R.mipmap.zhexiant_yixuan
    )
    var id = ""

    private fun setNull() {
        lineValue.clear()
        initChart(mutableListOf(), 32f, 42f)
    }

    override fun initData() {
        super.initData()
        id = arguments?.getString("id").toString()
    }
    private fun moveToErrorShow(bundle: Bundle) {
        RouteManager.goAct(ARouterUrl.TIWEN_EXCEPTION_DATA_ACT, bundle)
    }
    var highList = ArrayList<String>()
    var highListTime = ArrayList<String>()
    override fun initView() {
        super.initView()
        initTab()
        initTimeSelect()
        binding.ivSecond.setOnClickListener {
            val text = binding.tvSecondName.text.toString()
            bundle.clear()
            bundle.putString(BaseExceptionDataAct.exceptionKey, text)
            bundle.putStringArrayList(BaseExceptionDataAct.exceptionList, highList)
            bundle.putStringArrayList(BaseExceptionDataAct.exceptionListTime, highListTime)
            moveToErrorShow(bundle)
        }

        binding.mLineChart.setNoDataText(getString(R.string.line_chart_not_data))
        binding.mLineChart.setNoDataTextColor(
            ContextCompat.getColor(
                requireContext(),
                R.color.col_455
            )
        )
        binding.compareTv.setOnClickListener {
            if (isDemonstrate){
                (requireActivity() as HealthMainAct).dialogBind.show()
                return@setOnClickListener
            }
            bundle.clear()
            bundle.putString("id", id)
            bundle.putString("type", "4")//4 温度
            RouteManager.goAct(ARouterUrl.COMPARE_DATA_ACT, bundle)
        }
        binding.mTip.setOnClickListener {
            tipDialog.show()
        }
        binding.zhexianTv.setOnClickListener{
            binding.mLineChart.visibility = View.VISIBLE
            binding.mBarChart.visibility = View.GONE
            binding.zhuzhuanTv.setImageResource(icons[0])
            binding.zhexianTv.setImageResource(icons[3])
        }
        binding.zhuzhuanTv.setOnClickListener{
            binding.mLineChart.visibility = View.GONE
            binding.mBarChart.visibility = View.VISIBLE
            binding.zhuzhuanTv.setImageResource(icons[1])
            binding.zhexianTv.setImageResource(icons[2])
        }
        binding.mGroup.setOnCheckedChangeListener { group, checkedId ->
            when (checkedId) {
                R.id.mDay -> {
                    binding.mTvTime.visibility = View.VISIBLE
                    var selTime = binding.mTvTime.text.toString()
                    val date2String = RxTimeTool.date2String(Date(), SimpleDateFormat("yyyy-MM-dd"))
                    type = if (selTime == date2String) {   //是当天
                        1
                    } else {
                        0
                    }
                }
                R.id.mWeek -> {
                    binding.mTvTime.visibility = View.GONE
                    type = 2
                }
                R.id.mMonth -> {
                    binding.mTvTime.visibility = View.GONE
                    type = 3
                }
                R.id.mYear -> {
                    binding.mTvTime.visibility = View.GONE
                    type = 4
                }
            }
            setNull()
            setAvgInfo(null)
            if (type < 2) {
                mViewModel.getTempData(type, binding.mTvTime.text.toString(), id, isDemonstrate)
            } else {
                mViewModel.getTempData(type, null, id, isDemonstrate)


            }

        }
        isDemonstrate = (requireActivity() as HealthMainAct).isDemonstrate
        if (isDemonstrate){
            binding.tvXinLv.text = "36.4"
            binding.tvTestTime.text = RxTimeTool.getCurTimeString(
                SimpleDateFormat("HH:mm")
            )
        }else{
            mViewModel.getLastData(id)
        }
        mViewModel.getTempData(
            type,
            RxTimeTool.getCurTimeString(SimpleDateFormat("yyyy-MM-dd")),
            id,
            isDemonstrate
        )
        mViewModel.getSetting(Const.TEMP_SETTING)
        binding.mTvTime.text = RxTimeTool.getCurTimeString(SimpleDateFormat("yyyy-MM-dd"))
        binding.mTvTime.setOnClickListener {
            timeSelect.selectTime(it)
        }
        binding.tvTips.text = "体温：\n" +
                "了解体温：体温指人体的温度，是生命体征之一，也是临床常用的基础检查。在生理学上，体温可分为体核温度和体表温度。平常所指的体温就是体核温度，即机体核心部分的平均温度。\n\n" +
                "体温异常代表什么？\n" +
                "提示可能存在炎症反应、代谢紊乱、内环境失衡、体温调节中枢功能障碍等，体温升高常见于感染、白血病、类风湿关节炎等疾病，体温降低主要见于低血糖昏迷、甲状腺功能减退、全身衰竭等。\n\n" +
                "高风险值:\n" +
                "高于39.0℃可能引起昏迷、惊厥、造成不可逆脑损伤等，低于35℃则可危及生命。\n\n\n\n" +
                "测量结果仅供参考，不作为医疗诊断依据。"
    }

    override fun observer() {
        mViewModel.data.observe(this) {
            if (it == null || it[type] == null) {
                return@observe
            }
            // 这里把日的原始数据暂存起来，跳转异常页面时使用
            val daysModelList: MutableList<TempBean.Model> = it[type]!!.model
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                when (type) {
                    1 -> {//日
                        it[type]!!. model = it[type]!!.mapDataToTimeNodes(it[type]!!.model)
                    }
                    2 -> {//周
                        val split = binding.tvShowDate.text.toString().split("至")
                        val start = split[0]
                        val end = split[1]
                        it[type]!!.model = it[type]!!.fillMissingData(it[type]!!.model, start, end)
                    }
                    3 -> {//月
                        it[type]!!. model = it[type]!!.fillMissingData(it[type]!!.model, binding.tvShowDate.text.toString())
                    }
                    4 -> {//年
                        it[type]!!. model = it[type]!!.fillMissingData(it[type]!!.model)
                    }
                }
            }
            setLine(daysModelList, it[type])
        }
        mViewModel.setting.observe(this) {
            if (it.data.isNotEmpty()) {
                binding.mDesc.text = "${it.data[0].name}：${it.data[0].content}"
            }
        }
        mViewModel.lastData.observe(this) {
            it.data?.let {
                binding.tvXinLv.text = "${String.format("%.1f", it.tempInt + (it.tempFloat * 0.1))}"
                binding.tvTestTime.text = RxTimeTool.milliseconds2String(
                    it.time!!.toLong() * 1000,
                    SimpleDateFormat("HH:mm")
                )
            }
        }
    }


    private fun setAvgInfo(bean: TempBean.Value?) {
        if (bean == null) {
            binding.mAvg.setValue("-")
            binding.mMax.setValue("-")
            binding.mLow.setValue("-")
        } else {

            binding.mAvg.setValue(String.format("%.1f", bean.avg))
            binding.mMax.setValue(String.format("%.1f", bean.max))
            binding.mLow.setValue(String.format("%.1f", bean.min))
        }
    }

    //设置数据
    private fun setLine(daysModelList: MutableList<TempBean.Model>, data: TempBean?) {
        if (data == null) {
            return
        }
        setAvgInfo(data.value)
        var list = data.model
        lineValue.clear()
        barValue.clear()
        var rever = (list.indices).reversed()

        if (selectDay()) {
            normalTime = 0
            highTime = 0
            highList.clear()
            highListTime.clear()
        }
        for (t in daysModelList.indices) {
            if (selectDay()) {//如果是当天才操作
                getOrganStatu(daysModelList[daysModelList.size - t - 1])
            }
        }
        for (i in rever) {
            var bean = list[list.size - i - 1]
//            var times = (bean.time!!).toLong() * 1000
//            var strTime =RxTimeTool.milliseconds2String(times, SimpleDateFormat(fomartStr[type]))

            lineValue.add(Entry(((list.size - 1) - i).toFloat(), bean.temp,  tipTime(list[list.size - i - 1])))
            barValue.add(BarEntry(((list.size - 1) - i).toFloat(), bean.temp,  tipTime(list[list.size - i - 1])))
        }

        if (selectDay()) {
            setOrganStatu()
        }
        initChart(list, data.value.min, data.value.max)
    }

    private fun tipTime(
        bean: TempBean.Model
    ): String {
        when (type) {
            1,3 -> {
                if (bean.temp == 0f) {
                    bean.temp = Float.NaN
                }
                return bean.time
            }
            2 -> {
                val parts = bean.time.split("-")
                val year = parts[0]
                val month = parts[1]
                val day = parts[2]

                return "$month-$day"
            }

            else -> {
                return "${bean.time}月"
            }
        }
    }
    
    private fun setOrganStatu() {

        binding.tvFirstName.text = "正常"
        binding.tvFirstValue.text = setSpan(normalTime, 0)

        binding.tvSecondName.text = "高温"
        binding.tvSecondValue.text = setSpan(highTime, 1)
        if (normalTime == 0){
            binding.ivFirst.visibility  = View.GONE
        } else {
            binding.ivFirst.visibility  = View.VISIBLE
        }
        if (highTime == 0){
            binding.ivSecond.visibility  = View.GONE
        } else {
            binding.ivSecond.visibility  = View.VISIBLE
        }

//        if (highTime > 30) {
//            binding.ivSecond.setImageResource(R.mipmap.icon_error)
//        } else {
//            binding.ivSecond.setImageResource(R.mipmap.icon_warning)
//        }
    }

    private fun setSpan(time: Int, organStatu: Int): SpannableString {
        var statuSpan = SpannableString("${time}次")

        statuSpan.setSpan(
            ForegroundColorSpan(
                //设置字体颜色
                ContextCompat.getColor(
                    requireContext(), R.color.black
                )
            ), 0, statuSpan.length - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        //设置字体大小(绝对值,单位：像素),第二个参数boolean dip，如果为true，表示前面的字体大小单位为dip，否则为像素
        statuSpan.setSpan(
            AbsoluteSizeSpan(24, true),
            0, statuSpan.length - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        //设置字体样式: NORMAL正常，BOLD粗体，ITALIC斜体，BOLD_ITALIC粗斜体
        statuSpan.setSpan(
            StyleSpan(Typeface.BOLD),
            0, statuSpan.length - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        );

        return statuSpan
    }

    /**
     * 带ImageSpan图片设置
     */
//    private fun setSpan(time: Int, organStatu: Int): SpannableString {
//        var imageStr = "\t\t$organStatu"//imageSpan 占位符
//        var statuSpan = SpannableString("${time}次$imageStr")
//
//        statuSpan.setSpan(
//            ForegroundColorSpan(
//                //设置字体颜色
//                ContextCompat.getColor(
//                    requireContext(), R.color.white)
//            ),0, statuSpan.length - imageStr.length - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
//        //设置字体大小(绝对值,单位：像素),第二个参数boolean dip，如果为true，表示前面的字体大小单位为dip，否则为像素
//        statuSpan.setSpan(
//            AbsoluteSizeSpan(24, true),
//            0, statuSpan.length - imageStr.length - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
//        //设置字体样式: NORMAL正常，BOLD粗体，ITALIC斜体，BOLD_ITALIC粗斜体
//        statuSpan.setSpan(
//            StyleSpan(Typeface.BOLD),
//            0, statuSpan.length - imageStr.length - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//
//
//        var image = requireContext().getDrawable(
//            when (organStatu) {
//                0 ->
//                    R.mipmap.icon_normal
//                else ->
//                    R.mipmap.icon_error
//            }
//        )
//        image?.setBounds(0, 0, image.minimumWidth, image.minimumHeight)
//        statuSpan.setSpan(image?.let { ImageSpan(it, ImageSpan.ALIGN_CENTER) },
//            statuSpan.length - 1, statuSpan.length,
//            Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
//
//        return statuSpan
//    }


    /**
     * 是否是选的日，只有选日的时候才会刷新正常，异常状态
     */
    private fun selectDay(): Boolean {
        return type < 2
    }

    var normalTime: Int = 0
    var highTime: Int = 0
    private fun getOrganStatu(model:TempBean.Model) {
        when (OrganStatusCalcUtil.tempStatu(model.temp)) {//体温状态值 0正常 1低热 2中热 3高热 4超高热 5无效值
            0 ->
                normalTime++
            1, 2, 3, 4 ->
            {
                highTime++
                highList.add(model.temp.toString())
                highListTime.add(model.time.toString())
            }

        }
    }


    /**
     * 周数据
     */
    private fun getWeeks(time: Long): String {
        var calendear = Calendar.getInstance()
        var strData = RxTimeTool.milliseconds2String(time, SimpleDateFormat("yyyy-MM-dd"))
        var times = strData.split("-")
        calendear.set(times[0].toInt(), times[1].toInt() - 1, times[2].toInt())
        var intWeek = calendear.get(Calendar.DAY_OF_WEEK)
        return weekStr[intWeek - 1]
    }

    private fun initChart(list: MutableList<TempBean.Model>, min: Float, max: Float) {
        binding.mLineChart.let {
            it.clear()
            it.setNoDataText(getString(R.string.line_chart_not_data))
            it.setNoDataTextColor(ContextCompat.getColor(requireContext(), R.color.col_455))
            it.setLayerType(View.LAYER_TYPE_SOFTWARE, null)

            it.setBackgroundColor(Color.WHITE) ///color
            it.description.isEnabled = false
            it.setTouchEnabled(true)
            it.isDragEnabled = false
            it.setScaleEnabled(false)
            it.setPinchZoom(false)

            it.setDrawGridBackground(false)
            it.maxHighlightDistance = 300f


            it.isDragEnabled = true
            val m = Matrix()
            m.postScale(1.5f, 1f)
            it.viewPortHandler.refresh(m, it, false)
            it.isScaleXEnabled = true                 //支持x轴缩放
            it.isScaleYEnabled = false

            val x = it.xAxis
            x.granularity = 1f  //精准设置
            x.axisMinimum
            x.textColor = ContextCompat.getColor(requireContext(), R.color.col_92a)
            x.isEnabled = true
            x.setDrawGridLines(false)  //不绘制y
            x.setDrawAxisLine(false)
            x.position = XAxis.XAxisPosition.BOTTOM
            x.setValueFormatter { value, axis ->
                var index = value.toInt()
                //Log.e("x轴", "$value")
                if (index < 0 || index >= lineValue.size) {
                    ""
                } else {
                    "${lineValue[value.toInt()].data}"
                }
            }
            //y轴
            val y = it.axisLeft
//            y.axisMaximum=max
//            y.axisMinimum=min
            y.setLabelCountAndMaxMinValue(6, false, min-0.5f, max+0.5f)
//            y.axisMinimum =  35.5f
//            y.axisMaximum =   37.9f
//
//// 设置 Y 轴标签的数量为 5
//            y.setLabelCount(6, false)

            y.textColor = ContextCompat.getColor(requireContext(), R.color.col_92a)
            y.setDrawAxisLine(false)
            y.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART)
            y.gridColor = ContextCompat.getColor(requireContext(), R.color.col_0F2)
            y.setGridDashedLine(DashPathEffect(floatArrayOf(5f, 0f), 0f))
            y.setDrawGridLines(true)  //绘制横者的x轴
            y.setDrawZeroLine(true)


            var markerView =
                IMarkView(requireContext(), R.layout.mark_view, "℃", IMarkView.MarkPage.Temp)
            it.marker = markerView
            markerView.chartView = it
            it.axisRight.isEnabled = false
            it.legend.isEnabled = false
            //   it.animateXY(2000, 2000)  //设置xy轴进入的动画
            it.invalidate()
        }

        val barsetsNew: java.util.ArrayList<ILineDataSet> = java.util.ArrayList()
        var segments = splitDataIntoSegments(list)
        if (segments.size > 0) {
            segments = checkData(segments)
        }

        val dataSets = segments.mapNotNull { segment ->
            if (segment.isEmpty()) return@mapNotNull null

            val entries = segment.mapIndexed { index, model ->
                Entry(
                    convertTimeToIndex(model.time, index).toFloat(),
                    model.temp,
                    lineValue[convertTimeToIndex(model.time, index)].data
                )
            }
            return@mapNotNull setData(entries)
        }
        if (lineValue.isNotEmpty()) {
            barsetsNew.addAll(dataSets)
            val bard = LineData(barsetsNew)
            binding.mLineChart.data = bard
            binding.mLineChart.moveViewToX(lineValue[lineValue.size - 1].x)
        }

        binding.mBarChart.let {
            it.clear()
            it.setNoDataText(getString(R.string.line_chart_not_data))
            it.setNoDataTextColor(ContextCompat.getColor(requireContext(), R.color.col_455))
            it.setLayerType(View.LAYER_TYPE_SOFTWARE, null)

            it.setBackgroundColor(Color.WHITE) ///color
            it.description.isEnabled = false
            it.setTouchEnabled(true)
            it.setPinchZoom(true)//柱形图的横坐标缩放，

            it.setDrawGridBackground(false)//背景网格是否绘制
            it.maxHighlightDistance = 300f//设置高亮（Highlight）的最大距离


            it.isDragEnabled = true//是否启用拖动（drag）功能。
            val m = Matrix()
            m.postScale(
                when (type) {
                    1 -> {
                        11.5f
                    }

                    3 -> {
                        3.5f
                    }

                    else -> {
                        1f
                    }
                }, 0f
            )
            it.viewPortHandler.refresh(m, it, false)
            it.isScaleXEnabled = true  //支持x轴缩放
            it.isScaleYEnabled = false //支持Y轴缩放

            val x = it.xAxis
            x.granularity = 1f  //精准设置
            x.textColor = ContextCompat.getColor(requireContext(), R.color.col_92a)
            x.textSize = 9f
            x.axisMinimum = -0.5f
            x.axisMaximum = (barValue.size - 0.5).toFloat()

//                x.labelCount = 6
            x.isEnabled = true
            x.setDrawGridLines(false)  //不绘制y
            x.setDrawAxisLine(false)
            x.xOffset = 0f
            x.position = XAxis.XAxisPosition.BOTTOM
            x.setValueFormatter { value, axis ->
                var index = value.toInt()
                //Log.e("x轴", "$value")
                if (index < 0 || index >= barValue.size) {
                    ""
                } else {
                    "${barValue[value.toInt()].data}"
                }
            }
            val y = it.axisLeft
            y.setLabelCountAndMaxMinValue(6, false, min - 0.5f, max + 0.5f)
            y.textColor = ContextCompat.getColor(requireContext(), R.color.col_92a)
            y.setDrawAxisLine(false)
            y.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART)
            y.gridColor = ContextCompat.getColor(requireContext(), R.color.col_0F2)
            y.setGridDashedLine(DashPathEffect(floatArrayOf(5f, 0f), 0f))
            y.setDrawGridLines(true)  //绘制横者的x轴
            y.setDrawZeroLine(true)

            var markerView = IMarkView(
                requireContext(), R.layout.mark_view, "℃",
                IMarkView.MarkPage.Temp
            )
            it.marker = markerView
            markerView.chartView = it
            it.axisRight.isEnabled = false
            it.legend.isEnabled = false
            //   it.setVisibleXRangeMaximum(10f)
            //   it.animateXY(2000, 2000)  //设置xy轴进入的动画

        }

        if (barValue.isEmpty()) {
            return
        }
        binding.mBarChart.moveViewToX(barValue[barValue.size - 1].x)

        val dataBarSets = segments.mapNotNull { segment ->
            if (segment.isEmpty()) return@mapNotNull null

            val entries = segment.mapIndexed { index, model ->
                BarEntry(
                    convertTimeToIndex(model.time, index).toFloat(),
                    model.temp,
                    barValue[convertTimeToIndex(model.time, index)].data
                )
            }
            return@mapNotNull setBarData(entries)
        }

        val bard = BarData(dataBarSets)
        bard.barWidth = calculateBarWidth(bard.dataSetCount)
        binding.mBarChart.data = bard
        binding.mBarChart.invalidate()
    }

    private fun setBarData(entries: List<BarEntry>): BarDataSet {
        val set1: BarDataSet
        if (binding.mBarChart.data != null && binding.mBarChart.data.dataSetCount > 0) {
            set1 = binding.mBarChart.data.getDataSetByIndex(1) as BarDataSet
            set1.entries = entries
            binding.mBarChart.data.notifyDataChanged()
            binding.mBarChart.notifyDataSetChanged()
        } else {
            set1 = BarDataSet(entries, "DataSet 2")
            set1.setDrawValues(false) // 不显示值
            set1.valueTextColor = ContextCompat.getColor(requireContext(), R.color.col_9BDd)
            set1.highLightColor =
                ContextCompat.getColor(requireContext(), R.color.col_EFF)  //设置点后高亮颜色
            set1.color = ContextCompat.getColor(requireContext(), R.color.col_9BDd) //xian de yanse
            val data = BarData(set1)
            data.setValueTextSize(9f)
            data.setDrawValues(false)
        }
        return set1
    }

    private fun calculateBarWidth(dataCount: Int): Float {
        val maximumBarWidth = 0.3f // 设置最大柱形图宽度
        val minimumBarWidth = 0.3f // 设置最小柱形图宽度

        val defaultBarWidth = 0.3f // 默认柱形图宽度
        val availableWidth = 1.0f - (dataCount + 1) * 0.1f // 可用宽度减去间距占用

        val calculatedBarWidth = availableWidth / dataCount.toFloat()
        return max(minimumBarWidth, min(maximumBarWidth, calculatedBarWidth))
    }

    fun splitDataIntoSegments(dataList: MutableList<TempBean.Model>): MutableList<MutableList<TempBean.Model>> {
        val segments = mutableListOf<MutableList<TempBean.Model>>()
        var currentSegment = mutableListOf<TempBean.Model>()

        for (model in dataList) {
            if (model.temp.toFloat().isNaN()) {
                // 遇到 Float.NaN，如果当前线段不为空，则添加到结果中，并开始新的线段
                if (currentSegment.isNotEmpty()) {
                    segments.add(currentSegment)
                    currentSegment = mutableListOf()
                }
            } else {
                // 遇到有效数据，将其添加到当前线段中
                currentSegment.add(model)
            }
        }

        // 将最后一个线段添加到结果中
        if (currentSegment.isNotEmpty()) {
            segments.add(currentSegment)
        }

        return segments
    }
    private fun checkData(segmentsCompare: MutableList<MutableList<TempBean.Model>>): MutableList<MutableList<TempBean.Model>> {
        when(type){
            1->{
                if (segmentsCompare[0][0].time != "00:00"){
                    val d = mutableListOf<TempBean.Model>()
                    val b = TempBean.Model(0f, 0f, 0f, "00:00")
                    d.add(b)
                    segmentsCompare.add(d)
                }
                if (!TimeUtils.isToday(
                        binding.tvShowDate.text.toString(),
                        SimpleDateFormat("yyyy-MM-dd")
                    ) &&
                    segmentsCompare[segmentsCompare.size - 1][segmentsCompare[segmentsCompare.size - 1].size - 1].time != "23:50"
                ) {
                    val d = mutableListOf<TempBean.Model>()
                    val b = TempBean.Model(0f, 0f, 0f, "23:50")
                    d.add(b)
                    segmentsCompare.add(d)
                }
            }
            2 ->{
                val split = binding.tvShowDate.text.toString().split("至")
                val start = split[0]
                val end = split[1]

                if (segmentsCompare[0][0].time != start) {
                    val d = mutableListOf<TempBean.Model>()
                    val b = TempBean.Model(0f, 0f, 0f, start)
                    d.add(b)
                    segmentsCompare.add(d)
                }
                if (segmentsCompare[segmentsCompare.size - 1][segmentsCompare[segmentsCompare.size - 1].size - 1].time != end) {
                    val d = mutableListOf<TempBean.Model>()
                    val b = TempBean.Model(0f, 0f, 0f, end)
                    d.add(b)
                    segmentsCompare.add(d)
                }
            }
            3 -> {
                val split = binding.tvShowDate.text.toString().split("-")
                val year = split[0]//2023
                val month = split[1]//08

                val calendar = Calendar.getInstance()
                calendar.set(Calendar.YEAR, year.toInt())
                calendar.set(Calendar.MONTH, month.toInt() - 1) // Calendar.MONTH is 0-based
                val daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH)

                if (segmentsCompare[0][0].time != "$month-01") {
                    val d = mutableListOf<TempBean.Model>()
                    val b = TempBean.Model(0f, 0f, 0f, "$month-01")
                    d.add(b)
                    segmentsCompare.add(d)
                }
                if (!isCurYearMonth(binding.tvShowDate.text.toString(), SimpleDateFormat("yyyy-MM")) &&
                    segmentsCompare[segmentsCompare.size - 1][segmentsCompare[segmentsCompare.size - 1].size - 1].time != "$month-$daysInMonth") {
                    val d = mutableListOf<TempBean.Model>()
                    val b = TempBean.Model(0f, 0f, 0f, "$month-$daysInMonth")
                    d.add(b)
                    segmentsCompare.add(d)
                }
            }
            4 -> {
                if (segmentsCompare[0][0].time != "01") {
                    val d = mutableListOf<TempBean.Model>()
                    val b = TempBean.Model(0f, 0f, 0f, "01")
                    d.add(b)
                    segmentsCompare.add(d)
                }
                if (segmentsCompare[segmentsCompare.size - 1][segmentsCompare[segmentsCompare.size - 1].size - 1].time != "12") {
                    val d = mutableListOf<TempBean.Model>()
                    val b = TempBean.Model(0f, 0f, 0f, "12")
                    d.add(b)
                    segmentsCompare.add(d)
                }
            }
        }


        return segmentsCompare
    }

    private fun isCurYearMonth(dateString: String, dateFormat: SimpleDateFormat): Boolean {
        val date = dateFormat.parse(dateString)
        if (date == null) {
            LogUtils.w("isCurYearMonth, date == null")
            return false
        }
        val calendar = Calendar.getInstance()
        calendar.time = date

        val currentYear = Calendar.getInstance().get(Calendar.YEAR)
        val currentMonth = Calendar.getInstance().get(Calendar.MONTH)

        val isCurrentYear = calendar.get(Calendar.YEAR) == currentYear
        val isCurrentMonth = calendar.get(Calendar.MONTH) == currentMonth

        return isCurrentYear && isCurrentMonth
    }

    fun convertTimeToIndex(time: String, cur: Int): Int {
        var index = 0
        when (type) {
            1 -> {
                // 将时间格式转换为小时和分钟
                val parts = time.split(":")
                val hours = parts[0].toInt()
                val minutes = parts[1].toInt()

                // 计算对应的分钟数
                val totalMinutes = hours * 60 + minutes

                // 计算对应的索引值
                index = totalMinutes / 10
            }
            2-> {
                val split = binding.tvShowDate.text.toString().split("至")
                val rangeStart = split[0]
                val rangeEnd = split[1]
                val formatter = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
                try {
                    val startDate = formatter.parse(rangeStart)
                    val endDate = formatter.parse(rangeEnd)
                    val inputDateObj = formatter.parse(time)
                    if (startDate == null || endDate == null || inputDateObj == null) {
                        LogUtils.w("data formatter result is null")
                        return 0
                    }
                    val calendar = Calendar.getInstance()
                    calendar.time = startDate
                    while (calendar.time <= endDate) {
                        if (calendar.time == inputDateObj) {
                            println("Index: $index")
                            break
                        }
                        calendar.add(Calendar.DATE, 1)
                        index++
                    }

                } catch (e: java.text.ParseException) {
                    e.printStackTrace()
                }
            }
            3 -> {
                // 将08-01格式转换为序号
                val parts = time.split("-")
                val month = parts[0].toInt()
                val day = parts[1].toInt()
                index = day - 1
            }

            else -> {
                index = if (time.isDigitsOnly()) (time.toInt() - 1) else 0
            }
        }

        return if(index < 0 ) cur+1 else index
    }

    private fun setData(entries: List<Entry>?): LineDataSet {
        val set1: LineDataSet
        if (binding.mLineChart!!.data != null && binding.mLineChart!!.data.dataSetCount > 0) {
            set1 = binding.mLineChart!!.data.getDataSetByIndex(0) as LineDataSet
            set1.values = entries
            binding.mLineChart!!.data.notifyDataChanged()
            binding.mLineChart!!.notifyDataSetChanged()
        } else {
            // create a dataset and give it a type
            set1 = LineDataSet(entries, "DataSet 1")
            set1.mode = LineDataSet.Mode.LINEAR
            set1.cubicIntensity = 0.2f
            set1.setDrawFilled(true)
            set1.lineWidth = 2.5f
            // 分段后，数据若有一个点需要显示
            set1.setDrawCircles(entries?.size == 1)
            set1.circleRadius = 2f
            set1.highLightColor =
                ContextCompat.getColor(requireContext(), R.color.col_e0b)  //设置点后高亮颜色
            set1.enableDashedHighlightLine(10f, 5f, 0f)
            set1.setCircleColor(
                ContextCompat.getColor(
                    requireContext(),
                    R.color.col_41c
                )
            )    //#41C4AF
            set1.color = ContextCompat.getColor(requireContext(), R.color.col_41c) //xian de yanse
            val drawable = ContextCompat.getDrawable(requireContext(), R.drawable.shape_curve)
            set1.fillDrawable = drawable
            //   set1.setFillColor(Color.RED);
            set1.fillAlpha = 100
            set1.setDrawHorizontalHighlightIndicator(false)
            set1.fillFormatter =
                IFillFormatter { dataSet, dataProvider -> binding.mLineChart!!.axisLeft.axisMinimum }

            set1.setValueFormatter { value, entry, dataSetIndex, viewPortHandler ->

                "$value"
            }
            // create a data object with the data sets
            val data = LineData(set1)
            //data.setValueTypeface(tfLight)
            data.setValueTextSize(9f)
            data.setDrawValues(false)

            // set data
            //binding.mLineChart!!.data = data
        }
        return set1
    }

    var dayDate = ""
    var weekDate = ""
    var mouthDate = ""
    var yearDate = ""
    private fun getDateAdd(days: Int): Date? {
        val c = Calendar.getInstance()
        c.add(Calendar.DAY_OF_MONTH, -days)
        return c.time
    }

    private val mTabEntities: ArrayList<CustomTabEntity> = ArrayList()
    private val mFragments: SparseArray<Fragment> = SparseArray()
    private val mTitles = arrayOf(
        "日", "周", "月", "年"
    )

    private fun initTab() {

        val currentDay = Date()
        dayDate = TimeUtils.date2String(currentDay, "yyyy-MM-dd")
        weekDate = "${TimeUtils.date2String(getDateAdd(6), "yyyy-MM-dd")}至$dayDate"
        mouthDate = TimeUtils.date2String(currentDay, "yyyy-MM")
        yearDate = TimeUtils.date2String(currentDay, "yyyy")
        mTabEntities.clear()
        mFragments.clear()
        mTitles.forEach {
            mTabEntities.add(
                TabEntity(
                    it, android.R.drawable.ic_delete, android.R.drawable.ic_delete
                )
            )
        }
        binding.commonTabLayout.setTabData(mTabEntities)
        binding.commonTabLayout.setOnTabSelectListener(object : OnTabSelectListener {
            @SuppressLint("SetTextI18n")
            override fun onTabSelect(position: Int) {
                binding.commonTabLayout.currentTab = position

                when (position) {
                    0 -> {
                        binding.tvShowDate.text = dayDate
                        type = 1
                    }
                    1 -> {
                        binding.tvShowDate.text = weekDate
                        type = 2
                    }
                    2 -> {
                        binding.tvShowDate.text = mouthDate
                        type = 3
                    }
                    3 -> {
                        binding.tvShowDate.text = yearDate
                        type = 4
                    }
                }
                mViewModel.getTempData(type, binding.tvShowDate.text.toString(), id, isDemonstrate)
            }

            override fun onTabReselect(position: Int) {
            }

        })

    }

    private fun initTimeSelect() {
        //日筛选
        val calendar = Calendar.getInstance()
        timeSelect.startDate.set(1922, 1, 1)
        timeSelect.endDate.set(
            calendar[Calendar.YEAR], calendar[Calendar.MONTH], if (calendar[Calendar.HOUR] >= 11) {
                calendar[Calendar.DATE]
            } else {
                calendar[Calendar.DATE]
            }
        )
        binding.tvShowDate.text = RxTimeTool.getCurTimeString(SimpleDateFormat("yyyy-MM-dd"))

        binding.tvShowDate.setOnClickListener {
            when (type) {
                1 -> {
                    timeSelect.selectTime(it, "yyyy-MM-dd")
                }
                2 -> {
                    bottomSheetDialog?.show()
                }
                3 -> {
                    timeMonthSelect.selectTimeWithYearMonth(it, "yyyy-MM")
                }
                4 -> {
                    timeYearSelect.selectTimeWithYear(it, "yyyy")
                }
            }
        }

        binding.ivPosition.setOnClickListener {
            when (type) {
                1 -> {
                    timeSelect.selectTime( binding.tvShowDate, "yyyy-MM-dd")
                }
                2 -> {
                    bottomSheetDialog?.show()
                }
                3 -> {
                    timeMonthSelect.selectTimeWithYearMonth( binding.tvShowDate, "yyyy-MM")
                }
                4 -> {
                    timeYearSelect.selectTimeWithYear( binding.tvShowDate, "yyyy")
                }
            }
        }
        initBottomDialog()
    }

    private var bottomSheetDialog: BottomSheetDialog? = null

    fun initBottomDialog() {
        if (bottomSheetDialog == null) {
            bottomSheetDialog = BottomSheetDialog(this.requireContext())
            bottomSheetDialog?.let { dialog ->
                val view = LayoutInflater.from(context).inflate(R.layout.bottom_sheet_layout, null)
                dialog.setContentView(view)
                val wheelView = view.findViewById<WheelView>(R.id.wheelView)
                wheelView.setCyclic(false)
                wheelView.adapter = ArrayWheelAdapter(buildWeekLower26())
                val windowHeight = (resources.displayMetrics.heightPixels * 0.6).toInt()
                dialog.behavior.peekHeight = windowHeight

                dialog.behavior.isHideable = false
                dialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED

                val cancelButton = view.findViewById<TextView>(R.id.cancel_button)
                val confirmButton = view.findViewById<TextView>(R.id.confirm_button)

                cancelButton.setOnClickListener {
                    // 点击取消按钮的操作
                    dialog.dismiss()
                }

                confirmButton.setOnClickListener {
                    // 点击确定按钮的操作
                    val item = wheelView.adapter.getItem(wheelView.currentItem)
                    binding.tvShowDate.text = item.toString()
                    weekDate = binding.tvShowDate.text.toString()
                    mViewModel.getTempData(
                        type,
                        binding.tvShowDate.text.toString(),
                        id,
                        isDemonstrate
                    )
                    // 点击确定按钮的操作
                    dialog.dismiss()
                }
            }
        }
    }

    fun buildWeekLower26(): MutableList<String> {
        val startDate = Calendar.getInstance()
        startDate.firstDayOfWeek = Calendar.MONDAY
        startDate.add(Calendar.YEAR, -1)
        val endDate = Calendar.getInstance()

        val dateRanges: MutableList<String> = ArrayList()
        var currentMonday = startDate.clone() as Calendar
        currentMonday.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY)

        while (currentMonday.before(endDate)) {
            val currentSunday = currentMonday.clone() as Calendar
            currentSunday.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY)

            val formattedDateRange =
                SimpleDateFormat("yyyy-MM-dd").format(currentMonday.time) + "至" + SimpleDateFormat("yyyy-MM-dd").format(
                    currentSunday.time
                )
            dateRanges.add(formattedDateRange)

            currentMonday.add(Calendar.WEEK_OF_YEAR, 1)
        }

        dateRanges.reverse()
        return dateRanges
    }

    val timeSelect by lazy {
        TimeSelect(requireContext()) {
            type = 1
            dayDate = binding.tvShowDate.text.toString()
            mViewModel.getTempData(type, binding.tvShowDate.text.toString(), id, isDemonstrate)
        }
    }

    val timeMonthSelect by lazy {
        TimeSelect(requireContext()) {
            type = 3
            mouthDate = binding.tvShowDate.text.toString()
            mViewModel.getTempData(type, binding.tvShowDate.text.toString(), id, isDemonstrate)
//            mViewModel.getXlData(type, binding.tvShowDate.text.toString(), id)

        }
    }
    val timeYearSelect by lazy {
        TimeSelect(requireContext()) {
            type = 4
            yearDate = binding.tvShowDate.text.toString()
            mViewModel.getTempData(type, binding.tvShowDate.text.toString(), id, isDemonstrate)
        }
    }
}