package com.abbidot.tracker.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import androidx.core.content.ContextCompat
import com.abbidot.baselibrary.util.LogUtil
import com.abbidot.tracker.R
import com.abbidot.tracker.util.ViewUtil
import com.qmuiteam.qmui.util.QMUIDisplayHelper

/**
 *Created  by .yzq on 2022/1/7/007.
 * @link
 * @description: 自定义柱状图
 */
class MyChartView : View {

    private lateinit var mContext: Context
    private var mWidth = 0
    private var mHeight = 0

    //每个圆柱的宽度
    private var mBarWidth = 0f

    //圆柱之间的间距
    private var mBarSpace = 0f
    private var mRadius = 0f
    private var mData: MutableList<Int>? = null
    private lateinit var mPaint: Paint

    //是否显示Y轴分钟
    private var isShowMinY = false

    //配合动画,每次画百分几
    private var mAnimatedPercent = 0f

    //每个数值1所需要、占用的高度
    private var mValueHeight = 0f

    //动画时长（毫秒）
    private val mAnimatorDuration = 1000L

    constructor(context: Context) : super(context) {
        init(context)
    }


    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context, attrs, defStyleAttr
    ) {
        init(context)
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        init(context)
    }

    private fun init(context: Context) {
        //禁用硬件加速,防止返回到界面多次执行onDraw
        setLayerType(LAYER_TYPE_SOFTWARE, null)
        mContext = context
        mData = ArrayList()
        mBarWidth = QMUIDisplayHelper.dpToPx(8).toFloat()
        mRadius = QMUIDisplayHelper.dpToPx(4).toFloat()
        mPaint = Paint()
        mPaint.color = ContextCompat.getColor(mContext, R.color.btn_yellow_color)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        LogUtil.e("onMeasure")
        mWidth = measuredWidth
        mHeight = measuredHeight
    }

    override fun onDraw(canvas: Canvas) {
        LogUtil.d("MyChartView------>onDraw")
        super.onDraw(canvas)
        startDraw(canvas)
    }

    private fun startDraw(canvas: Canvas) {
        canvas.drawColor(ContextCompat.getColor(mContext, R.color.home_data_black_color1))
//        if (isShowMinY) {
//            mPaint!!.color = ContextCompat.getColor(mContext!!, R.color.white)
//            mPaint!!.textSize = QMUIDisplayHelper.sp2px(mContext, 11).toFloat()
//            val str = "60 min"
//            val fontMetrics = mPaint!!.fontMetrics
//            //获取文字的高度
//            val height = fontMetrics.bottom - fontMetrics.top
//            canvas.drawText(str, height / 4, height, mPaint!!)
//
//            mPaint!!.color = ContextCompat.getColor(mContext!!, R.color.btn_yellow_color)
//        }
        val size = mData!!.size
        if (size > 0) {
            //执行一次
            if (mValueHeight == 0f) {
                //从所有数据中找出最大值
//                var max = Collections.max(mData)
//                if (max == 0) max = 1
                //直接用60分钟作为最大值
                val max = 60
                //每个数值1所需要、占用的高度
                mValueHeight = (mHeight / max).toFloat()
                mBarSpace = ((mWidth - mBarWidth * size) / size)
            }
            for (i in 0 until size) {
                val value = mData!![i]
                if (value <= 0) continue
                val left: Float = (mBarSpace * i + i * mBarWidth)
                val top: Float = (mHeight - value * mValueHeight)
                val right: Float = (mBarSpace * i + mBarWidth * (i + 1))
                val bottom = mWidth.toFloat()
                canvas.drawRoundRect(
                    left, top / mAnimatedPercent, right, bottom, mRadius, mRadius, mPaint
                )
            }
        }
    }


    fun setData(data: MutableList<Int>, showMinY: Boolean = false) {
        mData = data
        isShowMinY = showMinY
        ViewUtil.instance.setValueAnimator(0f, 100f, mAnimatorDuration) {
            mAnimatedPercent = it.animatedValue as Float / 100
            postInvalidate()
        }
    }
}