package com.xunao.effectdemo.dlan;

import android.util.Log;

import com.hpplay.sdk.source.api.ILelinkPlayerListener;
import com.hpplay.sdk.source.api.INewPlayerListener;
import com.hpplay.sdk.source.bean.CastBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 推送
 */
public class CastManager {
    private final static String TAG = "DemoCastManager";
    private static CastManager sInstance = null;
    private final List<ILelinkPlayerListener> mListenerList = new ArrayList<>();
    private final List<INewPlayerListener> mListenerList2 = new ArrayList<>();
    private final ILelinkPlayerListener mLelinkPlayerListener = new ILelinkPlayerListener() {

        @Override
        public void onLoading() {
            for (ILelinkPlayerListener listener : mListenerList) {
                listener.onLoading();
            }
        }

        @Override
        public void onStart() {
            for (ILelinkPlayerListener listener : mListenerList) {
                listener.onStart();
            }
        }

        @Override
        public void onPause() {
            for (ILelinkPlayerListener listener : mListenerList) {
                listener.onPause();
            }
        }

        @Override
        public void onCompletion() {
            for (ILelinkPlayerListener listener : mListenerList) {
                listener.onCompletion();
            }
        }

        @Override
        public void onStop() {
            for (ILelinkPlayerListener listener : mListenerList) {
                listener.onStop();
            }
        }

        @Override
        public void onSeekComplete(int i) {
            for (ILelinkPlayerListener listener : mListenerList) {
                listener.onSeekComplete(i);
            }
        }

        @Override
        public void onInfo(final int what, final int extra) {
            for (ILelinkPlayerListener listener : mListenerList) {
                listener.onInfo(what, extra);
            }
        }

        @Override
        public void onInfo(int what, final String data) {
            for (ILelinkPlayerListener listener : mListenerList) {
                listener.onInfo(what, data);
            }
        }

        @Override
        public void onError(int what, int extra) {
            Log.i(TAG, "onError what:" + what + " extra:" + extra);
            for (ILelinkPlayerListener listener : mListenerList) {
                listener.onError(what, extra);
            }
        }

        @Override
        public void onVolumeChanged(float v) {
            for (ILelinkPlayerListener listener : mListenerList) {
                listener.onVolumeChanged(v);
            }
        }

        @Override
        public void onPositionUpdate(long duration, long position) {
            for (ILelinkPlayerListener listener : mListenerList) {
                listener.onPositionUpdate(duration, position);
            }
        }
    };

    public synchronized static CastManager getInstance() {
        synchronized (CastManager.class) {
            if (sInstance == null) {
                sInstance = new CastManager();
            }
        }
        return sInstance;
    }

    private CastManager() {

    }

    public ILelinkPlayerListener getLelinkPlayerListener() {
        return mLelinkPlayerListener;
    }

    public void addPlayerListener(ILelinkPlayerListener listener) {
        if (mListenerList.size()>0){
            mListenerList.clear();
        }
        mListenerList.add(listener);
    }

    public void removeListener(ILelinkPlayerListener listener) {
        if (listener != null){
            mListenerList.remove(listener);
        }

    }

    private final INewPlayerListener mPlayerListener = new INewPlayerListener() {

        @Override
        public void onLoading(CastBean bean) {
            for (INewPlayerListener listener : mListenerList2) {
                listener.onLoading(bean);
            }
        }

        @Override
        public void onStart(CastBean bean) {
            for (INewPlayerListener listener : mListenerList2) {
                listener.onStart(bean);
            }
        }

        @Override
        public void onPause(CastBean bean) {
            for (INewPlayerListener listener : mListenerList2) {
                listener.onPause(bean);
            }
        }

        @Override
        public void onCompletion(CastBean bean, int type) {
            for (INewPlayerListener listener : mListenerList2) {
                listener.onCompletion(bean, type);
            }
        }

        @Override
        public void onStop(CastBean bean) {
            for (INewPlayerListener listener : mListenerList2) {
                listener.onStop(bean);
            }
        }

        @Override
        public void onSeekComplete(CastBean bean, int position) {
            for (INewPlayerListener listener : mListenerList2) {
                listener.onSeekComplete(bean, position);
            }
        }

        @Override
        public void onInfo(CastBean bean, final int what, final int extra) {
            for (INewPlayerListener listener : mListenerList2) {
                listener.onInfo(bean, what, extra);
            }
        }

        @Override
        public void onInfo(CastBean bean, int what, final String data) {
            for (INewPlayerListener listener : mListenerList2) {
                listener.onInfo(bean, what, data);
            }
        }

        @Override
        public void onError(CastBean bean, int what, int extra) {
//            Logger.i(TAG, "onError what:" + what + " extra:" + extra);
            for (INewPlayerListener listener : mListenerList2) {
                listener.onError(bean, what, extra);
            }
        }

        @Override
        public void onVolumeChanged(CastBean bean, float percent) {
            for (INewPlayerListener listener : mListenerList2) {
                listener.onVolumeChanged(bean, percent);
            }
        }

        @Override
        public void onPositionUpdate(CastBean bean, long duration, long position) {
            for (INewPlayerListener listener : mListenerList2) {
                listener.onPositionUpdate(bean, duration, position);
            }
        }
    };


    public INewPlayerListener getPlayerListener() {
        return mPlayerListener;
    }

    public void addPlayerListener(INewPlayerListener listener) {
        mListenerList2.add(listener);
    }

    public void removeListener(INewPlayerListener listener) {
        mListenerList2.remove(listener);
    }

}
