package com.smartcar.animationmemorytest;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Created by thinkloki on 16/5/30.
 */
public class IconAnimationLayout extends RelativeLayout{

    private Context mContext;

    private LayoutInflater mInflater;

    private ImageView ivAnimationSmall;
    private ImageView ivAnimationBig;
    private AnimatorSet mAnimatorSet;
    private AtomicBoolean isStatrAnimation=new AtomicBoolean(false);

    public IconAnimationLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.mContext=context;
        init();
    }

    public IconAnimationLayout(Context context) {
        super(context);
        this.mContext=context;
        init();
    }


    public IconAnimationLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext=context;
        init();
    }

    public IconAnimationLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext=context;
        init();
    }

    public void startSearchAnimation(){
        ivAnimationBig.setAlpha(1.0f);
        ivAnimationBig.setScaleX(0.66f);
        ivAnimationBig.setScaleY(0.66f);

        ivAnimationSmall.setAlpha(1.0f);
        ivAnimationSmall.setScaleX(0.82f);
        ivAnimationSmall.setScaleY(0.82f);
        ivAnimationBig.setVisibility(VISIBLE);
        ivAnimationSmall.setVisibility(VISIBLE);
        if (isStatrAnimation.get() && mAnimatorSet != null && mAnimatorSet.isRunning()) {
            mAnimatorSet.cancel();
        }
        isStatrAnimation.set(true);
        getAnimatorSet().start();

    }

    public AtomicBoolean getIsStatrAnimation() {
        return isStatrAnimation;
    }

    public void cancelSearchAnimation(){
        isStatrAnimation.set(false);
        getAnimatorSet().cancel();
        ivAnimationSmall.setVisibility(GONE);
        ivAnimationBig.setVisibility(GONE);
    }


    private void init() {
        mInflater = LayoutInflater.from(mContext);
        View view = mInflater.inflate(R.layout.layout_icon_open_ainmation, null);
        ivAnimationSmall= (ImageView) view.findViewById(R.id.iv_animation_small);
        ivAnimationBig= (ImageView) view.findViewById(R.id.iv_animation_big);
        ivAnimationSmall.setVisibility(GONE);
        ivAnimationBig.setVisibility(GONE);
        addView(view);
    }

    public void setBackgroundDrawable(Drawable smallDrawable,Drawable bigDrawable){
        if(ivAnimationSmall!=null){
            ivAnimationSmall.setBackground(smallDrawable);
        }
        if(ivAnimationBig!=null){
            ivAnimationBig.setBackground(bigDrawable);
        }
    }

    private AnimatorSet getAnimatorSet(){

        if(mAnimatorSet==null){
            mAnimatorSet=new AnimatorSet();
            ObjectAnimator ivBigScaleX=ObjectAnimator.ofFloat(ivAnimationBig,View.SCALE_X,0.66f,1.0f);
            ivBigScaleX.setDuration(2000);

            ObjectAnimator  ivBigScaleY=ObjectAnimator.ofFloat(ivAnimationBig,View.SCALE_Y,0.66f,1.0f);
            ivBigScaleY.setDuration(2000);


            ObjectAnimator  ivBigAlpha=ObjectAnimator.ofFloat(ivAnimationBig,View.ALPHA,1.0f,0f);
            ivBigAlpha.setDuration(1000);
            ivBigAlpha.setStartDelay(1000);

            ObjectAnimator  ivSmallScaleX=ObjectAnimator.ofFloat(ivAnimationSmall,View.SCALE_X,0.82f,1.0f);
            ivSmallScaleX.setDuration(1500);
            ivSmallScaleX.setStartDelay(330);

            ObjectAnimator  ivSmallScaleY=ObjectAnimator.ofFloat(ivAnimationSmall,View.SCALE_Y,0.82f,1.0f);
            ivSmallScaleY.setDuration(1500);
            ivSmallScaleY.setStartDelay(330);


            ObjectAnimator  ivSmallAlpha=ObjectAnimator.ofFloat(ivAnimationSmall,View.ALPHA,1.0f,0f);
            ivSmallAlpha.setDuration(1000);
            ivSmallAlpha.setStartDelay(1300);

            mAnimatorSet.playTogether(ivBigScaleX,ivBigScaleY,ivBigAlpha,ivSmallScaleX,ivSmallScaleY,ivSmallAlpha);
            mAnimatorSet.setInterpolator(new SCPahtInterpolator(0.5f, 0f, 0.5f, 1f));

            mAnimatorSet.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {
                    ivAnimationBig.setAlpha(1.0f);
                    ivAnimationBig.setScaleX(0.66f);
                    ivAnimationBig.setScaleY(0.66f);

                    ivAnimationSmall.setAlpha(1.0f);
                    ivAnimationSmall.setScaleX(0.82f);
                    ivAnimationSmall.setScaleY(0.82f);
                    if(ivAnimationBig.getVisibility()!=View.VISIBLE&&isStatrAnimation.get()){
                        ivAnimationBig.setVisibility(View.VISIBLE);
                    }
                    if(ivAnimationSmall.getVisibility()!=View.VISIBLE&&isStatrAnimation.get()){
                        ivAnimationSmall.setVisibility(View.VISIBLE);
                    }
                }

                @Override
                public void onAnimationEnd(Animator animation) {
//                    ivAnimationBig.setVisibility(VISIBLE);
//                    ivAnimationSmall.setVisibility(VISIBLE);
                    if (isStatrAnimation.get()) {
                        mAnimatorSet.start();
                    }

                }

                @Override
                public void onAnimationCancel(Animator animation) {
                    if(ivAnimationBig.getVisibility()==View.VISIBLE){
                        ivAnimationBig.setVisibility(View.GONE);
                    }
                    if(ivAnimationSmall.getVisibility()==View.VISIBLE){
                        ivAnimationSmall.setVisibility(View.GONE);
                    }
                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });

        }
        return mAnimatorSet;
    }

}
