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

import com.sohutv.tv.player.interfaces.ISohuTVPlayerCallback;
import com.wasu.comp.videoview.IMediaInterceptListener;
import com.wasu.comp.videoview.IMediaListener;

import android.media.MediaPlayer;
import android.view.SurfaceHolder;

import java.lang.ref.WeakReference;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

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

    private List<IMediaListener> mediaListeners;
    private WeakReference<IMediaInterceptListener> mInterceptListener;

    public void setInterceptListener(IMediaInterceptListener listener) {
        this.mInterceptListener = new WeakReference<IMediaInterceptListener>(listener);
    }

    public SohuIMediaListener() {
        this(null);
    }

    public SohuIMediaListener(IMediaInterceptListener listener) {
        this.mediaListeners = new CopyOnWriteArrayList<IMediaListener>();
        if (listener != null) {
            mInterceptListener = new WeakReference<IMediaInterceptListener>(listener);
        }
    }

    @Override
    public void onBufferingUpdate(MediaPlayer mediaPlayer, int i) {
        /*for (IMediaListener listener : mediaListeners) {
            listener.onStatusChanged(mediaPlayer, i);
        }*/
    }

    @Override
    public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
        if (mInterceptListener.get() != null && mInterceptListener.get().onError(mediaPlayer, i, i1)) {
            return false;
        }

        for (IMediaListener listener : mediaListeners) {
            listener.onError(mediaPlayer, i, i1);
        }
        return false;
    }

    @Override
    public void onCompletion(MediaPlayer mediaPlayer) {
        if (mInterceptListener.get() != null && mInterceptListener.get().onCompletion(mediaPlayer)) {
            return;
        }

        for (IMediaListener listener : mediaListeners) {
            listener.onCompletion(mediaPlayer);
        }
    }

    @Override
    public void onPrepared(MediaPlayer mediaPlayer) {
        if (mInterceptListener.get() != null && mInterceptListener.get().onPrepareComplete(mediaPlayer)) {
            return;
        }

        for (IMediaListener listener : mediaListeners) {
            listener.onPrepareComplete(mediaPlayer);
        }
    }

    @Override
    public void onVideoSizeChanged(MediaPlayer mediaPlayer, int i, int i1) {

    }

    @Override
    public boolean onInfo(MediaPlayer mediaPlayer, int i, int i1) {
        if (mInterceptListener.get() != null && mInterceptListener.get().onInfo(mediaPlayer, i, i1)) {
            return false;
        }

        for (IMediaListener listener : mediaListeners) {
            listener.onInfo(mediaPlayer, i, i1);
        }
        return false;
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {

    }

    @Override
    public void OnSeekCompleteListener() {
        if (mInterceptListener.get() != null && mInterceptListener.get().onSeekComplete(null)) {
            return;
        }

        for (IMediaListener listener : mediaListeners) {
            listener.onSeekComplete(null);
        }
    }

    @Override
    public void throwableCallBack(Throwable throwable) {

    }

    @Override
    public void adRemainTime(int i) {

    }

    public void addListener(IMediaListener iMediaListener) {
        mediaListeners.add(iMediaListener);
    }

    public void removeListener(IMediaListener iMediaListener) {
        mediaListeners.remove(iMediaListener);
    }

    public void onStart() {
        if (mInterceptListener.get() != null && mInterceptListener.get().onStart(null)) {
            return;
        }

        for (IMediaListener listener : mediaListeners) {
            listener.onStart(null);
        }
    }

    public void onStop() {
        if (mInterceptListener.get() != null && mInterceptListener.get().onStop(null)) {
            return;
        }

        for (IMediaListener listener : mediaListeners) {
            listener.onStop(null);
        }
    }

    public void onProgress(int currentPosition, int duration) {
        if (mInterceptListener.get() != null && mInterceptListener.get().onProgress(currentPosition, duration, 0)) {
            return;
        }

        for (IMediaListener listener : mediaListeners) {
            listener.onProgress(currentPosition, duration, 0);
        }
    }

    public void onStatusChanged(MediaPlayer mp, int status) {
        if (mInterceptListener.get() != null && mInterceptListener.get().onStatusChanged(mp, status)) {
            return;
        }

        for (IMediaListener listener : mediaListeners) {
            listener.onStatusChanged(mp, status);
        }
    }

    public void onPause(MediaPlayer mp) {
        if (mInterceptListener.get() != null && mInterceptListener.get().onPause(mp)) {
            return;
        }

        for (IMediaListener listener : mediaListeners) {
            listener.onPause(mp);
        }
    }
}
