package macampcorp.macamp.game.scheduler;

import android.app.Activity;
import android.content.Intent;

import java.io.Serializable;
import java.util.ArrayList;

import macampcorp.macamp.activities.HostActivity;
import macampcorp.macamp.game.Event;
import macampcorp.macamp.game.Game;
import macampcorp.macamp.game.Resource;
import macampcorp.macamp.game.challenges.Challenge;
import macampcorp.macamp.utils.L;


public class Scheduler implements Serializable {

    protected Task mStart;
    protected Task mLastTask;

    public Scheduler() {
    }

    public String toString()
    {
        String out = "[ ";
        Task t = mStart;
        for(t=mStart; t!=null; t=t.next()) {
            if(t!=mStart) out+=", ";
            out+=t.toString();
        }
        return out+" ]";
    }

    private void append(Task t)
    {
        //Log.e("___SCHEDULER_DEBUG___AP", "Ajout de : "+t.toString()+" : "+toString());
        if(mStart==null) mStart=t;
        else mStart.enqueue(t);
    }

    public void append(Challenge c)
    {
        ArrayList<Event> a = c.getStartEvent();
        ArrayList<Event> b = c.getEndEvent();

        append(a);
        append(new ChallengeTask(c));
        append(b);
    }


    public void append(Resource c){ append(new ResourceTask(c));}
    public void append(Event c){ append(new EventTask(c));}
    public void append(int x){ append(new SpecialTask(x));}


    public void append(ArrayList<?> cc) {
        ArrayList<Object> c = (ArrayList<Object>) cc;
        for(int i=0; i<c.size(); i++)
            if(c.get(i) instanceof Resource)
                append(new ResourceTask((Resource)c.get(i)));
            else if(c.get(i) instanceof Event)
                append(new EventTask((Event)c.get(i)));
            else if(c.get(i) instanceof Challenge)
                append(new ChallengeTask((Challenge)c.get(i)));
    }

    public void prepend(Resource r) { prepend(new ResourceTask(r));}
    public void prepend(Event r) { prepend(new EventTask(r));}
    public void prepend(int x){ prepend(new SpecialTask(x));}



    public int schedule(HostActivity cd, Game g)
    {
        Task t = mStart;
        mLastTask=t;
        L.error("___SCHEDULER_DEBUG___", "Executing task : " + getTaskListString());
        if(t==null) return Task.TASK_NONE;
        if(t!=null)
        {
            mStart=mStart.next();
            mLastTask.mNext=null;
            return t.exec(this, cd, g);
        }else  mLastTask.mNext=null;
        return Task.TASK_NONE;
    }



    public ArrayList<Task> getTaskList()
    {
        ArrayList<Task> at = new ArrayList<Task>();
        for(Task t=mStart; t!=null ;t=mStart.next())
            at.add(t);
        return at;
    }

    public String getTaskListString()
    {
        String x = "[ ";
        Task t = mStart;
        while(t!=null)
        {
            x+=t.toString();
            t=t.mNext;
        }
        x+=" ]";
        if(mLastTask!=null) x+=" : "+mLastTask.toString();
        return x;
    }

    public Task getLastTask() {
        return mLastTask;
    }

    public Task getStart() {
        return mStart;
    }

    private void prepend(Task t)
    {
        Task sec = mStart;
        mStart=t;
        mStart.end().mNext=sec;
    }

    public void clear()
    {
        mStart=null;
    }

    public void undo()
    {
        if(mLastTask!=null)
        {
            prepend(mLastTask);
            mLastTask=null;
        }
    }
}
