package com.xty.common.weight

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.View
import androidx.core.content.ContextCompat
import com.xty.common.R

/**
 * 心电
 */
class HeartElePicture(context: Context?, attrs: AttributeSet?): View(context, attrs) {
    constructor(context: Context?):this(context,null)


    var provideLength=0f
    var isAddData=false
    var mindis=0

    val paintHeart by lazy{
        Paint().apply {
            this.isDither=true
            this.isAntiAlias=true
            this.style=Paint.Style.STROKE
            this.strokeWidth=3f
            this.color=ContextCompat.getColor(context!!, R.color.col_e75)
        }
    }
    val pathHeart by lazy { Path()    }


    val paintH by lazy {
        Paint().apply {
            this.isDither=true
            this.isAntiAlias=true
            this.style=Paint.Style.FILL_AND_STROKE
            this.strokeWidth=6f
            this.color=ContextCompat.getColor(context!!, R.color.col_e7e)

        }
    }
    val paintV by lazy {
        Paint().apply {
            this.isDither=true
            this.isAntiAlias=true
            this.color=ContextCompat.getColor(context!!, R.color.col_e7e)
            this.style=Paint.Style.FILL_AND_STROKE
            this.strokeWidth=6f
        }
    }
    val paintHs by lazy {
        Paint().apply {
            this.isDither=true
            this.isAntiAlias=true
            this.style=Paint.Style.FILL_AND_STROKE
            this.strokeWidth=1f
            this.color=ContextCompat.getColor(context!!, R.color.col_e7e)
        }
    }
    val paintVs by lazy {
        Paint().apply {
            this.isDither=true
            this.isAntiAlias=true
            this.color=ContextCompat.getColor(context!!, R.color.col_e7e)
            this.style=Paint.Style.FILL_AND_STROKE
            this.strokeWidth=1f
        }
    }


    var allLatt=20 //每行没列的格子数
    var everyLatt=5 //多少个格子为一组
    var bigLatt=allLatt/everyLatt  //共多少组

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        if(isAddData){

                canvas.drawPath(pathHeart,paintHeart)
        }else{
            var minLength=Math.min(width,height)

            mindis=  minLength/allLatt //分割为20  单个格子的尺寸



            var offsetY=height/2f-(mindis*(allLatt/2))  //y轴开始
            var offsetX=width/2f-(mindis*(allLatt/2))  //x轴开始

            var pathH=Path()
            var pathV=Path()

            var pathHs=Path()
            var pathVs=Path()
            for(i in 0..allLatt){  //这里不能去除16
                if(i%everyLatt==0){
                    //画横向
                    pathH.moveTo(offsetX,offsetY+(mindis*i))
                    pathH.lineTo(width-offsetX,offsetY+(mindis*i))
                    pathH.close()
                    canvas.drawPath(pathH,paintH)
                    //画x轴
                    pathV.moveTo(offsetX+(mindis*i),offsetY)
                    pathV.lineTo(offsetX+(mindis*i),height-offsetY)
                    pathV.close()
                    canvas.drawPath(pathV,paintV)
                }else{
                    //画横向
                    pathHs.moveTo(offsetX,offsetY+(mindis*i))
                    pathHs.lineTo(width-offsetX,offsetY+(mindis*i))
                    pathHs.close()
                    canvas.drawPath(pathHs,paintHs)
                    //画y轴
                    pathVs.moveTo(offsetX+(mindis*i),offsetY)
                    pathVs.lineTo(offsetX+(mindis*i),height-offsetY)
                    pathVs.close()
                    canvas.drawPath(pathVs,paintVs)
                }

            }
            pathHeart.moveTo(offsetX,height/2f)
            canvas.drawPath(pathHeart,paintHeart)
        }

    }

    var xx=0
    fun addSize(value:Long,hrv:Float){
       // var nowX=value*mindis/40f

        var offsetX=width/2f-(mindis*(allLatt/2))  //x轴开始
        var nowX=offsetX+(++xx)*mindis



        var y0=height/2f
        var y1= hrv/10f *mindis+height/2f
        if(y0==y1){
            pathHeart.lineTo(nowX,height/2f)
        }else if(y0>y1){
            pathHeart.lineTo(nowX,y0+(y0-y1))
        }else{
            pathHeart.lineTo(nowX,y0-(y1-y0))
        }


         invalidate()
    }



}