package com.test.basd.onecleanmaster.view

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import com.test.basd.onecleanmaster.R

class ArrowView : View {
    private val arrowUpN: Bitmap = BitmapFactory.decodeResource(resources, R.drawable.upper_n)
    private val arrowUpS: Bitmap = BitmapFactory.decodeResource(resources, R.drawable.l_upper)

    private val arrowLoweN: Bitmap = BitmapFactory.decodeResource(resources, R.drawable.lowe_n)
    private val arrowLoweS: Bitmap = BitmapFactory.decodeResource(resources, R.drawable.l_lower)
    private val drawPaint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG)
    private var upMargin = 0f
    private var lowMargin = 0f

    private var isAscOrder: Boolean = true
    private var isOrder: Boolean = false

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

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        upMargin = measuredHeight / 4f
        lowMargin = measuredHeight / 2.3f
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        drawOrderArrow(canvas)
    }

    fun drawOrderArrow(canvas: Canvas) {
        if (!isOrder) {
            canvas.drawBitmap(arrowUpN, 0f, upMargin, drawPaint)
            canvas.drawBitmap(arrowLoweN, 0f, lowMargin, drawPaint)
        } else {
            if (isAscOrder) {
                canvas.drawBitmap(arrowUpS, 0f, upMargin, drawPaint)
                canvas.drawBitmap(arrowLoweN, 0f, lowMargin, drawPaint)
            } else {
                canvas.drawBitmap(arrowUpN, 0f, upMargin, drawPaint)
                canvas.drawBitmap(arrowLoweS, 0f, lowMargin, drawPaint)
            }
        }

    }

    fun setAscOrder(isOrder: Boolean, isAscOrder: Boolean = false) {
        this.isOrder = isOrder
        this.isAscOrder = isAscOrder
        invalidate()
    }

}