package ai.pai.lensman.upload;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

import java.util.concurrent.CopyOnWriteArrayList;

import ai.pai.lensman.bean.PhotoBean;

public class UploadManager extends Service implements IUploadManager<PhotoBean>,UploadListener<PhotoBean>{

    private CopyOnWriteArrayList<PhotoBean> uploadList;
    private CopyOnWriteArrayList<UploadListener<PhotoBean>> listeners;
    private PhotoBean uploadingPhoto;

    @Override
    public void onCreate() {
        super.onCreate();
        listeners = new CopyOnWriteArrayList<>();
        uploadList = new CopyOnWriteArrayList<>();

    }

    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void startUpload(PhotoBean photoBean) {
        if (uploadList.contains(photoBean)) {
            return;
        }
        uploadList.add(photoBean);
        if(uploadingPhoto==null){
            //TODO
        }
    }

    @Override
    public void stopUpload(PhotoBean photoBean) {
        if(photoBean!=null && photoBean.equals(uploadingPhoto)){

        }
    }

    @Override
    public void registerUploadListener(UploadListener<PhotoBean> listener) {
        listeners.add(listener);
    }

    @Override
    public void unregisterUploadListener(UploadListener<PhotoBean> listener) {
        listeners.remove(listener);
    }

    @Override
    public void onUploadSuccess(PhotoBean photoBean) {
        for(UploadListener<PhotoBean> listener:listeners){
            listener.onUploadSuccess(photoBean);
        }
    }

    @Override
    public void onUploadFail(PhotoBean photoBean) {
        for(UploadListener<PhotoBean> listener:listeners){
            listener.onUploadFail(photoBean);
        }
    }

    private void _upload(PhotoBean photoBean){

    }


    public class MyBinder extends Binder {

        public UploadManager getService(){
            return UploadManager.this;
        }

    }
}
