package com.cr.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.util.AttributeSet
import android.view.View
import com.cr.common.CrColorManager
import com.cr.common.CrFontManager
import com.cr.common.CrUnitManager
import com.cr.cruav.R

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/8/26 11:23
 * 描述：自定义指示器视图
 */
class CrIndicator @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAtr: Int = 0
) : View(context, attrs, defStyleAtr) {
    // todo: 2023/8/26 视图尺寸定义
    private var viewWidth: Int = 0  // define: 2023/8/26 视图宽度
    private var viewHeight: Int = 0  // define: 2023/8/26 视图高度

    // todo: 2023/8/26 定义画布
    private var canvas: Canvas? = null

    // todo: 2023/8/26 定义画笔
    private var paintNormalBackground: Paint? = null  // define: 2023/8/26 普通画笔
    private var paintActiveBackground: Paint? = null  // define: 2023/8/26 激活画笔
    private var paintNormalText: Paint? = null // define: 2023/8/26 普通文字画笔
    private var paintActiveText: Paint? = null // define: 2023/8/26 激活文字画笔

    // todo: 2023/8/26 变量定义
    private var mCount: Int = 5  // define: 2023/8/26 指示器数量
    private var mSpace: Int = 10 // define: 2023/8/26 指示器间隔
    private var mIndex: Int = 2  // define: 2023/8/26 档期激活位置

    /**
     * 初始化
     */
    init {
        // todo: 2023/8/26 获取属性
        val attributes = context.obtainStyledAttributes(attrs,R.styleable.CrIndicator)
        if(attributes != null){
            mCount = attributes.getInt(R.styleable.CrIndicator_cr_count,0)
            mIndex = attributes.getInt(R.styleable.CrIndicator_cr_index,1)
            // todo: 2023/8/26 使属性生效
            attributes.recycle()
        }
    }

    /**
     * 生命周期函数
     * 当XML加载完毕后会调用此方法
     */
    override fun onFinishInflate() {
        super.onFinishInflate()
        this.setBackgroundColor(Color.argb(0, 70, 71, 81))
    }

    /**
     * 重置
     * @param widthMeasureSpec Int
     * @param heightMeasureSpec Int
     */
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        // todo: 2023/8/26 获取视图宽度和高度 单位是像素
        this.viewWidth = MeasureSpec.getSize(widthMeasureSpec)
        this.viewHeight = MeasureSpec.getSize(heightMeasureSpec)
    }

    /**
     * 重绘
     * @param canvas Canvas 画布
     */
    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        this.canvas = canvas
        // todo: 2023/8/26 初始化画笔
        initPaint()
        // todo: 2023/8/26 绘制
        draw()
    }

    /**
     * 绘制
     */
    private fun draw() {
        if (mCount <= 0) return
        // todo: 2023/8/26 半径大小
        val mRadius = viewHeight / 2 - 4
        // todo: 2023/8/26 计算指示器绘制内容占据的宽度
        val mWidth = mRadius * 2 * mCount + mSpace * (mCount - 1)
        // todo: 2023/8/26 计算左侧位置
        var mLeft = (viewWidth - mWidth) / 2
        // todo: 2023/8/26 绘制圆
        for (i in 0 until mCount) {
            // todo: 2023/8/26 计算圆心位置
            val cx = mLeft + mRadius * (i * 2 + 1) + mSpace * i
            val cy = mRadius * 1f + 4
            // todo: 2023/8/26 计算文字位置
            val title = "${i + 1}"
            var titleRect = Rect()
            paintNormalText?.getTextBounds(title, 0, title!!.length, titleRect)
            val dx = cx - titleRect.width() / 2
            val dy = cy + titleRect.height() / 2
            if (i != mIndex) {
                // todo: 2023/8/26 绘制普通
                canvas?.drawCircle(cx * 1f, cy, mRadius * 1f, paintNormalBackground!!)
                canvas?.drawText(title, dx * 1f, dy * 1f, paintNormalText!!)
            } else {
                // todo: 2023/8/26 绘制当前激活
                canvas?.drawCircle(cx * 1f, cy, mRadius * 1f, paintActiveBackground!!)
                canvas?.drawText(title, dx * 1f, dy * 1f, paintActiveText!!)
            }
        }
    }

    /**
     * 画笔初始化
     */
    private fun initPaint() {
        // todo: 2023/8/26 普通背景画笔
        paintNormalBackground = Paint(Paint.ANTI_ALIAS_FLAG)
        paintNormalBackground?.let {
            it.isAntiAlias = true  // todo: 2023/7/31 抗锯齿
            it.color = Color.argb(255, 36, 110, 140)
            it.alpha = 255
            it.strokeJoin = Paint.Join.ROUND
            it.strokeCap = Paint.Cap.BUTT
            it.style = Paint.Style.FILL  // define: 2023/7/31 画笔类型
        }

        // todo: 2023/8/26 选中背景画笔
        paintActiveBackground = Paint(Paint.ANTI_ALIAS_FLAG)
        paintActiveBackground?.let {
            it.isAntiAlias = true  // todo: 2023/7/31 抗锯齿
            it.color = Color.argb(255, 188, 14, 28)
            it.alpha = 255
            it.strokeJoin = Paint.Join.ROUND
            it.strokeCap = Paint.Cap.BUTT
            it.style = Paint.Style.FILL  // define: 2023/7/31 画笔类型
        }

        // todo: 2023/8/26 普通文字画笔
        paintNormalText = Paint()
        paintNormalText?.let {
            it.isAntiAlias = true  // todo: 2023/7/31 抗锯齿
            it.textAlign = Paint.Align.LEFT  // define: 2023/7/31 文字对齐方式
            it.color = CrColorManager.getColor(context, R.color.white)  // define: 2023/7/31 文字颜色
            it.textSize =
                CrUnitManager.dp2px(CrUnitManager.getDimens(context, R.dimen.cr_4_dp)).toFloat()
            it.isFakeBoldText = true  // define: 2023/7/31 加粗字体
            it.typeface = CrFontManager.getTTGBTypeface(context)
        }

        // todo: 2023/8/26 选中文字画笔
        paintActiveText = Paint()
        paintActiveText?.let {
            it.isAntiAlias = true  // todo: 2023/7/31 抗锯齿
            it.textAlign = Paint.Align.LEFT  // define: 2023/7/31 文字对齐方式
            it.color = CrColorManager.getColor(context, R.color.yellow)  // define: 2023/7/31 文字颜色
            it.textSize =
                CrUnitManager.dp2px(CrUnitManager.getDimens(context, R.dimen.cr_4_dp)).toFloat()
            it.isFakeBoldText = true  // define: 2023/7/31 加粗字体
            it.typeface = CrFontManager.getTTGBTypeface(context)
        }
    }

    /**
     * 设置指示器数量
     * @param count Int
     */
    fun crSetCount(count: Int) {
        if (count > 0) {
            mCount = count
            invalidate()
        }
    }

    /**
     * 设置当前激活
     * @param index Int
     */
    fun crSetIndex(index:Int){
        if(index > mCount) return
        mIndex = index
        invalidate()
    }
}