package com.zkjc.common.view;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.zkjc.common.utils.VibrateUtil;
import com.zkjc.utils.R;

public class CheckAnimView extends RelativeLayout {
    private Context mContext;
    private LinearLayout rl_one;
    private ProgressBar pb_one;
    private ProgressBar pb_two;
    private RelativeLayout rl_two;
    private Handler handler = new Handler();
    private final int BREATH_INTERVAL_TIME = 1000; //设置呼吸灯时间间隔
    private Boolean mVibrated = false;

    public CheckAnimView(Context context) {
        this(context, null);
    }

    public CheckAnimView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CheckAnimView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        inflate(context, R.layout.view_anim, this);
        rl_one = findViewById(R.id.rl_one);
        pb_one = findViewById(R.id.pb_one);
        pb_two = findViewById(R.id.pb_two);
        rl_two = findViewById(R.id.rl_two);
    }

    public void startAnim() {

        AlphaAnimation animationFadeIn = new AlphaAnimation(0.8f, 0.2f);
        animationFadeIn.setDuration(BREATH_INTERVAL_TIME);
        AlphaAnimation animationFadeOut = new AlphaAnimation(0.2f, 0.8f);
        animationFadeIn.setDuration(BREATH_INTERVAL_TIME);
        animationFadeIn.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationEnd(Animation arg0) {
                pb_one.startAnimation(animationFadeOut);
                pb_two.startAnimation(animationFadeOut);
            }

            @Override
            public void onAnimationRepeat(Animation arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationStart(Animation arg0) {
                // TODO Auto-generated method stub

            }

        });
        animationFadeOut.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationEnd(Animation arg0) {
                pb_one.startAnimation(animationFadeIn);
                pb_two.startAnimation(animationFadeIn);
            }

            @Override
            public void onAnimationRepeat(Animation arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationStart(Animation arg0) {
                // TODO Auto-generated method stub

            }
        });

        // rlTwo.startAnimation(animationFadeOut);
        pb_one.startAnimation(animationFadeOut);
        pb_two.startAnimation(animationFadeOut);
        handler.postDelayed(runnable, 1);
    }

    int progress = 0;
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            progress++;
            if (progress <= 100) {
                // rlTwo.setVisibility(View.GONE);
                pb_one.setProgress(progress);
                pb_two.setProgress(progress);
                if (!mVibrated) {
                    mVibrated = true;
                    VibrateUtil.vibrate(mContext);
                }

            } else {
                if (progress % 200 == 0) {
                    progress = 0;
                   // mVibrated = false;
                }
            }
            handler.postDelayed(this, 1);
        }
    };

    public void stopAnim() {
        mVibrated = false;
        progress = 0;
        pb_one.clearAnimation();
        pb_two.clearAnimation();
        handler.removeCallbacks(runnable);
    }

}
