package scripts;

import com.stencyl.graphics.G;
import com.stencyl.graphics.BitmapWrapper;

import com.stencyl.behavior.Script;
import com.stencyl.behavior.Script.*;
import com.stencyl.behavior.ActorScript;
import com.stencyl.behavior.SceneScript;
import com.stencyl.behavior.TimedTask;

import com.stencyl.models.Actor;
import com.stencyl.models.GameModel;
import com.stencyl.models.actor.Animation;
import com.stencyl.models.actor.ActorType;
import com.stencyl.models.actor.Collision;
import com.stencyl.models.actor.Group;
import com.stencyl.models.Scene;
import com.stencyl.models.Sound;
import com.stencyl.models.Region;
import com.stencyl.models.Font;
import com.stencyl.models.Joystick;

import com.stencyl.Engine;
import com.stencyl.Input;
import com.stencyl.Key;
import com.stencyl.utils.Utils;

import openfl.ui.Mouse;
import openfl.display.Graphics;
import openfl.display.BlendMode;
import openfl.display.BitmapData;
import openfl.display.Bitmap;
import openfl.events.Event;
import openfl.events.KeyboardEvent;
import openfl.events.TouchEvent;
import openfl.net.URLLoader;

import box2D.common.math.B2Vec2;
import box2D.dynamics.B2Body;
import box2D.dynamics.B2Fixture;
import box2D.dynamics.joints.B2Joint;
import box2D.collision.shapes.B2Shape;

import motion.Actuate;
import motion.easing.Back;
import motion.easing.Cubic;
import motion.easing.Elastic;
import motion.easing.Expo;
import motion.easing.Linear;
import motion.easing.Quad;
import motion.easing.Quart;
import motion.easing.Quint;
import motion.easing.Sine;

import com.stencyl.graphics.shaders.BasicShader;
import com.stencyl.graphics.shaders.GrayscaleShader;
import com.stencyl.graphics.shaders.SepiaShader;
import com.stencyl.graphics.shaders.InvertShader;
import com.stencyl.graphics.shaders.GrainShader;
import com.stencyl.graphics.shaders.ExternalShader;
import com.stencyl.graphics.shaders.InlineShader;
import com.stencyl.graphics.shaders.BlurShader;
import com.stencyl.graphics.shaders.SharpenShader;
import com.stencyl.graphics.shaders.ScanlineShader;
import com.stencyl.graphics.shaders.CSBShader;
import com.stencyl.graphics.shaders.HueShader;
import com.stencyl.graphics.shaders.TintShader;
import com.stencyl.graphics.shaders.BloomShader;



class Design_12_12_roomdecoration extends SceneScript
{
	public var _menuup:Bool;
	public var _iteration:Float;
	public var _state:String;
	public var _currentlistlocation:Float;
	public var _currentlististoday:Bool;
	public var _currentfurnituretoplace:String;
	public var _currentfurnitureactor:Actor;
	public var _currentlyselectedtile:Float;
	public var _currentlyselectedtiley:Float;
	public var _tempmap:Map<String,Dynamic>;
	public var _placementselectionchanged:Bool;
	public var _grabicon:Actor;
	public var _grabselectionrectangle:Actor;
	public var _currentlyhighlightedfurniture:Actor;
	public var _3d:Actor;
	public var _hueshiftfactornumberdigit:Float;
	
	/* ========================= Custom Event ========================= */
	public function _customEvent_choose():Void
	{
		_state = "choose";
		propertyChanged("_state", _state);
		_currentlistlocation = asNumber(0);
		propertyChanged("_currentlistlocation", _currentlistlocation);
		_currentlististoday = true;
		propertyChanged("_currentlististoday", _currentlististoday);
		if(((hasValue(_currentfurnitureactor)) && _currentfurnitureactor.isAlive()))
		{
			recycleActor(_currentfurnitureactor);
		}
	}
	
