package com.zj365.health.weight

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import androidx.core.content.ContextCompat
import com.zj365.health.R
import com.xty.network.model.ReportBean

class DayHealthView(context: Context, attrs: AttributeSet?) : View(context, attrs) {

    val healthCol by lazy {
        arrayOf(
            R.color.col_EFF,
            R.color.col_7b8,
            R.color.col_9BDd,
            R.color.col_9BD,
            R.color.col_FDA481,
        )
    }
    val healthStr by lazy { context.resources.getStringArray(R.array.status_array) }
    var downX = -1f
    var format = 10

    /**
     * 是否器官趋势图
     */
    private var isOrgan = false

    private var list = mutableListOf<ReportBean.ChildStatus>()

    val paint by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.FILL_AND_STROKE

        }
    }
    val paintTxt by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.FILL_AND_STROKE
            color = ContextCompat.getColor(context, R.color.col_455)
            textSize = MyTextSize.toFloat()

        }
    }
    val paintBg by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.FILL_AND_STROKE
            color = ContextCompat.getColor(context, R.color.transparent_50)
            textSize = MyTextSize.toFloat()

        }
    }


    var MyTextSize = 0

    init {
        var recycle = context.obtainStyledAttributes(attrs, R.styleable.DayHealthView)
        MyTextSize = recycle.getDimensionPixelSize(R.styleable.DayHealthView_txt_size, 0)
        recycle.recycle()
    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        if (list.isNullOrEmpty()) {
            val rectf =
                RectF(0f, paintTxt.descent() - paintTxt.ascent(), width.toFloat(), height.toFloat())
            paint.color = ContextCompat.getColor(context, R.color.col_c3d)
            canvas.drawRoundRect(rectf, 18f, 0f, paint)
            return
        }

        val avgWidth = width / list.size.toFloat()

        var startWidth = 0f
        val descHeight = paintTxt.descent() - paintTxt.ascent()
        val startHeight = descHeight

        for (i in 0 until list.size) {
//            if (i>0&&healthCol[getHealthStatus(list[i-1])]!=healthCol[getHealthStatus(list[i])]){
//                startWidth += 2
//            }
            val rectf = RectF(startWidth, startHeight, startWidth + avgWidth, height.toFloat())
            paint.color = ContextCompat.getColor(context, healthCol[getHealthStatus(list[i])])
            canvas.drawRoundRect(rectf, 18f, 0f, paint)

            if (downX > -1f) {
                if (downX > startWidth && downX <= startWidth + avgWidth) {


                    val str = "时间：${
                        list[i].createTime.substring(
                            0,
                            format
                        )
                    }  状态：${healthStr[getHealthStatus(list[i])]}"
                    val descWidth = paintTxt.measureText(str)
                    paintTxt.color =
                        ContextCompat.getColor(context, healthCol[getHealthStatus(list[i])])
                    canvas.drawText(
                        str,
                        width / 2 - descWidth / 2,
                        paintTxt.ascent() * -1f,
                        paintTxt
                    )

                    val rectf =
                        RectF(startWidth, startHeight, startWidth + avgWidth, height.toFloat())
                    canvas.drawRect(rectf, paintBg)
                }
            }
            startWidth += avgWidth
        }

    }

    private fun getHealthStatus(bean: ReportBean.ChildStatus): Int {
        return if (isOrgan) {
            if (bean.idHealth >= 4) {
                0
            } else {
                bean.idHealth + 1
            }
        } else {
            if (bean.result >= 4) {
                0
            } else {
                bean.result + 1
            }
        }
    }

    fun setData(list: MutableList<ReportBean.ChildStatus>, format: Int) {
        this.list = list
        this.format = format
        invalidate()
    }

    fun setData(list: MutableList<ReportBean.ChildStatus>, format: Int, isOrgan: Boolean) {
        this.isOrgan = isOrgan
        setData(list, format)
    }

    override fun dispatchTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {

            MotionEvent.ACTION_DOWN -> {
                downX = event.x
                parent.requestDisallowInterceptTouchEvent(true)
                invalidate()

            }

            MotionEvent.ACTION_MOVE -> {
                downX = event.x
                parent.requestDisallowInterceptTouchEvent(true)
                invalidate()

            }

            MotionEvent.ACTION_UP -> {
                downX = -1f
                invalidate()
                parent.requestDisallowInterceptTouchEvent(false)
            }

        }
        return true
    }
}