package com.sensetime.demo.util;

import android.view.View;
import android.view.MotionEvent;
import android.os.Handler;
import android.annotation.SuppressLint;
import java.util.ArrayList;

public class ViewShowUtils {
    private static final boolean AUTO_HIDE = true;
    private static final int AUTO_HIDE_DELAY_MILLIS = 1000;
    private static final int UI_ANIMATION_DELAY = 100;
    private final Handler mHideHandler = new Handler();
    private ArrayList<View> mViews = new ArrayList<>();
    private boolean mVisible = true;

    public ViewShowUtils(){
        mViews.clear();
    }

    public void addView(View v){
        mViews.add(v);
    }

    public void toggle(){
        if(mVisible){
            hide();
        }else{
            show();
        }
    }


    public void delayedHide(int delayMillis) {
        mHideHandler.removeCallbacks(mHideRunnable);
        mHideHandler.postDelayed(mHideRunnable, delayMillis);
    }
    private final Runnable mShowPart2Runnable = new Runnable() {
        @Override
        public void run() {
            for(View v:mViews){
                v.setVisibility(View.VISIBLE);
            }
        }
    };

    private final Runnable mHideRunnable = new Runnable() {
        @Override
        public void run() {
            hide();
        }
    };

    private void hide() {

        for(View v:mViews){
            v.setVisibility(View.GONE);
        }
        mVisible = false;
        mHideHandler.removeCallbacks(mShowPart2Runnable);
    }
    @SuppressLint("InlinedApi")
    private void show(){
        mVisible = true;
        mHideHandler.postDelayed(mShowPart2Runnable,UI_ANIMATION_DELAY);
    }
}
