package com.kdanmobile.reader.screen.signature

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Paint.Cap
import android.graphics.Path
import android.support.v4.content.ContextCompat
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View


class SignatureDrawView : View {

    private lateinit var paint: Paint
    private var currentPath: Path? = null
    private val datas = ArrayList<SignaturePathData>()
    var onSignatureDrawListener: OnSignatureDrawListener? = null

    var paintColor: Int = android.R.color.black
    var strokeWidth: Float = 10f

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

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

    constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {
        initView()
    }

    private fun initView() {
        paint = Paint()
        paint.isAntiAlias = true
        paint.style = Paint.Style.STROKE
        paint.strokeCap = Cap.ROUND
        paint.color = ContextCompat.getColor(context, paintColor)
        paint.strokeWidth = strokeWidth
    }

    fun clear() {
        datas.clear()
        invalidate()
        onSignatureDrawListener?.onClearSignature()
    }

    fun getBitmap(): Bitmap {
        val bitmap = Bitmap.createBitmap(measuredWidth, measuredHeight, Bitmap.Config.ARGB_8888)
        draw(Canvas(bitmap))
        return bitmap
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        for (data in datas) {
            paint.color = data.color
            paint.strokeWidth = data.strokeWidth
            canvas?.drawPath(data.path, paint)
        }
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        if (null != event) {
            val actionIndex = event.actionIndex
            val pointerId = event.getPointerId(actionIndex)
            if (pointerId == 0) {
                val x = event.getX(actionIndex)
                val y = event.getY(actionIndex)
                when (event.action) {
                    MotionEvent.ACTION_DOWN -> {
                        currentPath = Path()
                        datas.add(SignaturePathData(currentPath!!, paintColor, strokeWidth))
                        currentPath?.moveTo(x, y)
                        onSignatureDrawListener?.onStartSignature()
                    }
                    MotionEvent.ACTION_MOVE -> {
                        currentPath?.lineTo(x, y)
                    }
                    MotionEvent.ACTION_UP -> {
                    }
                }
                invalidate()
            }
        }

        return true
    }

    interface OnSignatureDrawListener {
        fun onClearSignature()

        fun onStartSignature()
    }

    data class SignaturePathData(val path: Path, val color: Int, val strokeWidth: Float)
}