package it.integry.integrywmsnative.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Interpolator;
import android.view.animation.Transformation;

import androidx.viewpager.widget.ViewPager;

public class DeactivatableViewPager extends ViewPager implements Animation.AnimationListener {

    private View mCurrentView;
    private PagerAnimation mAnimation = new PagerAnimation();
    private boolean mAnimStarted = false;
    private long mAnimDuration = 100;


    public DeactivatableViewPager(Context context) {
        super(context);
        mAnimation.setAnimationListener(this);

        setLayerType(View.LAYER_TYPE_HARDWARE, null);
//        getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
    }

    public DeactivatableViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        mAnimation.setAnimationListener(this);

        setLayerType(View.LAYER_TYPE_HARDWARE, null);
//        getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return !isEnabled() || super.onTouchEvent(event);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return isEnabled() && super.onInterceptTouchEvent(event);
    }


    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        if (!mAnimStarted && mCurrentView != null) {
            mCurrentView.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            int height = mCurrentView.getMeasuredHeight();

            if (height < getMinimumHeight()) {
                height = getMinimumHeight();
            }

            int displayHeight = (int)(getResources().getDisplayMetrics().heightPixels*0.5);

            if(height > displayHeight) height = displayHeight;


            int newHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
            if (/*getLayoutParams().height != 0 && */heightMeasureSpec != height && height > 0 && height < 100000) {
//                animate()
//                        .y(height)
//                        .withLayer()
//                        .start();
//                mAnimation.setDimensions(height, getLayoutParams().height);
//                mAnimation.setDuration(mAnimDuration);
//                startAnimation(mAnimation);
//                mAnimStarted = true;
            } else {
            }
            heightMeasureSpec = newHeight;
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    /**
     * This method should be called when the ViewPager changes to another page. For best results
     * call this method in the adapter's setPrimary
     *
     * @param currentView PagerAdapter item view
     */
    public void onPageChanged(View currentView) {
        mCurrentView = currentView;
        requestLayout();
    }


    /**
     * Custom animation to animate the change of height in the
     */
    private class PagerAnimation extends Animation {
        private int targetHeight;
        private int currentHeight;
        private int heightChange;

        /**
         * Set the dimensions for the animation.
         *
         * @param targetHeight  View's target height
         * @param currentHeight View's current height
         */
        void setDimensions(int targetHeight, int currentHeight) {
            this.targetHeight = targetHeight;
            this.currentHeight = currentHeight;
            this.heightChange = targetHeight - currentHeight;
        }

        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            if (interpolatedTime >= 1) {
                getLayoutParams().height = targetHeight;
            } else {
                int stepHeight = (int) (heightChange * interpolatedTime);
                getLayoutParams().height = currentHeight + stepHeight;
            }
            requestLayout();
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    }

    /**
     * Sets the duration of the animation.
     *
     * @param duration Duration in ms
     */
    public void setAnimationDuration(long duration) {
        mAnimDuration = duration;
    }

    /**
     * Sets the interpolator used by the animation.
     *
     * @param interpolator {@link Interpolator}
     */
    public void setAnimationInterpolator(Interpolator interpolator) {
        mAnimation.setInterpolator(interpolator);
    }

    @Override
    public void onAnimationStart(Animation animation) {
        mAnimStarted = true;
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        mAnimStarted = false;
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
    }

}
