package play.widget;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import xn.xxp.R;


/**
 * 自动隐藏的工具栏View
 */
public class AutoHideView extends FrameLayout {
    /**
     * 定义的工具栏展示的时间，无操作后10s隐藏
     */
    private static final int HIDE_TIMEOUT_IN_MS = 10 * 1000;

    public AutoHideView(@NonNull Context context) {
        this(context, null);
    }

    public AutoHideView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AutoHideView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        setBackgroundColor(Color.parseColor("#cc000000"));
        inflate(getContext(), R.layout.view_auto_hide, this);
        ImageView switchScreenImage = findViewById(R.id.switch_screen_view);
        switchScreenImage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mOnFullScreenListener) {
                    if (mOnFullScreenListener.toggleFullScreen()) {
                        // 全屏
                        switchScreenImage.setImageResource(R.drawable.ic_common_video_fullscreen_off_white_24);
                    } else {
                        // 退出全屏
                        switchScreenImage.setImageResource(R.drawable.ic_common_video_fullscreen_white_24);
                    }
                }
                resetHideTask();
            }
        });
    }

    private OnFullScreenListener mOnFullScreenListener;

    public void setOnFullScreenListener(OnFullScreenListener listener) {
        this.mOnFullScreenListener = listener;
    }

    public interface OnFullScreenListener {
        boolean toggleFullScreen();
    }

    /**
     * 显示工具栏
     */
    public void show() {
        if (isVisible()) {
            return;
        }
        this.setVisibility(View.VISIBLE);
        this.setAnimation(moveToViewLocation());
        postDelayed(hideControlViewTask, HIDE_TIMEOUT_IN_MS);
    }


    public boolean isVisible() {
        return this.getVisibility() == VISIBLE;
    }

    /**
     * 隐藏工具栏
     */
    public void hide() {
        if (!isVisible()) {
            return;
        }
        this.setVisibility(View.GONE);
        this.setAnimation(moveToViewBottom());
        removeCallbacks(hideControlViewTask);
    }

    /**
     * 被点击的时候，需要重置延时
     */
    private void resetHideTask() {
        removeCallbacks(hideControlViewTask);
        postDelayed(hideControlViewTask, HIDE_TIMEOUT_IN_MS);
    }

    /**
     * 显示动画
     */
    private TranslateAnimation moveToViewLocation() {
        TranslateAnimation translateAnimation = new TranslateAnimation(1, 0.0F, 1, 0.0F, 1, 1.0F, 1, 0.0F);
        translateAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
        translateAnimation.setDuration(400L);
        return translateAnimation;
    }

    /**
     * 隐藏动画，避免隐藏时，控制工具栏一闪而逝
     */
    private TranslateAnimation moveToViewBottom() {
        TranslateAnimation hiddenAction = new TranslateAnimation(1, 0.0F,
                1, 0.0F, 1, 0.0F, 1, 1.0F);
        hiddenAction.setInterpolator(new AccelerateDecelerateInterpolator());
        hiddenAction.setDuration(400L);
        return hiddenAction;
    }

    /**
     * 控制栏自动隐藏任务
     */
    private final Runnable hideControlViewTask = new Runnable() {
        @Override
        public void run() {
            hide();
        }
    };
}
