/*******************************************************************************
 * Copyright 2014 Rafael Garcia Moreno.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.bladecoder.engine.actions;

import com.bladecoder.engine.actions.Param.Type;
import com.bladecoder.engine.model.CharacterActor;
import com.bladecoder.engine.model.Scene;
import com.bladecoder.engine.model.VerbRunner;
import com.bladecoder.engine.model.World;
import com.bladecoder.engine.util.ActionUtils;

@ActionDescription("Execute actions inside the If/EndIf if the scene attribute has the specified value.")
public class IfSceneAttrAction extends AbstractIfAction {

	public enum SceneAttr {
		STATE, CURRENT_SCENE, PLAYER
	}

	@ActionPropertyDescription("The scene to check its attribute")
	@ActionProperty(type = Type.SCENE)
	private String scene;

	@ActionProperty(required = true, defaultValue = "state")
	@ActionPropertyDescription("The scene attribute")
	private SceneAttr attr;

	@ActionProperty
	@ActionPropertyDescription("The attribute value")
	private String value;
	
	private World w;
	
	@Override
	public void init(World w) {
		this.w = w;
	}

	@Override
	public boolean run(VerbRunner cb) {
		Scene s = (scene != null && !scene.isEmpty()) ? w.getScene(scene) : w
				.getCurrentScene();

		if (attr == SceneAttr.STATE) {
			if (!ActionUtils.compareNullStr(value, s.getState())) {
				gotoElse((VerbRunner) cb);
			}
		} else if (attr == SceneAttr.CURRENT_SCENE) {
			String scn = w.getCurrentScene().getId();
			
			if (!ActionUtils.compareNullStr(value, scn)) {
				gotoElse((VerbRunner) cb);
			}
		} else if (attr == SceneAttr.PLAYER) {
			CharacterActor player = s.getPlayer();
			
			String id = player!=null?player.getId():null;
			
			if (!ActionUtils.compareNullStr(value, id)) {
				gotoElse((VerbRunner) cb);
			}			
		}

		return false;
	}
}

@@ -1648,18 +1648,17 @@
  value;%0A
-%09
 %0A
+
 %09private
@@ -1667,18 +1667,17 @@
 orld w;%0A
-%09
 %0A
+
 %09@Overri
@@ -1845,21 +1845,16 @@
 ene) : w
-%0A%09%09%09%09
 .getCurr
@@ -1967,37 +1967,24 @@
 %09%09%09gotoElse(
-(VerbRunner) 
 cb);%0A%09%09%09%7D%0A%09%09
@@ -2070,27 +2070,24 @@
 ().getId();%0A
-%09%09%09
 %0A%09%09%09if (!Act
@@ -2116,16 +2116,63 @@
 r(value,
+ scn) && !ActionUtils.compareNullStr(s.getId(),
  scn)) %7B
@@ -2181,37 +2181,24 @@
 %09%09%09gotoElse(
-(VerbRunner) 
 cb);%0A%09%09%09%7D%0A%09%09
@@ -2278,19 +2278,16 @@
 ayer();%0A
-%09%09%09
 %0A%09%09%09Stri
@@ -2304,15 +2304,19 @@
 ayer
+ 
 !=
+ 
 null
-?
+ ? 
 play
@@ -2329,18 +2329,17 @@
 Id()
-:
+ : 
 null;%0A
-%09%09%09
 %0A%09%09%09
@@ -2401,21 +2401,8 @@
 lse(
-(VerbRunner) 
 cb);
@@ -2406,19 +2406,16 @@
 b);%0A%09%09%09%7D
-%09%09%09
 %0A%09%09%7D%0A%0A%09%09

Check scene name also in CURRENT_SCENE ifSceneAttr.