package com.khmer9.lotto.extension

import android.graphics.drawable.AnimationDrawable
import android.os.Handler
import android.view.View
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import android.view.animation.LinearInterpolator
import android.view.animation.OvershootInterpolator
import com.khmer9.lotto.util.SafeClickListener
import io.reactivex.Completable
import java.util.*

fun View.showIf(show: Boolean) {
    if (show) {
        show()
    } else {
        hide()
    }
}

fun View.show() {
    this.visibility = View.VISIBLE
}

fun View.hide() {
    this.visibility = View.INVISIBLE
}

fun View.slideIn(offset: Float): Completable {
    return Completable.create {
        visibility = View.VISIBLE
        alpha = 0f
        scaleX = 0f
        scaleY = 0f
        translationY = offset
        animate().alpha(1f)
            .translationY(0f)
            .scaleX(1f)
            .scaleY(1f)
            .setDuration(200)
            .setInterpolator(OvershootInterpolator())
            .withEndAction(it::onComplete)
    }
}

fun View.slideOut(offset: Float): Completable {
    return Completable.create {
        animate().alpha(0f)
            .scaleX(0f)
            .scaleY(0f)
            .translationY(offset)
            .setDuration(200)
            .withEndAction {
                visibility = View.INVISIBLE
                it.onComplete()
            }
    }
}

fun View.fadeOut(duration: Long = 30): Completable {
    return Completable.create {
        animate().setDuration(duration)
            .alpha(0f)
            .withEndAction {
                visibility = View.GONE
                it.onComplete()
            }
    }
}

fun View.fadeIn(): Completable {
    return Completable.create {
        //visibility = View.VISIBLE
        alpha = 0f
        animate().alpha(1f)
            .setDuration(200)
            .withEndAction(it::onComplete)
    }
}

fun View.rotate(degree: Float): Completable {
    return Completable.create {
        rotation=degree
        animate().rotation(0f)
            .setDuration(200)
            .withEndAction(it::onComplete)
    }
}

fun View.blink(degree: Float): Completable {
    return Completable.create {
        rotation=degree
        animate().rotation(0f)
            .setDuration(200)
            .withEndAction(it::onComplete)
    }
}
fun View.blink(
    times: Int = Animation.INFINITE,
    duration: Long = 150L,
    offset: Long = 20L,
    minAlpha: Float = 0.0f,
    maxAlpha: Float = 1.0f,
    repeatMode: Int = Animation.REVERSE
) {
    startAnimation(AlphaAnimation(minAlpha, maxAlpha).also {
        it.duration = duration
        it.startOffset = offset
        it.repeatMode = repeatMode
        it.repeatCount = times
    })


}


fun View.zoomIn(): Completable {
    return Completable.create {
        //alpha = 0f
        //pivotX = 0.5f
       // pivotY = 0.5f
        scaleX = 4f
        scaleY = 4f
        //translationY = 0f
        //translationX = 0f
        animate().alpha(1f)
            //.translationY(0f)
            //.translationX(0f)
            .scaleX(1f)
            .scaleY(1f)
            .setDuration(200)
            .setInterpolator(LinearInterpolator())
            .withEndAction(it::onComplete)
    }
}
fun View.waitingLotto(): Completable {
    return Completable.create {
//        alpha = 0f
//        scaleX = 0.5f
//        scaleY = 0f
//        translationY = 0f
//        translationX = 20f
//        animate().alpha(1f)
//            .translationY(0f)
//            .translationX(0f)
//            .scaleX(1f)
//            .scaleY(1f)
//            .setDuration(200)
//            .setInterpolator(OvershootInterpolator())
//            .withEndAction(it::onComplete)
        alpha = 0f
        animate().alpha(1f)
            .setDuration(50)
            .withEndAction(it::onComplete)
    }
}
fun View.btnClick(): Completable {
    return Completable.create {
        //visibility = View.VISIBLE
        alpha = 0f
        animate().alpha(1f)
            .setDuration(30)
            .withEndAction(it::onComplete)
    }
}
fun View.resultBlink(): Completable {
    return Completable.create {
        //visibility = View.VISIBLE
        alpha = 0f
        animate().alpha(1f)
            .setDuration(200)
            .withEndAction(it::onComplete)
    }
}
fun View.randomResult(): Completable {
    return Completable.create {
        //visibility = View.VISIBLE
        alpha = 0f
        animate().alpha(1f)
//            .also {
//                val rdns = (1..80).random()
//                tvNumber.text= "$rdns"
//            }
            .setDuration(5)
            .withEndAction(it::onComplete)
    }
}
fun AnimationDrawable.onAnimationFinished(block: () -> Unit) {
    var duration: Long = 0
    for (i in 0..numberOfFrames) {
        duration += getDuration(i)
    }
    Handler().postDelayed({
        block()
    }, duration*5)
}
fun IntRange.random() = Random().nextInt((endInclusive + 1) - start) + start

fun View.setSafeOnClickListener(onSafeClick: (View) -> Unit) {
    val safeClickListener = SafeClickListener {
        onSafeClick(it)
    }
    setOnClickListener(safeClickListener)
}

fun <T> MutableList<T>.prepend(element: T) {
    add(0, element)
}
