package;

import flixel.input.gamepad.FlxGamepad;
import flixel.FlxG;
import openfl.Assets;
#if web
import js.Syntax;
import js.Browser;
import js.lib.Date;
#end

import flixel.math.FlxPoint;
import flixel.graphics.frames.FlxAtlasFrames;
import flixel.util.FlxSave;


#if html5
@:expose
#end
class Reg {

	public static var playstate:PlayState;

	public static var gamepad:FlxGamepad;

	static inline public function lineOverlap(min1, max1, min2, max2):Float {
		return Math.max(0, Math.min(max1, max2) - Math.max(min1, min2));
	}
	
	static inline public function almostEqual(baseNum:Float, otherNum:Float, give:Float = 0.1):Bool {
		if (baseNum + give > otherNum && baseNum - give < otherNum) return true;
		else return false;
	}
	
	static public function floatToStringPrecision(n:Float, prec:Int):String{
		n = Math.round(n * Math.pow(10, prec));
		var str = '' + n;
		var len = str.length;
		if(len <= prec){
			while(len < prec){
				str = '0' + str;
				len++;
			}
			if (str.substr(str.length - prec) == '00') return '0';
			else return '0.' + str;
		}
		else {
			if (str.substr(str.length - prec) == '00') return str.substr(0, str.length-prec);
			return str.substr(0, str.length-prec) + '.' + str.substr(str.length - prec);
		}
	}

	// This assumes a string with whole number without spaces //
	static public function addSpacesInIntegerString(string:String):String
	{
		if (string.length <= 4)
			return string;
		else if (string.length <= 6) {
			var subString:String = string.substring(string.length - 3, string.length);
			subString = " " + subString;
	
			string = string.substring(0, string.length - 3);
	
			string = string + subString;
			return string;
		}
		else if (string.length <= 9) {
			var subString:String = string.substring(string.length - 3, string.length);
			subString = " " + subString;
			string = string.substring(0, string.length - 3);
			string = string + subString;
	
			subString = string.substring(string.length - 7, string.length);
			subString = " " + subString;
			string = string.substring(0, string.length - 7);
			string = string + subString;
	
			return string;
		}
	
		return string;
	}


	static public function gameToScreenX(x:Float):Float {
		//centerX = FlxG.camera.scroll.x + FlxG.width * 0.5;  //where Reg.mouseX should be if in center
        //adjustedMouseX = ((Reg.mouseX - centerX) * FlxG.camera.zoom) + centerX - FlxG.camera.scroll.x;
		return (((x - (FlxG.camera.scroll.x + FlxG.width * 0.5)) * FlxG.camera.zoom) + (FlxG.camera.scroll.x + FlxG.width * 0.5) - FlxG.camera.scroll.x);
	}
	static public function gameToScreenY(y:Float):Float {
		return (((y - (FlxG.camera.scroll.y + FlxG.height * 0.5)) * FlxG.camera.zoom) + (FlxG.camera.scroll.y + FlxG.height * 0.5) - FlxG.camera.scroll.y);
	}

	static public function timeFromFloat(time:Float):String {
		var timeText:String;

		var hours:Int = Std.int(time / 3600);
		var minutes:Int = Std.int(time / 60) - hours * 60;
		var seconds:Int = Std.int(time - hours * 3600 - minutes * 60);
		var hSeconds:Int = Std.int((time - Std.int(time)) * 100);

		var minutesStr:String;
		var secondsStr:String;
		var hSecondsStr:String;

		if (minutes < 10 && hours > 0)
			minutesStr = "0" + minutes;
		else
			minutesStr = "" + minutes;
		if (seconds < 10 && (minutes > 0 || hours > 0))
			secondsStr = "0" + seconds;
		else {
			if (seconds > 9)
				secondsStr = "" + seconds;
			else
				secondsStr = "0" + seconds;
		}
		if (hSeconds < 10 && (seconds > 0 || minutes > 0 || hours > 0))
			hSecondsStr = "0" + hSeconds;
		else
			hSecondsStr = "" + hSeconds;

		if (hours > 0)
			timeText = "" + hours + ":" + minutesStr + ":" + secondsStr;
		else
			timeText = "" + minutes + ":" + secondsStr + "." + hSecondsStr;

		return timeText;
	}

	static public function pointInArea(X:Float, Y:Float, X2:Float, Y2:Float, W:Float, H:Float):Bool {
		if (X >= X2 && X <= X2 + W && Y >= Y2 && Y <= Y2 + H)
			return true;
		return false;
	}
}
