package de.tudarmstadt.informatik.hostage.sync.wifi_direct;

import android.os.AsyncTask;


/**
 * Created by Julien on 07.01.2015.
 */
public class BackgroundTask extends AsyncTask<Void, Void, String> {

    /**
     * This listener calls didSucceed if the method performInBackground will return true, otherwise it calls didFail.
     */
    public interface BackgroundTaskCompletionListener {
        public void didSucceed();
        public void didFail();
    }

    private boolean isInterrupted;

    private boolean wasSuccessfully;

    public void interrupt(boolean b){
        this.isInterrupted = b;
    }

    public boolean isInterrupted() {
        return isInterrupted;
    }

    private BackgroundTaskCompletionListener completion_listener;

    public BackgroundTask(BackgroundTaskCompletionListener l){
        super();
        this.completion_listener = l;
    }

    /**
     * Interrupts the background process.
     * @param isInterrupted
     */
    public void setInterrupted(boolean isInterrupted) {
        this.isInterrupted = isInterrupted;
    }


    /**
     * Do any stuff here if it should run in the background. It should return true or false if the process was successfully.
     * @return boolean
     */
    public boolean performInBackground(){
        return true;
    }

    @Override
    protected String doInBackground(Void... params) {
        this.wasSuccessfully = this.performInBackground();
        return null;
    }

    /*
     * (non-Javadoc)
     * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
     */
    @Override
    protected void onPostExecute(String result) {
        if (this.completion_listener != null) {
            if (this.wasSuccessfully) {
                this.completion_listener.didSucceed();
            }else {
                this.completion_listener.didFail();
            }
        }
    }

    /*
     * (non-Javadoc)
     * @see android.os.AsyncTask#onPreExecute()
     */
    @Override
    protected void onPreExecute() {


    }

}