package com.kdanmobile.reader

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.drawable.StateListDrawable
import android.os.Build
import android.support.annotation.LayoutRes
import android.support.annotation.RequiresApi
import android.support.constraint.ConstraintLayout
import android.support.constraint.ConstraintSet
import android.support.v4.content.ContextCompat
import android.support.v4.graphics.drawable.DrawableCompat
import android.support.v7.widget.AppCompatImageButton
import android.support.v7.widget.AppCompatImageView
import android.transition.AutoTransition
import android.transition.TransitionManager
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView

object Utils {
    /**
     * @param:
     * @author: luozhipeng
     * @Decreption:通过tint实现图片selector
     * @create at: 8/8/16 16:17
     **/
    fun setTintDrawableList(context: Context, view: View, srcResourceId: Int, colorResId: Int, colorPressedResId: Int) {
        var drawable = ContextCompat.getDrawable(context, srcResourceId)
        val colors = intArrayOf(ContextCompat.getColor(context, colorPressedResId), ContextCompat.getColor(context, colorPressedResId), ContextCompat.getColor(context, colorResId))
        val states = arrayOfNulls<IntArray>(3)
        states[0] = intArrayOf(android.R.attr.state_pressed)
        states[1] = intArrayOf(android.R.attr.state_selected)
        states[2] = intArrayOf()
        val colorList = ColorStateList(states, colors)
        val stateListDrawable = StateListDrawable()
        stateListDrawable.addState(states[0], drawable)//注意顺序
        stateListDrawable.addState(states[1], drawable)
        stateListDrawable.addState(states[2], drawable)
        val state = stateListDrawable.constantState
        drawable = DrawableCompat.wrap(if (state == null) stateListDrawable else state.newDrawable()).mutate()
        DrawableCompat.setTintList(drawable!!, colorList)
        drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)

        if (view is AppCompatImageView) {
            view.setImageDrawable(drawable)
        }

        if (view is ImageView) {
            view.setImageDrawable(drawable)
        }

        if (view is AppCompatImageButton) {
            view.setImageDrawable(drawable)
        }
    }

    @RequiresApi(Build.VERSION_CODES.KITKAT)
    fun ConstraintLayout.applyConstraintSet(constraintSet: ConstraintSet, duration: Long = 300) {
        val transition = AutoTransition()
        transition.duration = duration
        TransitionManager.beginDelayedTransition(this, transition)
        constraintSet.applyTo(this)
    }

    @RequiresApi(Build.VERSION_CODES.KITKAT)
    fun ConstraintLayout.applyConstraintSet(context: Context, @LayoutRes layoutResId: Int, duration: Long = 300) {
        val constraintSet = ConstraintSet()
        constraintSet.clone(context, layoutResId)
        this.applyConstraintSet(constraintSet, duration)
    }

    fun makeDropDownMeasureSpec(measureSpec: Int): Int {
        val mode = if (measureSpec == ViewGroup.LayoutParams.WRAP_CONTENT) {
            View.MeasureSpec.UNSPECIFIED
        } else {
            View.MeasureSpec.EXACTLY
        }
        return View.MeasureSpec.makeMeasureSpec(View.MeasureSpec.getSize(measureSpec), mode)
    }
}