package eu.kanade.tachiyomi.ui.base.controller

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.res.Configuration
import android.view.View
import android.view.ViewGroup
import com.bluelinelabs.conductor.ControllerChangeHandler
import com.bluelinelabs.conductor.changehandler.FadeChangeHandler

/**
 * A variation of [FadeChangeHandler] that only fades in.
 */
class OneWayFadeChangeHandler : FadeChangeHandler {
    constructor()
    constructor(removesFromViewOnPush: Boolean) : super(removesFromViewOnPush)
    constructor(duration: Long) : super(duration)
    constructor(duration: Long, removesFromViewOnPush: Boolean) : super(
        duration,
        removesFromViewOnPush
    )

    override fun getAnimator(
        container: ViewGroup,
        from: View?,
        to: View?,
        isPush: Boolean,
        toAddedToContainer: Boolean
    ): Animator {
        val animator = AnimatorSet()
        if (to != null) {
            val start: Float = if (toAddedToContainer) 0F else to.alpha
            animator.play(ObjectAnimator.ofFloat(to, View.ALPHA, start, 1f))
        }

        val hasSideNav = container.context.resources.configuration?.orientation == Configuration.ORIENTATION_LANDSCAPE
        if (from != null && (!isPush || removesFromViewOnPush())) {
            if (!hasSideNav) {
                animator.play(ObjectAnimator.ofFloat(from, View.ALPHA, 0f))
            } else {
                container.removeView(from)
            }
        }
        return animator
    }

    override fun copy(): ControllerChangeHandler {
        return OneWayFadeChangeHandler(animationDuration, removesFromViewOnPush())
    }
}
