package com.wasu.cs.widget.videoview.ext.sohu;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import com.sohutv.tv.player.interfaces.ISohuTVPlayerCallback;
import com.sohutv.tv.player.partner.SohuTvPlayer;
import com.wasu.comp.videoview.IMediaInterceptListener;
import com.wasu.comp.videoview.IMediaListener;
import com.wasu.comp.videoview.UrlProperty;
import com.wasu.cs.widget.videoview.ext.IExMediaControl;

/**
 * Created by zhangrm on 16-1-28.
 */
public class SohuIMediaControl extends SohuIMediaListener implements IExMediaControl {

    private SohuTvPlayer sohuTvPlayer;
    private SohuIMediaListener sohuIMediaListener;
    private boolean issuspend; // suspend标志，解决抢先看支付跳出时无法真正关闭播放

    /**
     * 为了确保搜狐资产能够播放，play方法之前需设定issuspend 为 false
     * */
    public void setIssuspend(boolean issuspend) {
        this.issuspend = issuspend;
    }

    private IMediaListener mediaListener = new IMediaListener() {
        @Override
        public void onStart(MediaPlayer mp) {

        }

        @Override
        public void onPreparing(MediaPlayer mp) {

        }

        @Override
        public void onPrepareComplete(MediaPlayer mp) {
            isPrepared = true;

            if (seekTime != -1) {
                sohuTvPlayer.seekTo(seekTime);
                seekTime = -1;
            }

            sohuTvPlayer.start();
            sohuIMediaListener.onStatusChanged(mp, IMediaListener.STATE_PREPARED);
            progressHandler.sendEmptyMessage(0);
        }

        @Override
        public void onSeeking(MediaPlayer mp) {

        }

        @Override
        public void onSeekComplete(MediaPlayer mp) {

        }

        @Override
        public void onProgress(int position, int duration, int buffingPercent) {

        }

        @Override
        public void onPause(MediaPlayer mp) {

        }

        @Override
        public void onResume(MediaPlayer mp) {

        }

        @Override
        public void onStop(MediaPlayer mp) {
            isPrepared = false;
            progressHandler.removeMessages(0);
        }

        @Override
        public void onCompletion(MediaPlayer mp) {
            isPrepared = false;
            progressHandler.removeMessages(0);
        }

        @Override
        public void onError(MediaPlayer mp, int what, int extra) {
            isPrepared = false;
            progressHandler.removeMessages(0);
        }

        @Override
        public void onInfo(MediaPlayer mp, int what, int extra) {

        }

        @Override
        public void onStatusChanged(MediaPlayer mp, int currentStatus) {

        }

        @Override
        public void onAdStatusChanged(int status, int extra) {

        }

        @Override
        public void onWasuError(int errorCode, String extra) {

        }

        @Override
        public void onWasuPlayLimit(int status, String extra) {

        }
    };

    class ProgressHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            sohuIMediaListener.onProgress(sohuTvPlayer.getCurrentPosition(), sohuTvPlayer.getDuration());

