package com.adealink.weparty.commonui.flipper

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import android.widget.ViewFlipper
import com.adealink.weparty.R

/**
 */
class MarqueeFlipperView @JvmOverloads constructor(
    context: Context,
    attributes: AttributeSet? = null
) : ViewFlipper(context, attributes) {


    private var currentIndex: Int = 0
    private var adapter: Adapter<*>? = null
    var orientation: Int = LinearLayout.VERTICAL
        set(value) {
            field = value
//            if (field == LinearLayout.HORIZONTAL) {
//                setInAnimation(context, R.anim.flip_anim_in_x)
//                setOutAnimation(context, R.anim.flip_anim_out_x)
//            } else
            if (field == LinearLayout.VERTICAL) {
                setInAnimation(context, R.anim.flip_anim_in)
                setOutAnimation(context, R.anim.flip_anim_out)
            }
        }

    var onShowListener: ((view: View?, position: Int) -> Unit)? = null

    init {
        flipInterval = 3000
        orientation = LinearLayout.VERTICAL
    }

    override fun setDisplayedChild(whichChild: Int) {
        adapter?.let {
            bindView(whichChild, it)
        }
        super.setDisplayedChild(whichChild)
    }

    private fun <T : Any> bindView(whichChild: Int, adapter: Adapter<T>) {
        val realChild = if (whichChild >= 2) {
            0
        } else if (whichChild < 0) {
            1
        } else {
            whichChild
        }

        val index = currentIndex + whichChild - displayedChild
        currentIndex = if (index >= adapter.list.size) {
            0
        } else if (index < 0) {
            adapter.list.size - 1
        } else {
            index
        }
        if (realChild < childCount && currentIndex < adapter.list.size) {
            val showView = getChildAt(realChild)
            adapter.onBindViewHolder(showView, currentIndex, adapter.list[currentIndex])
            onShowListener?.invoke(showView, currentIndex)
        }
    }

    /**
     * 切换时保持词下标一样 第一次不展示动画
     */
    fun setDisplayedChild(index: Int, showAnim: Boolean) {
        if (!showAnim) {
            val tempIn = inAnimation
            val tempOut = outAnimation
            inAnimation = null
            outAnimation = null
            postDelayed({
                inAnimation = tempIn
                outAnimation = tempOut
            }, 380)
        }
        currentIndex = index
        displayedChild = displayedChild
    }

    fun <T : Any> setAdapter(adapter: Adapter<T>) {
        this.adapter = adapter
        if (childCount == 0) {
            addView(adapter.onCreateViewHolder(null))
            addView(adapter.onCreateViewHolder(null))
        }
    }

    abstract class Adapter<T : Any>(val list: List<T>) {
        abstract fun onCreateViewHolder(t: T?): View
        abstract fun onBindViewHolder(view: View, position: Int, t: T?)
    }

    override fun onDetachedFromWindow() {
        try {
            super.onDetachedFromWindow()
        } catch (e: IllegalArgumentException) {
            stopFlipping()
        }
    }
}