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_7_7_DNSSystem extends SceneScript
{
	public var _mintickcounter:Float;
	public var _currentpmoram:String;
	public var _valuetoreturn:String;
	public var _timetodisplay:String;
	public var _darkbox:Actor;
	
	/* ========================= Custom Block ========================= */
	public function _customBlock_extendDigits(__original:String, __intendedlength:Float):String
	{
		_valuetoreturn = __original;
		propertyChanged("_valuetoreturn", _valuetoreturn);
		for(index0 in 0...Std.int((__intendedlength - ("" + __original).length)))
		{
			_valuetoreturn = (("" + "0") + ("" + _valuetoreturn));
			propertyChanged("_valuetoreturn", _valuetoreturn);
		}
		return _valuetoreturn;
	}
	
	
	public function new(dummy:Int, dummy2:Engine)
	{
		super();
		nameMap.set("min tick counter", "_mintickcounter");
		_mintickcounter = 0.0;
		nameMap.set("current pm or am", "_currentpmoram");
		_currentpmoram = "";
		nameMap.set("value to return", "_valuetoreturn");
		_valuetoreturn = "";
		nameMap.set("time to display", "_timetodisplay");
		_timetodisplay = "";
		nameMap.set("dark box", "_darkbox");
		
	}
	
	override public function init()
	{
		
		/* ======================== When Creating ========================= */
		createRecycledActor(getActorType(10), 0, 0, Script.FRONT);
		getLastCreatedActor().setBlendMode(BlendMode.MULTIPLY);
		_darkbox = getLastCreatedActor();
		propertyChanged("_darkbox", _darkbox);
		getLastCreatedActor().fadeTo(0, 0, Linear.easeNone);
		getLastCreatedActor().anchorToScreen();
		getLastCreatedActor().makeAlwaysSimulate();
		if(!(Engine.engine.getGameAttribute("INSIDE")))
		{
			if(Engine.engine.getGameAttribute("DNS pm"))
			{
				if(!((Engine.engine.getGameAttribute("DNS Hours") == 12)))
				{
					if((Engine.engine.getGameAttribute("DNS Hours") == 7))
					{
						_darkbox.fadeTo(50 / 100, 0, Linear.easeNone);
					}
					else if((Engine.engine.getGameAttribute("DNS Hours") >= 8))
					{
						_darkbox.fadeTo(100 / 100, 0, Linear.easeNone);
					}
				}
			}
			else
			{
				if(((Engine.engine.getGameAttribute("DNS Hours") == 12) || ((Engine.engine.getGameAttribute("DNS Hours") >= 1) && (Engine.engine.getGameAttribute("DNS Hours") < 6))))
				{
					_darkbox.fadeTo(100 / 100, 0, Linear.easeNone);
				}
				else if((Engine.engine.getGameAttribute("DNS Hours") == 6))
				{
					_darkbox.fadeTo(50 / 100, 0, Linear.easeNone);
				}
			}
		}
		
		/* ========================= When Drawing ========================= */
		addWhenDrawingListener(null, function(g:G, x:Float, y:Float, list:Array<Dynamic>):Void
		{
			if(wrapper.enabled)
			{
				g.setFont(getFont(6));
				if(Engine.engine.getGameAttribute("DNS pm"))
				{
					_currentpmoram = "pm";
					propertyChanged("_currentpmoram", _currentpmoram);
				}
				else
				{
					_currentpmoram = "am";
					propertyChanged("_currentpmoram", _currentpmoram);
				}
				_timetodisplay = (("" + (("" + Engine.engine.getGameAttribute("DNS Hours")) + ("" + (("" + ":") + ("" + "" + sayToScene("DNS System", "_customBlock_extendDigits", [Engine.engine.getGameAttribute("DNS minutes"), 2])))))) + ("" + _currentpmoram));
				propertyChanged("_timetodisplay", _timetodisplay);
				g.alpha = (30/100);
				g.fillColor = Utils.getColorRGB(0,0,0);
				g.fillRect((getScreenWidth() - 46), 3, 46, (g.font.getHeight()/Engine.SCALE + 1));
				g.alpha = (100/100);
				g.drawString("" + _timetodisplay, ((getScreenWidth() - g.font.font.getTextWidth(_timetodisplay)/Engine.SCALE) - 4), 4);
			}
		});
		
		/* ======================== When Updating ========================= */
		addWhenUpdatedListener(null, function(elapsedTime:Float, list:Array<Dynamic>):Void
		{
			if(wrapper.enabled)
			{
				if(!(Engine.engine.getGameAttribute("INSIDE")))
				{
					_mintickcounter += 1;
					propertyChanged("_mintickcounter", _mintickcounter);
				}
				if((_mintickcounter == 3))
				{
					_mintickcounter = asNumber(0);
					propertyChanged("_mintickcounter", _mintickcounter);
					Engine.engine.setGameAttribute("DNS minutes", (Engine.engine.getGameAttribute("DNS minutes") + 1));
					if((Engine.engine.getGameAttribute("DNS minutes") >= 60))
					{
						Engine.engine.setGameAttribute("DNS minutes", 0);
						Engine.engine.setGameAttribute("DNS Hours", (Engine.engine.getGameAttribute("DNS Hours") + 1));
						if(!(Engine.engine.getGameAttribute("INSIDE")))
						{
							if(Engine.engine.getGameAttribute("DNS pm"))
							{
								if((Engine.engine.getGameAttribute("DNS Hours") == 7))
								{
									_darkbox.fadeTo(50 / 100, 2, Quad.easeInOut);
								}
								else if((Engine.engine.getGameAttribute("DNS Hours") == 8))
								{
									_darkbox.fadeTo(100 / 100, 2, Quad.easeInOut);
								}
							}
							else
							{
								if((Engine.engine.getGameAttribute("DNS Hours") == 6))
								{
									_darkbox.fadeTo(50 / 100, 2, Quad.easeInOut);
								}
								else if((Engine.engine.getGameAttribute("DNS Hours") == 7))
								{
									_darkbox.fadeTo(0 / 100, 2, Quad.easeInOut);
								}
							}
						}
						if((Engine.engine.getGameAttribute("DNS Hours") == 12))
						{
							Engine.engine.setGameAttribute("DNS pm", !(Engine.engine.getGameAttribute("DNS pm")));
							if(!(Engine.engine.getGameAttribute("DNS pm")))
							{
								Engine.engine.setGameAttribute("INV Yesterday", Engine.engine.getGameAttribute("INV Today").copy());
								Utils.clear(Engine.engine.getGameAttribute("INV Today"));
							}
						}
						if((Engine.engine.getGameAttribute("DNS Hours") >= 13))
						{
							Engine.engine.setGameAttribute("DNS Hours", 1);
						}
					}
				}
			}
		});
		
	}
	
	override public function forwardMessage(msg:String)
	{
		
	}
}