            if (isPrepared) {
                sendEmptyMessageDelayed(0, 1000);
            }
        }
    }
    private ProgressHandler progressHandler;

    private boolean isPrepared;
    private int seekTime = -1;

    public SohuIMediaControl(SohuTvPlayer sohuTvPlayer, ISohuTVPlayerCallback callback) {
        this.sohuTvPlayer = sohuTvPlayer;
        if (this.sohuTvPlayer != null) {
            sohuTvPlayer.setPlayerCallback(callback);
        }

        progressHandler = new ProgressHandler();
        sohuIMediaListener = (SohuIMediaListener) callback;
        sohuIMediaListener.addListener(mediaListener);
    }

    @Override
    public View getMediaControl() {
        return sohuTvPlayer;
    }

    @Override
    public void setVideoParam(String jsonParam) {
        if (sohuTvPlayer != null && !issuspend) {
            sohuTvPlayer.setVideoParam(jsonParam);
        }
    }

    @Override
    public void pause() {
        if (sohuTvPlayer != null) {
            sohuTvPlayer.pause();
        }
        sohuIMediaListener.onPause(null);
    }

    @Override
    public void resume() {
        if (sohuTvPlayer != null) {
            sohuTvPlayer.resume();
            issuspend = false;
        }
    }

    @Override
    public void start() {
        if (sohuTvPlayer != null && !issuspend && isPrepared) {
            sohuTvPlayer.start();
        }
    }

    @Override
    public void stop() {
        sohuIMediaListener.onStop();
        if (isPrepared && sohuTvPlayer != null) {
            sohuTvPlayer.stop();
        }
    }

    @Override
    public void release() {
        isPrepared = false;
        if (sohuTvPlayer != null) {
            sohuTvPlayer.release();
        }
    }

    @Override
    public void seekTo(int msec) {
        if (sohuTvPlayer == null) {
            return;
        }

        if (isPrepared) {
            sohuTvPlayer.seekTo(msec);
        } else {
            seekTime = msec;
        }
    }

    @Override
    public boolean isPlaying() {
        if (sohuTvPlayer != null) {
            return sohuTvPlayer.isPlaying();
        }
        return false;
    }

    @Override
    public boolean isAdPlaying() {
        if (sohuTvPlayer != null) {
            sohuTvPlayer.isAdPlaying();
        }
        return false;
    }

    @Override
    public int getVideoWidth() {
        if (sohuTvPlayer != null) {
            return sohuTvPlayer.getVideoWidth();
        }
        return 0;
    }

    @Override
    public int getVideoHeight() {
        if (sohuTvPlayer != null) {
            return sohuTvPlayer.getVideoHeight();
        }
        return 0;
    }

    @Override
    public boolean isInPlaybackState() {
        return isPrepared;
    }

    @Override
    public int getDuration() {
        if (isPrepared && sohuTvPlayer != null) {
            return sohuTvPlayer.getDuration();
        }
        return 0;
    }

    @Override
    public int getCurrentPosition() {
        if (isPrepared && sohuTvPlayer != null) {
            return sohuTvPlayer.getCurrentPosition();
        }
        return 0;
    }

    @Override
    public void setVideoPath(String s, UrlProperty urlProperty) throws IllegalArgumentException {
        if (sohuTvPlayer != null && !issuspend) {
            sohuTvPlayer.setVideoParam(s);
        }
        sohuIMediaListener.onStart();
    }

    @Override
    public void setVideoClips(int i, int i1) {

    }

    @Override
    public void resume(String s) {
        if (sohuTvPlayer != null) {
            sohuTvPlayer.resume();
        }
    }

    @Override
    public void suspend() {
        if (sohuTvPlayer != null) {
            seekTime = sohuTvPlayer.getCurrentPosition();
            sohuTvPlayer.stop();
            sohuIMediaListener.onStop();
            issuspend = true;
        }
    }

    @Override
    public void stopPlayback() {
        sohuIMediaListener.onStop();
        seekTime = -1;
        if (sohuTvPlayer != null) {
            sohuTvPlayer.stop();
        }
    }



    @Override
    public void stopPlayback(boolean clearAdStatus) {
        stopPlayback();
    }

    @Override
    public boolean isPreparing() {
        return !isPrepared;
    }

    @Override
    public void setPlayType(PlayType playType) {

    }

    @Override
    public int getCurrentADDuration() {
        return 0;
    }

    @Override
    public int getCurrentADPosition() {
        return 0;
    }

    @Override
    public void addObserver(IMediaListener iMediaListener) {
        if (sohuIMediaListener != null) {
            sohuIMediaListener.addListener(iMediaListener);
        }
    }

    @Override
    public void removeObserver(IMediaListener iMediaListener) {
        if (sohuIMediaListener != null) {
            sohuIMediaListener.removeListener(iMediaListener);
        }
    }

    @Override
    public void setInterceptListener(IMediaInterceptListener listener) {
        if (sohuIMediaListener != null) {
            sohuIMediaListener.setInterceptListener(listener);
        }
    }

    @Override
    public void postOnStart() {
        sohuIMediaListener.onStart();
    }

    @Override
    public void postOnPreparing() {

    }

    @Override
    public void postOnPrepareComplete() {
        sohuIMediaListener.onPrepared(null);
    }

    @Override
    public void postOnSeeking() {

    }

    @Override
    public void postOnSeekcomplete() {

    }

    @Override
    public void postOnPause() {
        sohuIMediaListener.onPause(null);
    }

    @Override
    public void postOnResume() {

    }

    @Override
    public void postOnStop() {
        sohuIMediaListener.onStop();
    }

    @Override
    public void postOnCompletion() {
        sohuIMediaListener.onCompletion(null);
    }

    @Override
    public void postOnError(int i, int i1) {

    }

    @Override
    public void postOnInfo(int i, int i1) {

    }

    public void setDimension(int width, int height) {

    }

    public void setDimensionOriginal() {

    }

    public void setDimensionAutio() {

    }

    public void setDimensionFullScreen() {

    }

    public void setDimension_16_9() {

    }

    public void setDimension_4_3() {

    }

    public void setDefinition(int definition, int currentPosition) {

    }

    public void setPauseADTopMarginPercent(int topMarginPercent) {

    }

    public void onLogResume(Activity mActivity) {

    }

    public void onLogStop(Activity mActivity) {

    }


}
