package com.tencent.liteav.demo.superplayer.ui.player;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Vibrator;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tencent.liteav.demo.superplayer.R;
import com.tencent.liteav.demo.superplayer.SuperPlayerDef;
import com.tencent.liteav.demo.superplayer.SuperPlayerModel;
import com.tencent.liteav.demo.superplayer.ui.view.UnlockProgressView;

/**
 * 全屏模式播放控件
 * <p>
 * 除{@link WindowPlayer}基本功能外，还包括进度条关键帧打点信息显示与跳转、快进快退时缩略图的显示、切换画质
 * 镜像播放、硬件加速、倍速播放、弹幕、截图等功能
 * <p>
 * 2、触摸事件监听{@link #onTouchEvent(MotionEvent)}
 */
public class FullScreenPlayer extends AbsPlayer implements UnlockProgressView.OnUnlockListener {
    private UnlockProgressView mIvLock;                                // 锁屏按钮
    private TextView mIvLockText;
    private ImageView mImageCover;                            // 封面图

    private boolean isShowing;  // 自身是否可见
    private SuperPlayerDef.PlayerType mPlayType;   // 当前播放视频类型
    private SuperPlayerDef.PlayerState mCurrentPlayState = SuperPlayerDef.PlayerState.END; // 当前播放状态

    private Bitmap mBackgroundBmp; // 背景图

    private boolean isDestroy = false;  // Activity是否被销毁

    public FullScreenPlayer(Context context) {
        super(context);
        initialize(context);
    }

    public FullScreenPlayer(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize(context);
    }

    public FullScreenPlayer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize(context);
    }

    /**
     * 初始化控件、手势检测监听器、亮度/音量/播放进度的回调
     */
    private void initialize(Context context) {
        initView(context);
    }

    /**
     * 初始化view
     */
    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.superplayer_vod_player_fullscreen, this);

        mIvLock = (UnlockProgressView) findViewById(R.id.superplayer_iv_lock);
        mIvLock.setUnlockListener(this);

        mIvLockText = (TextView) findViewById(R.id.superplayer_iv_lock_text);

        mImageCover = (ImageView) findViewById(R.id.superplayer_cover_view);
    }

    public void setPlayNextButtonVisibility(boolean isShowing) {

    }

    public void preparePlayVideo(SuperPlayerModel superPlayerModel) {
        updateTitle(superPlayerModel.title);
        if (!isDestroy) {
            Glide.with(getContext())
                    .load(superPlayerModel.cover)
                    .placeholder(R.mipmap.default_cover_dark)
                    .into(mImageCover);
        }
        toggleView(mImageCover, true);
        updateVideoProgress(0, superPlayerModel.duration);
    }

    /**
     * 显示控件
     */
    @Override
    public void show() {
        isShowing = true;
        mIvLock.setVisibility(VISIBLE);
        mIvLockText.setVisibility(VISIBLE);
    }

    /**
     * 隐藏控件
     */
    @Override
    public void hide() {
        isShowing = false;
        mIvLock.setVisibility(GONE);
        mIvLockText.setVisibility(GONE);
    }

    /**
     * 释放控件的内存
     */
    @Override
    public void release() {
        isDestroy = true;
    }

    public void toggleCoverView(boolean isVisible) {
        toggleView(mImageCover, isVisible);
    }

    public void prepareLoading() {

    }

    @Override
    public void updatePlayState(SuperPlayerDef.PlayerState playState) {
        mCurrentPlayState = playState;
    }

    /**
     * 更新视频名称
     *
     * @param title 视频名称
     */
    @Override
    public void updateTitle(String title) {

    }

    /**
     * 更新实时播放进度
     *
     * @param current  当前进度(秒)
     * @param duration 视频总时长(秒)
     */
    @Override
    public void updateVideoProgress(long current, long duration) {

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (isShowing) {
                hide();
            } else {
                show();
            }
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            if (isShowing) {
                postDelayed(mHideViewRunnable, HIDDEN_DELAY);
            }
        }
        return true;
    }

    @Override
    public void startUnlock() {
        removeCallbacks(mHideViewRunnable);
    }

    @Override
    public void unlockSuccess() {
        if (mControllerCallback != null) {
            mControllerCallback.onSwitchPlayMode(SuperPlayerDef.PlayerMode.WINDOW);
        }
        Vibrator service = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);
        service.vibrate(100);
    }

    @Override
    public void unlockFailed() {
        postDelayed(mHideViewRunnable, HIDDEN_DELAY);
    }
}
