package  {
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.*;
	import flash.filesystem.File;
	import flash.net.FileFilter;
	import Defs.GraphicDef;
	import flash.text.engine.GraphicElement;
	
	public class NewGraphicDialog extends MovieClip
	{
		
		public function NewGraphicDialog()
		{
			// constructor code
			Init();
		}

		var typeButtons:Array = new Array();
		
		public function Init()
		{
			graphicPath.text = "Path to Graphic Here";

			this.removeChild(browseButton);
			
			var newBrowseButton = new UI_GenericBlueButton();
			newBrowseButton.Text = "Browse";
			newBrowseButton.OnClick = BrowseForFile;
			newBrowseButton.x = browseButton.x + (newBrowseButton.width / 2);
			newBrowseButton.y = browseButton.y + (newBrowseButton.height / 2);
			addChild(newBrowseButton);
			
			var newTestButton = new UI_GenericBlueButton();
			newTestButton.Text = "Export";
			newTestButton.OnClick = TestFile;
			newTestButton.x = testButton.x + (newTestButton.width / 2);
			newTestButton.y = testButton.y + (newTestButton.height / 2);
			addChild(newTestButton);
			
			this.removeChild(typePlaceholderButton);

			var typeX = typePlaceholderButton.x;
			var typeY = typePlaceholderButton.y + (typePlaceholderButton.height / 2);
			
			
			var typeButton = new UI_GenericBlueButton();
			typeButton.Text = "Graphic (1)";
			typeButton.OnClick = function(){ SetType(1); }
			typeButton.x = typeX + (typeButton.width / 2);
			typeButton.y = typeY;
			typeButtons.push(typeButton);
			addChild(typeButton);
			
			typeX += typeButton.width + 5;
			
			typeButton = new UI_GenericBlueButton();
			typeButton.Text = "Skeletal (3)";
			typeButton.OnClick = function(){ SetType(3); }
			typeButton.x = typeX + (typeButton.width / 2);
			typeButton.y = typeY;
			typeButtons.push(typeButton);
			addChild(typeButton);
			
			typeX += typeButton.width + 5;
			
			typeButton = new UI_GenericBlueButton();
			typeButton.Text = "Scene (6)";
			typeButton.OnClick = function(){ SetType(6); }
			typeButton.x = typeX + (typeButton.width / 2);
			typeButton.y = typeY;
			typeButtons.push(typeButton);
			addChild(typeButton);

			animationCheckbox.Checked = true;
			//browseButton
			//typePlaceholderButton
			//usesInput
			//usesText.text = ""
		}

		var usesInputDefaultText = "Comma Seperated List of Uses Here";
		
		public static var fakeID = 9999999;
		public function TestFile()
		{
			fakeID++;
			var graphicName = graphicPath.text;
			var graphicData = {id:fakeID,graphic:graphicPath.text,v:1,fs:0,p:0,type:selectedType};

			//"export_params":{"uses":["background"],"available_sizes":["default"],"compress":false}}
			
			var graphicDef = new GraphicDef(graphicData);
			
			this.Hide();
			graphicExport.ExportItem(graphicDef);
		}
		
		var selectedType = 1;
		public function SetType(type:Number)
		{
			selectedType = type;
			
			for(var i = 0; i < typeButtons.length; i++)
			{
				typeButtons[i].Enabled = true;
			}

			animationCheckbox.visible = false;
			
			switch(type)
			{
				case 1: {
					typeButtons[0].Enabled = false;
					animationCheckbox.visible = true;
					break;
				}
				case 3: typeButtons[1].Enabled = false; break;
				case 6: typeButtons[2].Enabled = false; break;
			}
		}

		var graphicExport:GraphicExport;
		public function Show(parentClip)
		{
			this.graphicExport = parentClip;
			
			parentClip.addChild(this);
			
			SetType(selectedType);
			
			var graphicList = GameData.Instance().GraphicDefines;
			
			var usesString:String = "";
			var uniqueUseTypes:Array = new Array();
			for (var i = 0; i < graphicList.length; i++)
			{
				var graphicDef:GraphicDef = graphicList[i];
				var uses = graphicDef.ExportParams['uses'];
				for (var usage in uses)
				{
					if (uniqueUseTypes[uses[usage]] == null)
					{
						uniqueUseTypes[uses[usage]] = uses[usage];
						usesString += uses[usage] + ", ";
					}
				}
			}
			
			graphicPath.text = "";
			
		}
		
		public function CloseClicked(event)
		{
			Hide();
		}
		
		public function Hide()
		{
			if (this.parent != null) this.parent.removeChild(this);
		}
		
		public function BrowseForFile()
		{
			var file:File  = new File();
			var swfTypeFilter:FileFilter = new FileFilter("SWF Files","*.swf");
			file.addEventListener(Event.SELECT, openFile);              
			file.browse([swfTypeFilter]);
		}

		private function openFile(event:Event):void
		{
			_strace(event.target.nativePath);
			var path:String = event.target.nativePath;
			var pathParts:Array = path.split("Graphics\\");
			if (pathParts.length == 2)
			{
				var fileRemainder:String = pathParts[1];
				var withoutExtension:String = fileRemainder.split(".")[0];
				var pattern:RegExp = /\\/g;
				withoutExtension = withoutExtension.replace(pattern, "/");
				_strace(withoutExtension);
				graphicPath.text = withoutExtension;

				if (withoutExtension.indexOf("Backgrounds") > -1)
				{
					SetType(6);
				}
				
				if (withoutExtension.indexOf("Monsters") > -1)
				{
					SetType(3);
				}
		
				if (withoutExtension.indexOf("Characters") > -1)
				{
					SetType(3);
				}
				
				if (withoutExtension.indexOf("Portraits") > -1)
				{
					SetType(1);
				}
				
				if (withoutExtension.indexOf("Quest") > -1)
				{
					SetType(1);
				}
				
				if (withoutExtension.indexOf("Equipment") > -1)
				{
					SetType(1);
				}

				
			}
		}
		
		
	}
	
}