	/* ========================= Custom Event ========================= */
	public function _customEvent_pickup():Void
	{
		_state = "choose";
		propertyChanged("_state", _state);
		_currentlistlocation = asNumber(0);
		propertyChanged("_currentlistlocation", _currentlistlocation);
		_currentlististoday = true;
		propertyChanged("_currentlististoday", _currentlististoday);
		if(((hasValue(_currentfurnitureactor)) && _currentfurnitureactor.isAlive()))
		{
			recycleActor(_currentfurnitureactor);
		}
	}
	
	/* ========================= Custom Event ========================= */
	public function _customEvent_newActiveFurn():Void
	{
		if(((hasValue(_currentfurnitureactor)) && _currentfurnitureactor.isAlive()))
		{
			recycleActor(_currentfurnitureactor);
		}
		createRecycledActor(getActorTypeByName((("" + "FRN ") + ("" + _currentfurnituretoplace))), 0, 0, Script.FRONT);
		_currentfurnitureactor = getLastCreatedActor();
		propertyChanged("_currentfurnitureactor", _currentfurnitureactor);
	}
	
	
	public function new(dummy:Int, dummy2:Engine)
	{
		super();
		nameMap.set("menu up", "_menuup");
		_menuup = false;
		nameMap.set("iteration", "_iteration");
		_iteration = 0.0;
		nameMap.set("state", "_state");
		_state = "";
		nameMap.set("current list location", "_currentlistlocation");
		_currentlistlocation = 0.0;
		nameMap.set("current list is today", "_currentlististoday");
		_currentlististoday = false;
		nameMap.set("current furniture to place name", "_currentfurnituretoplace");
		_currentfurnituretoplace = "";
		nameMap.set("current furniture actor", "_currentfurnitureactor");
		nameMap.set("currently selected tile x", "_currentlyselectedtile");
		_currentlyselectedtile = 0.0;
		nameMap.set("currently selected tile y", "_currentlyselectedtiley");
		_currentlyselectedtiley = 0.0;
		nameMap.set("temp map", "_tempmap");
		nameMap.set("placement selection changed", "_placementselectionchanged");
		_placementselectionchanged = false;
		nameMap.set("grab icon", "_grabicon");
		nameMap.set("grab selection rectangle", "_grabselectionrectangle");
		nameMap.set("currently highlighted furniture", "_currentlyhighlightedfurniture");
		nameMap.set("3d", "_3d");
		nameMap.set("hue shift factor number digit", "_hueshiftfactornumberdigit");
		_hueshiftfactornumberdigit = 0.0;
		
	}
	
