package library.core.object;

import java.util.ArrayList;

import library.core.debug.CoreLog;
import library.core.model.CoreModel;
import library.core.screen.CoreScreen;
import library.core.view.CoreView;

public class CoreObjectFactory {


	private CoreScreen screen;

	private ArrayList<CoreObject> createObjects;

	public CoreObjectFactory(CoreScreen screen){
		this.screen=screen;
		this.createObjects=new ArrayList<CoreObject>();
	}

	public CoreObject create(CoreModel model,CoreView view){

		CoreObject object=new CoreObject(model, view);
		screen.addView(view);

		this.createObjects.add(object);

		return object;
	}

	/**
	 * オブジェクト削除
	 * @param object
	 */
	public void delete(CoreObject object){
		this.screen.deleteView(object.getView());
		this.createObjects.remove(object);
		object=null;
	}

	public void deleteAll(){
		int objectSize=createObjects.size();

		//たぶんこれで全部消える
		for(int i=objectSize-1;i>-1;i--){
			this.delete(createObjects.get(i));
		}
	}

	/**
	 * オブジェクトの削除　（モデルデータを基に削除）
	 * @param model
	 */
	public void delete(CoreModel model){
		CoreObject o=null;

		for(int i=0;i<createObjects.size();i++){
			if(createObjects.get(i).getModel().equals(model)){
				o=createObjects.get(i);
			}
		}

		if(o==null){
			CoreLog.error("null desu");
			return ;
		}

		this.delete(o);


	}



}
