package app.brest.utils;

import android.app.Activity;
import android.content.res.Resources;
import android.util.Log;

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

import app.brest.utils.app.brest.game.Game;
import app.brest.utils.app.brest.game.Resource;
import min3d.core.Object3d;
import min3d.core.Object3dContainer;
import min3d.parser.IParser;
import min3d.parser.Parser;

/**
 * Created by ptitcois on 18/08/16.
 */
public class ResourceManager implements Serializable{
    protected ArrayList<Resource> mAllResources = new ArrayList<Resource>();
    protected ArrayList< ArrayList<Resource> > mResourcesAquiered  = new ArrayList<ArrayList<Resource> >();
    protected ArrayList<Resource> mResourcesLeft  = new ArrayList<Resource>();
    protected Resource mLatestResource = null;
    protected ArrayList<Resource> mResourcesPickedUp = new ArrayList<Resource>();
    protected int mNStages;
    public ResourceManager(ArrayList<Resource> x, int n)
    {
        mAllResources =x;
        mNStages = n;
    }




    public Resource getResourceLeftByName(String name)
    {
        for(int i=0; i<mResourcesLeft.size(); i++)
        {
            if(mResourcesLeft.get(i).getName().compareTo(name)==0)
                return mResourcesLeft.get(i);
        }
        return null;
    }

    public void clearPickedUpResources()
    {
        mResourcesPickedUp.clear();
    }

    public ArrayList<Resource> getResourceLeftByName(ArrayList<String> names)
    {
        ArrayList<Resource> res = new ArrayList<Resource>();
        for(int i=0; i< names.size(); i++) {
            res.add(getResourceLeftByName(names.get(i)));
        }
        return res;
    }

    public void nextStage(int stage)
    {
        mResourcesAquiered.add(new ArrayList<Resource>());
        mResourcesLeft.clear();
        for(int i=0; i<mAllResources.size(); i++)
            if(mAllResources.get(i).getStage()==stage)
                mResourcesLeft.add(mAllResources.get(i));
    }

    public boolean pickUpResource(String name)
    {
        for(int i=0; i<mResourcesLeft.size(); i++)
        {
            if(mResourcesLeft.get(i).getName().compareTo(name)==0)
            {
                mResourcesPickedUp.add(mResourcesLeft.get(i));
                mResourcesAquiered.get(mResourcesAquiered.size()-1).add(mResourcesLeft.get(i));
                mLatestResource = mResourcesLeft.get(i);
                mResourcesLeft.remove(i);
                return true;
            }
        }
        return false;
    }

    public ArrayList<Resource> getPickedUpResources()
    {
        return mResourcesPickedUp;
    }

    public ArrayList<Resource> getResourceLeft()
    {
        return mResourcesLeft;
    }

    public ArrayList<Resource> getResourceAquiered(int stage)
    {
        return mResourcesAquiered.get(stage-1);
    }


    public ArrayList< ArrayList<Resource> > getResourceAquiered()
    {
        return mResourcesAquiered;
    }

    public void pickAllResoucres()
    {
        mResourcesLeft.clear();
        mResourcesAquiered.clear();
        for(int i=0; i<mNStages; i++)
        {
            ArrayList<Resource> ar = new ArrayList<Resource>();
            for(int j=0; j<mAllResources.size(); j++) {
                if (mAllResources.get(j).getStage() == i + 1)
                    ar.add(mAllResources.get(j));
            }
            mResourcesAquiered.add(ar);
        }
    }

    public void deleteAll3DModel()
    {
        for(int i=0; i<mAllResources.size(); i++)
            mAllResources.get(i).delete3DModel();
    }

    public int getNResourceAcquired()
    {
        if(mResourcesAquiered.size()>0)
            return mResourcesAquiered.get(mResourcesAquiered.size()-1).size();
        return 0;
    }

    public int getNResourceLeft()
    {
        return mResourcesLeft.size();
    }

    public Resource getLatestResource()
    {
        return mLatestResource;
    }
}
