package com.haochuan.hciptvbasic;

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.view.Surface;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.VideoView;

import androidx.annotation.RawRes;

import com.haochuan.core.Logger;



/**
 * Created by hzqiujiadi on 16/4/5.
 * hzqiujiadi ashqalcn@gmail.com
 *
 * http://developer.android.com/intl/zh-cn/reference/android/media/MediaPlayer.html
 * status
 */
public class SystemPlayerWrapper implements MediaPlayer.OnPreparedListener {
    protected VideoView mPlayer;
    private MediaPlayer.OnPreparedListener mPreparedListener;
    private static final int STATUS_IDLE = 0;
    private static final int STATUS_PREPARING = 1;
    private static final int STATUS_PREPARED = 2;
    private static final int STATUS_STARTED = 3;
    private static final int STATUS_PAUSED = 4;
    private static final int STATUS_STOPPED = 5;
    private int mStatus = STATUS_IDLE;

    private Surface surface;

    public void init(Context context){
        mStatus = STATUS_IDLE;
        mPlayer = new VideoView(context);
        mPlayer.setOnPreparedListener(this);
        mPlayer.setOnInfoListener(new MediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(MediaPlayer mp, int what, int extra) {
                return false;
            }
        });

        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mPlayer.setLayoutParams(params);


        if (mPlayer.getParent() == null) {
            Activity activity = (Activity)context;
            ViewGroup viewGroup = (ViewGroup)activity.getWindow().getDecorView();
            viewGroup.addView(mPlayer, 0);
        }
    }



    public void setSurface(Surface surface){
        this.surface = surface;
    }


    public void openRemoteFile(String path){
        try {
            mPlayer.setVideoPath(path);
            mPlayer.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void openRawFile(Context context, @RawRes int videoId) {
        String rawPath = "android.resource://" + context.getPackageName() + "/" + videoId;
        openRemoteFile(rawPath);
    }


    public VideoView getPlayer() {
        return mPlayer;
    }


    public void stop(){
        if (mPlayer == null) return;
        if (mStatus == STATUS_STARTED || mStatus ==  STATUS_PAUSED){
            mPlayer.stopPlayback();
            mStatus = STATUS_STOPPED;
        }
    }

    public void pause(){
        if (mPlayer == null) return;
        if (mPlayer.isPlaying() && mStatus == STATUS_STARTED) {
            mPlayer.pause();
            mStatus = STATUS_PAUSED;
        }
    }

    private void start(){
        if (mPlayer == null) return;
        if (mStatus == STATUS_PREPARED || mStatus == STATUS_PAUSED){
            mPlayer.start();
            mStatus = STATUS_STARTED;
        }

    }

    public void setPreparedListener(MediaPlayer.OnPreparedListener mPreparedListener) {
        this.mPreparedListener = mPreparedListener;
    }


    public void resume() {
        start();
    }

    public void destroy() {
        stop();
        if (mPlayer != null) {
            mPlayer.stopPlayback();
        }
        mPlayer = null;
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        mStatus = STATUS_PREPARED;
        if(this.surface != null){
            Logger.d("准备好，设置surface");
            mp.setSurface(this.surface);
        }
        if (mPreparedListener != null) mPreparedListener.onPrepared(mp);
    }

    public void seek(boolean isBack){
        final int currentPosition = mPlayer.getCurrentPosition();
        if(isBack){
            Logger.d("后退");
            mPlayer.seekTo(currentPosition - 10000 >=0?currentPosition-10000:0);
        }else{
            Logger.d("前进");
            mPlayer.seekTo(currentPosition + 10000 < mPlayer.getDuration()?mPlayer.getDuration() -1000:currentPosition + 10000);
        }
    }
}
