-package sfxworks.services 
+﻿package sfxworks.services 
 {
 	import by.blooddy.crypto.MD5; //Awesome guy
-	import com.maclema.mysql.Connection;
-	import com.maclema.mysql.MySqlToken;
-	import com.maclema.mysql.ResultSet;
-	import com.maclema.mysql.Statement;
+	import flash.data.SQLResult;
+	import flash.data.SQLStatement;
+	import flash.errors.SQLError;
 	import flash.events.Event;
 	import flash.events.EventDispatcher;
 	import flash.filesystem.File;

			}
			
			//Get the last row in the table
			var st:Statement = mysqlConnection.createStatement();
			st.sql = "SELECT * FROM files "
				+ "ORDER BY `id` DESC "
				+ "LIMIT 1;";
			var t:MySqlToken = st.executeQuery();
			t.addResponder(new AsyncResponder(getLastRowAddFileSuccess, mysqlNearIDUpdateError, t));
			
			//It's inside a function so I can handle a ton of addFile() methods at once since it's asyncronous 
			function getLastRowAddFileSuccess(data:Object, token:MySqlToken):void
			{
				var fs:FileStream = new FileStream();
				var rs:ResultSet = new ResultSet(token);
				
				var lastEndIndex:Number;
				if (rs.next())
				{
					lastEndIndex = parseFloat(rs.getString("groupID"));
				}
				else
				{
					lastEndIndex = 0;
				}
				
				var numberOfSplits:Number = Math.ceil(file.size / SPLIT_SIZE);
				
				if (lastEndIndex + numberOfSplits > 9007199254740992)
				{
					//A new group must be made. Tell all clients.
					numberOfGroups ++;
				}
				var startIndex:Number = lastEndIndex + 1;
				var endIndex:Number = startIndex + numberOfSplits;
				
				//write to sql --- 
				
				//generate md5s
				var md5s:String = new String()
				var fileData:ByteArray = new ByteArray();
				fs.open(file, FileMode.READ);
					for (var i:Number = 0; i < numberOfSplits; i++)
					{
						var tmp:ByteArray = new ByteArray();
						
						var toRead:Number = SPLIT_SIZE;
						if (fs.bytesAvailable < SPLIT_SIZE)
						{
							toRead = fs.bytesAvailable;
						}
						
						fs.readBytes(tmp, 0, toRead);
						var newMd5:String = MD5.hashBytes(tmp);
						md5s = md5s + newMd5;
						trace("MD5 HASH:" + newMd5 + " for file sector " + i.toString());
					}
				fs.close();
				
				//Mysql format: id(autogen) | FileName.ext | Start Index | EndIndex |  groupID | md5
				var submitSt:Statement = mysqlConnection.createStatement();
				submitSt.sql = "INSERT INTO files (`filename`, `startindex`, `endindex`, `groupid`, `md5`)"
					+ " VALUES ('" + file.name + "'," + startIndex.toString() + "," + endIndex.toString() + "," + numberOfGroups.toString() + ",'" + md5s + "');";
					
				trace("Submitting file to central database..");
				var submitToken:MySqlToken = submitSt.executeQuery();
				submitToken.addResponder(new AsyncResponder(submitSuccess, submitFailure, submitToken));
				
				//Yo dawg I heard you like functions
				function submitSuccess(data:Object, token:MySqlToken):void
				{
					trace("Writing to group..");
					//write to group
					c.addHaveObject(SERVICE_NAME + numberOfGroups.toString(), startIndex, endIndex);
					
					//write to index
					trace("Writing to index..");
					for (var i:Number = startIndex; i < endIndex; i++)
					{
						_filePaths.push(file.nativePath);
						_fileIDs.push(i);
						_groupIDs.push(numberOfGroups);
						_fileStartIndex.push(startIndex);
						_fileEndIndex.push(endIndex);
					}
					
					trace("Writing to filesharing info..");
					//write to filesharinginfo
					var fs:FileStream = new FileStream();
					
					fs.open(fileSharingInfo, FileMode.WRITE);
						fs.writeFloat(new Number(_filePaths.length)) //Write number of files to first float
						fs.close();
					fs.open(fileSharingInfo, FileMode.APPEND);
						fs.writeUTF(file.nativePath);
						fs.writeFloat(numberOfGroups);
						fs.writeFloat(lastEndIndex + 1);
						fs.close();
					
					dispatchEvent(new FileSharingEvent(FileSharingEvent.FILE_ADDED, "Successfully added file " + file.name, file.nativePath, startIndex, endIndex
					, numberOfGroups));
				}
				
				function submitFailure(data:Object, token:MySqlToken):void
				{
					trace("Final submit failure. Mysql Error: " + data);
					dispatchEvent(new FileSharingEvent(FileSharingEvent.ERROR, "Failed to added file " + file.name + ". Couldn't submit to database..."));
					return; 
				}
			}
			
			function mysqlNearIDUpdateError(info:Object, token:MySqlToken):void
			{
				trace("MYSQL ERROR: " + info);
				dispatchEvent(new FileSharingEvent(FileSharingEvent.ERROR, "Failed to contact database for file submission.", file.name));
			}
		}
		
		

	import sfxworks.Communications;
	import sfxworks.NetworkGroupEvent;
	import flash.errors.IOError;
	import sfxworks.services.events.DatabaseServiceEvent;
	import sfxworks.services.events.FileSharingEvent;
	/**
	 * ...
