package com.adealink.weparty.commonui.widget.roulette.panel

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Point
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View

class RoulettePanelView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0,
) : View(context, attrs, defStyleAttr), IRoulettePanel {

    companion object {
        const val TAG = "RoulettePanelView"
        val DEFAULT_COLOR_LIST = listOf(Color.WHITE, Color.parseColor("#FFFFE2F4"), Color.parseColor("#FFFFF2F3"))
    }

    private var playerCount = 0
    private var playerColorList = DEFAULT_COLOR_LIST
    private var paint: Paint = Paint()
    var center = Point(0, 0)
    val eachPartitionDegree: Float
        get() {
            if (playerCount <= 0) {
                return 360f
            }
            return 360f / playerCount
        }
    private val rectF: RectF = RectF()
    private var inGameColorList = mutableListOf<Int>()

    init {
        initPaint()
    }

    override fun startGame(playerCount: Int, playerColorList: List<Int>?) {
        this.playerCount = playerCount
        inGameColorList.clear()
        if (playerColorList.isNullOrEmpty()) {
            this.playerColorList = DEFAULT_COLOR_LIST
        } else {
            this.playerColorList = playerColorList
        }
        invalidate()
    }

    override fun addPlayer() {
        playerCount ++
        invalidate()
    }

    override fun weepOutPlayer(playerIndex: Int) {
        if (playerCount == 0) {
            return
        }
        playerCount --
        inGameColorList.removeAt(playerIndex)
        invalidate()
    }

    private fun initPaint() {
        paint.color = playerColorList[0]
        paint.style = Paint.Style.FILL
        paint.isAntiAlias = true
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        center.x = w / 2
        center.y = h / 2
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.translate(center.x.toFloat(), center.y.toFloat())
        canvas.rotate(-90f)
        val radius = (width / 2).toFloat()
        rectF.set(-radius + paddingLeft,
            -radius + paddingTop,
            radius - paddingRight,
            radius - paddingBottom)
        if (playerCount == 0) {
            drawArc(canvas, 0, eachPartitionDegree)
            return
        }
        var startAngle = 0f
        for (i in 0 until playerCount) {
            drawArc(canvas, i, startAngle)
            startAngle += eachPartitionDegree
        }
    }

    private fun drawArc(canvas: Canvas?, index: Int, startAngle: Float) {
        val arcColor: Int?
        if (index < inGameColorList.size) {
            arcColor = inGameColorList[index]
        } else {
            arcColor = playerColorList[index.rem(playerColorList.size)]
            inGameColorList.add(arcColor)
        }
        paint.color = arcColor
        canvas?.drawArc(rectF, startAngle, eachPartitionDegree, true, paint)
    }

}