package com.viaccessorca.voplayerdemo.downloader;
import android.util.Log;
import android.content.Context;

import com.viaccessorca.vodownloader.VODownloader;
import com.viaccessorca.vodownloader.VODownloader.AudioTrackInfo;


public class DownloaderWrapper extends VODownloader implements VODownloader.OnErrorListener, VODownloader.OnInfoListener, VODownloader.OnPreparedListener{

    public interface OnErrorListener {
        boolean onError(DownloaderWrapper downloader, int what, int extra);
    }
    public interface OnPreparedListener {
        void onPrepared(DownloaderWrapper downloader);
    }

    public interface OnInfoListener
    {
        boolean onInfo(DownloaderWrapper downloader, int what, int extra);
    }

    public enum State {
        UNAVAILABLE,
        NEW,
        INITIALIZED,
        DOWNLOADING,
        RENEWINGKEYS,
        PREPARED,
        PAUSED,
        ERROR,
    }

    private static final String TAG = "DownloaderWrapper";

    protected OnPreparedListener externPreparedListener = null;
    protected OnErrorListener externErrorListener = null;
    protected OnInfoListener externInfoListener = null;

    private String currentUri = null;

    private State state;


    public DownloaderWrapper(Context ctxt, String mediaId) {
        super(ctxt, mediaId);
        state = State.NEW;
    }

    public State getState() {
        return state;
    }

    protected void setState(State newState) {
        synchronized (state) {
            state = newState;
        }
    }

    public String getUri() {
        return currentUri;
    }


    public void init() throws IllegalStateException
    {
        synchronized (state) {
            if(State.NEW != state) {
                Log.w(TAG, "init - Wrong state: " + state.name());
                throw new IllegalStateException("Wrong state: " + state.name());
            }
        }
        super.init();
        super.setOnErrorListener(this);
        super.setOnInfoListener(this);
        super.setOnPreparedListener(this);

        setState(State.INITIALIZED);
    }

    public void start(){
        super.start();
        setState(State.DOWNLOADING);
    }

    public void renewOfflineKeys(){
        if (state != State.DOWNLOADING){
             setState(State.RENEWINGKEYS);
        }
        super.renewOfflineKeys();
    }

    public void stop(){
        super.stop();
        setState(State.PAUSED);
    }

    public void setUri(String uri){
        super.setDataSource(uri);
        currentUri = uri;
    }

    public boolean onError(VODownloader gateway, int what, int extra){
        if(null != externErrorListener){
            setState(State.ERROR);
            return externErrorListener.onError(this,what,extra);
        }else{
            setState(State.UNAVAILABLE);
        }
        return false;
    }

    public boolean onInfo(VODownloader gateway, int what, int extra){
        if(null != externInfoListener){
            if(what == DownloadManager.DOWNLOADER_INFO_DOWNLOAD_COMPLETED){
                setState(State.PREPARED);
            }
            return externInfoListener.onInfo(this,what,extra);
        }else{
            setState(State.UNAVAILABLE);
        }
        return false;
    }

    public void onPrepared(VODownloader gateway){
        if(null != externPreparedListener){
            setState(State.PREPARED);
            externPreparedListener.onPrepared(this);
        }else{
            setState(State.UNAVAILABLE);
        }
    }

    public void setWrapperOnErrorListener(OnErrorListener listener) {
        Log.e(TAG, "setOnErrorListener");
        externErrorListener = listener;
    }

    public void setWrapperOnInfoListener(OnInfoListener listener) {
        Log.e(TAG, "setOnInfoListener");
        externInfoListener = listener;
    }

    public void setWrapperOnPreparedListener(OnPreparedListener listener) {
        externPreparedListener = listener;
        Log.e(TAG, "setOnPreparedListener");
    }
}
