package com.gh.gamecenter.common.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import com.gh.gamecenter.common.R
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.view.TrapezoidProgressBar.Companion.MAX_RATIO
import com.gh.gamecenter.common.view.TrapezoidProgressBar.Companion.MIN_RATIO
import kotlin.math.cos
import kotlin.math.sin
import kotlin.math.tan

class PKBackgroundView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
    private val leftPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val rightPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val leftShadowPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val rightShadowPaint = Paint(Paint.ANTI_ALIAS_FLAG)

    private var partMinWidth = 0F
    private var partMaxWidth = 0F

    private var viewWidth = 0F
    private var viewHeight = 0F
    private var partHeight = 0F

    private val cornerRadius = 6F.dip2px(context).toFloat()
    private val angle = Math.toRadians(60.0).toFloat()
    private val shadowOffset = 4F.dip2px(context).toFloat()
    private val gap = 5F.dip2px(context).toFloat() // 左右两部分中间的间距
    private var offset = 0F // 长短边差值

    private var leftColor = R.color.text_theme.toColor(context)
    private var rightColor = R.color.number_1.toColor(context)
    private var leftShadowColor = R.color.pk_bg_left_shadow.toColor(context)
    private var rightShadowColor = R.color.pk_bg_right_shadow.toColor(context)

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
        viewHeight = height.toFloat()
        viewWidth = width.toFloat()
        partHeight = viewHeight - shadowOffset
        partMinWidth = viewWidth * MIN_RATIO
        partMaxWidth = viewWidth * MAX_RATIO
        offset = partHeight / tan(angle)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        leftPaint.color = leftColor
        rightPaint.color = rightColor
        leftShadowPaint.color = leftShadowColor
        rightShadowPaint.color = rightShadowColor
        leftPaint.style = Paint.Style.FILL
        rightPaint.style = Paint.Style.FILL
        leftShadowPaint.style = Paint.Style.FILL
        rightShadowPaint.style = Paint.Style.FILL
        drawParts(canvas)
    }

    private fun drawParts(canvas: Canvas) {
        val leftEndX = partMinWidth + (partMaxWidth - partMinWidth) * 0.5F
        val leftShadowEndX = leftEndX + 1F.dip2px(context)
        val rightStartX = leftEndX - offset + gap
        val leftPartShadowPath = getLeftPartPath(leftShadowEndX, shadowOffset)
        val leftPartPath = getLeftPartPath(leftEndX, 0F)
        val rightPartShadowPath = getRightPartPath(rightStartX, shadowOffset)
        val rightPartPath = getRightPartPath(rightStartX, 0F)

        canvas.drawPath(leftPartShadowPath, leftShadowPaint)
        canvas.drawPath(leftPartPath, leftPaint)
        canvas.drawPath(rightPartShadowPath, rightShadowPaint)
        canvas.drawPath(rightPartPath, rightPaint)
    }

    private fun getLeftPartPath(endX: Float, translateY: Float): Path = Path().apply {
        val bottomRightX = endX - partHeight / tan(angle)
        val cosValue = cornerRadius * cos(angle)
        val sinValue = cornerRadius * sin(angle)
        val leftRadius = partHeight / 2F

        moveTo(endX - cornerRadius, translateY) // 右上角

        quadTo(
            endX,
            translateY,
            endX - cosValue,
            translateY + sinValue
        ) // 右上圆角

        lineTo(
            bottomRightX + cosValue,
            translateY + partHeight - sinValue
        ) // 右下角

        quadTo(
            bottomRightX,
            translateY + partHeight,
            bottomRightX - cosValue - cornerRadius,
            translateY + partHeight
        ) // 右下圆角

        lineTo(leftRadius, translateY + partHeight) // 左下角

        arcTo(
            RectF(
                0F,
                translateY,
                2 * leftRadius,
                translateY + 2 * leftRadius
            ),
            90F,
            180F,
            false
        ) // 左半圆

        close()
    }

    private fun getRightPartPath(startX: Float, translateY: Float): Path = Path().apply {
        val topLeftX = startX + partHeight / tan(angle)
        val cosValue = cornerRadius * cos(angle)
        val sinValue = cornerRadius * sin(angle)
        val rightRadius = partHeight / 2F

        moveTo(startX + cornerRadius, translateY + partHeight) // 左下角

        quadTo(
            startX,
            translateY + partHeight,
            cosValue + startX,
            translateY + partHeight - sinValue
        ) // 左下圆角

        lineTo(
            topLeftX - cosValue,
            translateY + sinValue
        ) // 左上角

        quadTo(
            topLeftX,
            translateY,
            topLeftX + cosValue + cornerRadius,
            translateY
        ) // 左上圆角

        lineTo(viewWidth - rightRadius, translateY) // 右上角

        arcTo(
            RectF(
                viewWidth - 2 * rightRadius,
                translateY,
                viewWidth,
                translateY + partHeight
            ),
            270F,
            180F,
            false
        ) // 右半圆

        close()
    }
}