package com.duben.microtribe.ui.adapter

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.view.animation.LinearInterpolator
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.viewpager.widget.PagerAdapter
import com.daimajia.androidanimations.library.Techniques
import com.daimajia.androidanimations.library.YoYo
import com.duben.microtribe.R
import com.duben.microtribe.common.AppConfig
import com.duben.microtribe.ui.adapter.base.OnItemClickListener


class VPAdapter() : PagerAdapter() {

    private val MAX_COUNT = 10000 // 最大数量

    private lateinit var layoutList: IntArray
    private lateinit var context: Context
    private var rl1: RelativeLayout? = null
    private var rl2: RelativeLayout? = null
    private var rl3: RelativeLayout? = null

    private var mOnItemClickListener: OnItemClickListener? = null

    constructor(context: Context, layoutList: IntArray) : this() {
        this.context = context
        this.layoutList = layoutList
    }

    override fun getCount(): Int {
        return MAX_COUNT
    }

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view == `object`
    }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        // 计算实际位置
        val realPosition: Int = position % layoutList.size
        val view = View.inflate(context, this.layoutList[realPosition], null)
        // 添加视图
        setContentView(view, realPosition)
        if (view.parent != null) {
            container.removeView(view)
        }
        container.addView(view)
        return view
    }

    private fun setContentView(view: View, realPosition: Int) {
        when (realPosition) {
            0 -> {
                rl1 = view.findViewById<RelativeLayout>(R.id.rl_guide1)
                val tvGuide1Top = view.findViewById<TextView>(R.id.tv_guide1_top)
                val tvGuide1Bottom = view.findViewById<TextView>(R.id.tv_guide1_bottom)
                val llGuide1Next = view.findViewById<LinearLayout>(R.id.ll_guide1_next)
                val ivGuideBg = view.findViewById<ImageView>(R.id.iv_guide1_bg)
                val ivGuideBg2 = view.findViewById<ImageView>(R.id.iv_guide2_bg)
                llGuide1Next.setOnClickListener {
                    mOnItemClickListener?.onItemClick(realPosition)
                }
                llGuide1Next?.postDelayed({
                    YoYo.with(Techniques.Pulse).duration(1000).repeat(-1)
                        .playOn(llGuide1Next)
                    val rotationanimator: ObjectAnimator = ObjectAnimator.ofFloat(
                        ivGuideBg,
                        "rotation", 0f, 360f
                    )
                    rotationanimator.duration = 1000
                    rotationanimator.repeatCount = ValueAnimator.INFINITE
                    rotationanimator.setInterpolator(LinearInterpolator())
                    rotationanimator.start()

                    val rotationanimator2: ObjectAnimator = ObjectAnimator.ofFloat(
                        ivGuideBg2,
                        "rotation", 0f, 360f
                    )
                    rotationanimator2.duration = 900
                    rotationanimator2.repeatCount = ValueAnimator.INFINITE
                    rotationanimator2.setInterpolator(LinearInterpolator())
                    rotationanimator2.start()
                }, 200)
            }
            1 -> {
                rl2 = view.findViewById<RelativeLayout>(R.id.rl_guide2)
                val llGuide2Next = view.findViewById<LinearLayout>(R.id.ll_guide2_next)
                llGuide2Next.setOnClickListener {
                    mOnItemClickListener?.onItemClick(realPosition)
                }
                llGuide2Next?.postDelayed({
                    YoYo.with(Techniques.Pulse).duration(1000).repeat(-1)
                        .playOn(llGuide2Next)
                }, 200)

            }
            2 -> {
                rl3 = view.findViewById<RelativeLayout>(R.id.rl_guide3)
                val llGuide3Next = view.findViewById<LinearLayout>(R.id.ll_guide3_next)
                llGuide3Next.setOnClickListener {
//                    readyGo(WxCleanActivity::class.java)
                    mOnItemClickListener?.onItemClick(realPosition)
                }
                llGuide3Next?.postDelayed({
                    YoYo.with(Techniques.Pulse).duration(1000).repeat(-1)
                        .playOn(llGuide3Next)
                }, 200)
            }
        }

    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        container.removeView(`object` as View)
    }

    interface OnItemClickListener {
        fun onItemClick(position: Int)
    }

    fun setOnItemClickListener(listener: OnItemClickListener) {
        mOnItemClickListener = listener
    }

}