	override public function init()
	{
		
		/* ======================== When Creating ========================= */
		for(item in cast(Engine.engine.getGameAttribute("room array"), Array<Dynamic>))
		{
			createRecycledActor(getActorTypeByName((("" + "FRN ") + ("" + item.get("name")))), (asNumber(item.get("x")) * 16), (asNumber(item.get("y")) * 16), Script.FRONT);
			getLastCreatedActor().shout("_customEvent_" + "placeMarkers");
			getLastCreatedActor().setAnimation("" + item.get("anim"));
		}
		createRecycledActor(getActorType(25), 80, 0, Script.FRONT);
		_grabicon = getLastCreatedActor();
		propertyChanged("_grabicon", _grabicon);
		getLastCreatedActor().disableActorDrawing();
		createRecycledActor(getActorType(48), (getScreenWidth() - 40), 4, Script.FRONT);
		_3d = getLastCreatedActor();
		propertyChanged("_3d", _3d);
		getLastCreatedActor().disableActorDrawing();
		
		/* ======================== When Updating ========================= */
		addWhenUpdatedListener(null, function(elapsedTime:Float, list:Array<Dynamic>):Void
		{
			if(wrapper.enabled)
			{
				if(isKeyPressed("enter"))
				{
					playSound(getSound(43));
					if(_menuup)
					{
						_3d.disableActorDrawing();
						stopSoundOnChannel(Std.int(30));
						_menuup = false;
						propertyChanged("_menuup", _menuup);
						Engine.engine.setGameAttribute("disable movement", false);
						if(((hasValue(_grabselectionrectangle)) && _grabselectionrectangle.isAlive()))
						{
							recycleActor(_grabselectionrectangle);
						}
					}
					else
					{
						loopSoundOnChannel(getSound(46), Std.int(30));
						_state = "choose";
						propertyChanged("_state", _state);
						_menuup = true;
						propertyChanged("_menuup", _menuup);
						_currentlististoday = true;
						propertyChanged("_currentlististoday", _currentlististoday);
						Engine.engine.setGameAttribute("disable movement", true);
						for(actorOfType in getActorsOfType(getActorType(0)))
						{
							if(actorOfType != null && !actorOfType.dead && !actorOfType.recycled){
								actorOfType.setX(152);
								actorOfType.setY(176);
							}
						}
					}
				}
				if(_menuup)
				{
					if((_state == "choose"))
					{
						_hueshiftfactornumberdigit += 2;
						propertyChanged("_hueshiftfactornumberdigit", _hueshiftfactornumberdigit);
						_3d.clearFilters();
						_3d.setFilter([createHueFilter(_hueshiftfactornumberdigit)]);
						if(isKeyPressed("action2"))
						{
							_3d.disableActorDrawing();
							playSound(getSound(43));
							stopSoundOnChannel(Std.int(30));
							_menuup = false;
							propertyChanged("_menuup", _menuup);
							Engine.engine.setGameAttribute("disable movement", false);
							if(((hasValue(_grabselectionrectangle)) && _grabselectionrectangle.isAlive()))
							{
								recycleActor(_grabselectionrectangle);
							}
						}
						if(isKeyPressed("down"))
						{
							playSound(getSound(36));
							_currentlistlocation += 1;
							propertyChanged("_currentlistlocation", _currentlistlocation);
							if(((_currentlististoday && (_currentlistlocation > (Engine.engine.getGameAttribute("INV Today").length - 1))) || (!(_currentlististoday) && (_currentlistlocation > (Engine.engine.getGameAttribute("INV Yesterday").length - 1)))))
							{
								_currentlistlocation = asNumber(0);
								propertyChanged("_currentlistlocation", _currentlistlocation);
								_currentlististoday = !(_currentlististoday);
								propertyChanged("_currentlististoday", _currentlististoday);
							}
						}
						else if(isKeyPressed("up"))
						{
							playSound(getSound(36));
							_currentlistlocation -= 1;
							propertyChanged("_currentlistlocation", _currentlistlocation);
							if((0 > _currentlistlocation))
							{
								_currentlististoday = !(_currentlististoday);
								propertyChanged("_currentlististoday", _currentlististoday);
								if(_currentlististoday)
								{
									_currentlistlocation = asNumber((Engine.engine.getGameAttribute("INV Today").length - 1));
									propertyChanged("_currentlistlocation", _currentlistlocation);
								}
								else
								{
									_currentlistlocation = asNumber((Engine.engine.getGameAttribute("INV Yesterday").length - 1));
									propertyChanged("_currentlistlocation", _currentlistlocation);
								}
							}
						}
						else if(isKeyPressed("right"))
						{
							playSound(getSound(36));
							_grabicon.setAnimation("" + "on");
							_state = "overGrab";
							propertyChanged("_state", _state);
						}
						if(isKeyPressed("action1"))
						{
							playSound(getSound(42));
							if(((_currentlististoday && !((Engine.engine.getGameAttribute("INV Today").length == 0))) || (!(_currentlististoday) && !((Engine.engine.getGameAttribute("INV Yesterday").length == 0)))))
							{
								if(_currentlististoday)
								{
									_currentfurnituretoplace = Engine.engine.getGameAttribute("INV Today")[Std.int(_currentlistlocation)];
									propertyChanged("_currentfurnituretoplace", _currentfurnituretoplace);
								}
								else
								{
									_currentfurnituretoplace = Engine.engine.getGameAttribute("INV Yesterday")[Std.int(_currentlistlocation)];
									propertyChanged("_currentfurnituretoplace", _currentfurnituretoplace);
								}
								_state = "place";
								propertyChanged("_state", _state);
								_placementselectionchanged = true;
								propertyChanged("_placementselectionchanged", _placementselectionchanged);
								_currentlyselectedtile = asNumber(5);
								propertyChanged("_currentlyselectedtile", _currentlyselectedtile);
								_currentlyselectedtiley = asNumber(5);
								propertyChanged("_currentlyselectedtiley", _currentlyselectedtiley);
								_customEvent_newActiveFurn();
							}
						}
					}
					else if((_state == "overGrab"))
					{
						if(isKeyPressed("left"))
						{
							playSound(getSound(36));
							_state = "choose";
							propertyChanged("_state", _state);
							_grabicon.setAnimation("" + "off");
						}
						if(isKeyPressed("action1"))
						{
							playSound(getSound(36));
							_state = "grab";
							propertyChanged("_state", _state);
							createRecycledActor(getActorType(20), 0, 0, Script.FRONT);
							_grabselectionrectangle = getLastCreatedActor();
							propertyChanged("_grabselectionrectangle", _grabselectionrectangle);
							_grabicon.setAnimation("" + "off");
						}
					}
					else if((_state == "grab"))
					{
						if(isKeyPressed("down"))
						{
							_placementselectionchanged = true;
							propertyChanged("_placementselectionchanged", _placementselectionchanged);
							_currentlyselectedtiley += 1;
							propertyChanged("_currentlyselectedtiley", _currentlyselectedtiley);
							playSound(getSound(36));
						}
						else if(isKeyPressed("up"))
						{
							_placementselectionchanged = true;
							propertyChanged("_placementselectionchanged", _placementselectionchanged);
							_currentlyselectedtiley -= 1;
							propertyChanged("_currentlyselectedtiley", _currentlyselectedtiley);
							playSound(getSound(36));
						}
						if(isKeyPressed("left"))
						{
							_placementselectionchanged = true;
							propertyChanged("_placementselectionchanged", _placementselectionchanged);
							_currentlyselectedtile -= 1;
							propertyChanged("_currentlyselectedtile", _currentlyselectedtile);
							playSound(getSound(36));
						}
						else if(isKeyPressed("right"))
						{
							_placementselectionchanged = true;
							propertyChanged("_placementselectionchanged", _placementselectionchanged);
							_currentlyselectedtile += 1;
							propertyChanged("_currentlyselectedtile", _currentlyselectedtile);
							playSound(getSound(36));
						}
						if(_placementselectionchanged)
						{
							if(((hasValue(_currentlyhighlightedfurniture)) && _currentlyhighlightedfurniture.isAlive()))
							{
								_currentlyhighlightedfurniture.clearFilters();
							}
							for(actorOfType in getActorsOfType(getActorType(22)))
							{
								if(actorOfType != null && !actorOfType.dead && !actorOfType.recycled){
									if(((actorOfType.getX() == (_currentlyselectedtile * 16)) && (actorOfType.getY() == (_currentlyselectedtiley * 16))))
									{
										if(!(actorOfType.getActorValue("parent") == null))
										{
											_currentlyhighlightedfurniture = actorOfType.getActorValue("parent");
											propertyChanged("_currentlyhighlightedfurniture", _currentlyhighlightedfurniture);
											actorOfType.getActorValue("parent").clearFilters();
											actorOfType.getActorValue("parent").setFilter([createBrightnessFilter(30)]);
										}
									}
								}
							}
						}
						_grabselectionrectangle.setX((_currentlyselectedtile * 16));
						_grabselectionrectangle.setY((_currentlyselectedtiley * 16));
						if(isKeyPressed("action1"))
						{
							for(actorOfType in getActorsOfType(getActorType(22)))
							{
								if(actorOfType != null && !actorOfType.dead && !actorOfType.recycled){
									if(((actorOfType.getX() == (_currentlyselectedtile * 16)) && (actorOfType.getY() == (_currentlyselectedtiley * 16))))
									{
										if(!(actorOfType.getActorValue("parent") == null))
										{
											playSound(getSound(39));
											actorOfType.getActorValue("parent").clearFilters();
											_currentfurnituretoplace = actorOfType.getActorValue("parent").getValue("Pickup Item", "_itemname");
											propertyChanged("_currentfurnituretoplace", _currentfurnituretoplace);
											actorOfType.getActorValue("parent").shout("_customEvent_" + "destroyMarkers");
											/* "" */ _iteration = asNumber(0);
											propertyChanged("_iteration", _iteration);
											for(item in cast(Engine.engine.getGameAttribute("room array"), Array<Dynamic>))
											{
												if((((item.get("x") == (actorOfType.getActorValue("parent").getX() / 16)) && (item.get("y") == (actorOfType.getActorValue("parent").getY() / 16))) && (item.get("name") == _currentfurnituretoplace)))
												{
													Engine.engine.getGameAttribute("room array").splice(Std.int(_iteration), 1);
													break;
												}
												_iteration += 1;
												propertyChanged("_iteration", _iteration);
											}
											recycleActor(actorOfType.getActorValue("parent"));
											recycleActor(_grabselectionrectangle);
											_state = "place";
											propertyChanged("_state", _state);
											_placementselectionchanged = true;
											propertyChanged("_placementselectionchanged", _placementselectionchanged);
											Engine.engine.getGameAttribute("INV Today").push(_currentfurnituretoplace);
											_customEvent_newActiveFurn();
										}
									}
								}
							}
						}
						else if(isKeyPressed("action2"))
						{
							playSound(getSound(36));
							if(((hasValue(_currentlyhighlightedfurniture)) && _currentlyhighlightedfurniture.isAlive()))
							{
								_currentlyhighlightedfurniture.clearFilters();
							}
							recycleActor(_grabselectionrectangle);
							_customEvent_choose();
						}
						_placementselectionchanged = false;
						propertyChanged("_placementselectionchanged", _placementselectionchanged);
					}
					else if((_state == "place"))
					{
						if(isKeyPressed("down"))
						{
							_placementselectionchanged = true;
							propertyChanged("_placementselectionchanged", _placementselectionchanged);
							_currentlyselectedtiley += 1;
							propertyChanged("_currentlyselectedtiley", _currentlyselectedtiley);
							playSound(getSound(36));
						}
						else if(isKeyPressed("up"))
						{
							_placementselectionchanged = true;
							propertyChanged("_placementselectionchanged", _placementselectionchanged);
							_currentlyselectedtiley -= 1;
							propertyChanged("_currentlyselectedtiley", _currentlyselectedtiley);
							playSound(getSound(36));
						}
						if(isKeyPressed("left"))
						{
							_placementselectionchanged = true;
							propertyChanged("_placementselectionchanged", _placementselectionchanged);
							_currentlyselectedtile -= 1;
							propertyChanged("_currentlyselectedtile", _currentlyselectedtile);
							playSound(getSound(36));
						}
						else if(isKeyPressed("right"))
						{
							_placementselectionchanged = true;
							propertyChanged("_placementselectionchanged", _placementselectionchanged);
							_currentlyselectedtile += 1;
							propertyChanged("_currentlyselectedtile", _currentlyselectedtile);
							playSound(getSound(36));
						}
						_currentfurnitureactor.setX((_currentlyselectedtile * 16));
						_currentfurnitureactor.setY((_currentlyselectedtiley * 16));
						if(_placementselectionchanged)
						{
							if(cast(_currentfurnitureactor.say("Room Collision Data", "_customBlock_checkFurnPlaceable"), Bool))
							{
								_currentfurnitureactor.clearFilters();
							}
							else
							{
								_currentfurnitureactor.clearFilters();
								_currentfurnitureactor.setFilter([createTintFilter(Utils.getColorRGB(255,0,51), 70/100)]);
							}
						}
						if(isKeyPressed("action1"))
						{
							if(cast(_currentfurnitureactor.say("Room Collision Data", "_customBlock_checkFurnPlaceable"), Bool))
							{
								playSound(getSound(41));
								_tempmap = new Map<String, Dynamic>();
								propertyChanged("_tempmap", _tempmap);
								_tempmap.set("name", _currentfurnituretoplace);
								_tempmap.set("x", _currentlyselectedtile);
								_tempmap.set("y", _currentlyselectedtiley);
								_tempmap.set("anim", _currentfurnitureactor.getAnimation());
								Engine.engine.getGameAttribute("room array").push(_tempmap);
								trace("" + _currentfurnituretoplace);
								trace("" + _currentlististoday);
								if(_currentlististoday)
								{
									Engine.engine.getGameAttribute("INV Today").remove(_currentfurnituretoplace);
								}
								else
								{
									Engine.engine.getGameAttribute("INV Yesterday").remove(_currentfurnituretoplace);
									playSound(getSound(35));
								}
								_currentfurnitureactor.shout("_customEvent_" + "placeMarkers");
								_currentfurnitureactor = getDefaultValue(_currentfurnitureactor);
								_customEvent_choose();
							}
						}
						else if(isKeyPressed("action2"))
						{
							playSound(getSound(36));
							_customEvent_choose();
						}
						_placementselectionchanged = false;
						propertyChanged("_placementselectionchanged", _placementselectionchanged);
					}
				}
			}
		});
		
		/* ========================= When Drawing ========================= */
		addWhenDrawingListener(null, function(g:G, x:Float, y:Float, list:Array<Dynamic>):Void
		{
			if(wrapper.enabled)
			{
				if(_menuup)
				{
					g.setFont(getFont(6));
					g.moveTo(40, 4);
					g.drawString("" + "-TODAY-", -((g.font.font.getTextWidth("-TODAY-")/Engine.SCALE / 2)), 0);
					g.translate(0, 8);
					_iteration = asNumber(0);
					propertyChanged("_iteration", _iteration);
					for(item in cast(Engine.engine.getGameAttribute("INV Today"), Array<Dynamic>))
					{
						if(((_currentlististoday && (_currentlistlocation == _iteration)) && (_state == "choose")))
						{
							g.fillColor = Utils.getColorRGB(87,174,199);
							g.fillRect(-40, 0, 80, 10);
							/* "" */ _3d.enableActorDrawing();
							_3d.setAnimation("" + item);
							_3d.setX(((getScreenWidth() - 40) - (_3d.getWidth()/2)));
						}
						g.drawString("" + item, -((g.font.font.getTextWidth(item)/Engine.SCALE / 2)), 0);
						g.translate(0, 8);
						_iteration += 1;
						propertyChanged("_iteration", _iteration);
					}
					g.translate(0, 16);
					g.drawString("" + "-TRASH BIN-", -((g.font.font.getTextWidth("-TRASH BIN-")/Engine.SCALE / 2)), 0);
					g.translate(0, 8);
					_iteration = asNumber(0);
					propertyChanged("_iteration", _iteration);
					for(item in cast(Engine.engine.getGameAttribute("INV Yesterday"), Array<Dynamic>))
					{
						if(((!(_currentlististoday) && (_currentlistlocation == _iteration)) && (_state == "choose")))
						{
							g.fillColor = Utils.getColorRGB(87,174,199);
							g.fillRect(-40, 0, 80, 10);
							/* "" */ _3d.enableActorDrawing();
							_3d.setAnimation("" + item);
							_3d.setX(((getScreenWidth() - 40) - (_3d.getWidth()/2)));
						}
						g.drawString("" + item, -((g.font.font.getTextWidth(item)/Engine.SCALE / 2)), 0);
						g.translate(0, 8);
						_iteration += 1;
						propertyChanged("_iteration", _iteration);
					}
					g.moveTo(80, 0);
					_grabicon.drawImage(g);
					if(!((_state == "choose")))
					{
						_3d.disableActorDrawing();
					}
				}
			}
		});
		
	}
	
	override public function forwardMessage(msg:String)
	{
		
	}
}