<?php
require('DB.class.php');
$db = DB::connect();

function GetItemID($itemName, $db)
{
	$itemName = CorrectItem($itemName);
	$query = DB::query('SELECT `typeID` FROM `invTypes` WHERE `typeName` LIKE "'. $itemName.'"', $db);
	$result = mysqli_fetch_assoc($query);
	$return = $result['typeID'];
	//mysql_free_result($query);
	if(strlen($return) == 0)
	{
		return 0;
	}
	return($return);
}

function CorrectItem($itemName)
{
	$ret = $itemName;
	
	if (strpos($itemName, 'Omni-Directional') !== FALSE)
	{
		$ret = str_replace('Omni-Directional','Omnidirectional',$ret);
	}
	
	return ($ret);
}

function GetFitsParsed()
{
	$ret = 0;
	if(file_exists('fitsparsed.count'))
	{
		$ret = file_get_contents('fitsparsed.count');
	}
	return ($ret);
}

function IncrementFitsParsed()
{
	$i = GetFitsParsed();
	$i = $i + 1;
	file_put_contents('fitsparsed.count', $i);
}

function startsWith($haystack, $needle)
{
    return $needle === "" || strpos($haystack, $needle) === 0;
}
function endsWith($haystack, $needle)
{
    return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
}

function addToArray($itemid, $array, $amount=1)
{
	if(isset($array[$itemid]))
	{
		$array[$itemid] = $array[$itemid] + $amount;
	}
	else
	{
		$array[$itemid] = $amount;
	}
	return ($array);
}
$shipid = 0;
$modulearr = array();
$unknownModules = array();
if(isset($_POST['fit']))
{
	if(isset($_POST['contract']))
	{
		$array = explode(PHP_EOL, $_POST['fit']);
		$i = 0;
		foreach($array as $k=>$v)
		{
			$split = preg_split("/[\t]/", $v); 
			if(isset($split[3]) && $split[3] == 'Ship')
			{
				$shipid = GetItemID(trim($split[0]), $db);
			}
			if(isset($split[3]) && $split[3] == 'Drone')
			{
				$droneid = GetItemID(trim($split[0]), $db);
				$amount = $split[1];
				$modulearr = addToArray($droneid, $modulearr, $amount);
			}
			if(isset($split[3]) && $split[3] == 'Module')
			{
				switch($_POST['contract'])
				{
					case 'guessfit':
						$modid = GetItemID(trim($split[0]), $db);
						$amount = $split[1];
						$modulearr = addToArray($modid, $modulearr, $amount);
						break;
					case 'showfitted':
						if(isset($split[4]) && trim($split[4]) == 'Fitted')
						{
							$modid = GetItemID(trim($split[0]), $db);
							$amount = $split[1];
							$modulearr = addToArray($modid, $modulearr, $amount);
						}
						break;
				}
			}
			if(isset($split[3]) && $split[3] == 'Charge')
			{
				$modid = GetItemID(trim($split[0]), $db);
				$amount = $split[1];
				$modulearr = addToArray($modid, $modulearr, $amount);
			}
			if(isset($split[3]) && $split[3] == 'Subsystem')
			{
				$modid = GetItemID(trim($split[0]), $db);
				$amount = $split[1];
				$modulearr = addToArray($modid, $modulearr, $amount);
			}
		}
	}
	else
	{
		$array = explode(PHP_EOL, $_POST['fit']);
		$i = 0;
		foreach($array as $k=>$v)
		{
			$line = trim($v);
			if(strlen($line) == 0)
			{
				continue;
			}
			if($i == 0)
			{
				$stripped = str_replace('[','', $line);
				$stripped = str_replace(']','', $stripped);
				$strarr = explode(',', $stripped);
				$shipid = GetItemID(trim($strarr[0]), $db);
			}
			else
			{		
				if(startsWith($line, '['))
				{
					
				}
				else
				{
					$stripped = str_replace('[','', $line);
					$stripped = str_replace(']','', $stripped);
					$strarr = explode(',', $stripped);
					$modid = GetItemID(trim($strarr[0]), $db);
					$estrarr = explode(' x', $stripped);
					if($modid == 0 && !isset($estrarr[1]))
					{
						if(!isset($estrarr[1]))
						{
							$unknownModules = addToArray(trim($strarr[0]), $unknownModules);
						}
						continue;
					}
					
					if(isset($estrarr[1]))
					{
						$modid = GetItemID(trim($estrarr[0]), $db);
						$amount = 1;
						if(isset($estrarr[1]))
						{
							$amount = $estrarr[1];
						}
						$modulearr = addToArray($modid, $modulearr, $amount);
					}
					else
					{
						$modulearr = addToArray($modid, $modulearr);
					}
					if(isset($strarr[1]))
					{
						$ammoid = GetItemID(trim($strarr[1]), $db);
						$modulearr = addToArray($ammoid, $modulearr);
					}
				}
			}
			$i++;
		}
	}
}

$GETed = 0;
$fitString = ':';
if($shipid > 0)
{
	$mods = '';
	foreach($modulearr as $mod=>$num)
	{
		if($num > 0)
		{
			$mods = $mods . $mod.';'.$num.':';
		}
	}
	$fitString = $shipid .':'. $mods . $fitString;
	$GETed = 1;
	//echo $fitString;
}

?>
<html>
	<head>
		<title>
			EVE EFT fit and fitted ship contract parser.
		</title>
		<link rel="stylesheet" type="text/css" href="../participation_tool/style/style.css">
		<script language="JavaScript">
				<?php
			if($GETed == 1)
			{
				echo "function ShowFit()
				{
					CCPEVE.showFitting('".$fitString."');
				}";
			}
			?>
			function madeByLink()
			{
				CCPEVE.showInfo(1377, 92351003);
			}
		</script>
	</head>
	<body>
		<center>
			<table width="100%">
				<tr>
					<td align="center">
						Instructions and fitting link:
					</td>
					<td>
						Paste your EFT compatible fit or contract lines here:
					</td>
					<?php
						if(count($unknownModules) > 0)
						{
							echo '<td>Unknown/erroneous modules:</td>';
						}
						else
						{
							if($GETed == 1)
							{
								IncrementFitsParsed();
							}
						}
					?>
				</tr>
				<tr>
					<td valign="top">
						<?php
							if($GETed == 1)
							{
								echo '<center><b>Result fit:</b> <br />-><a href="#" onclick="JavaScript:ShowFit()">Show Fit</a><-</center><br /><br />';
							}
							else
							{
								echo '<br /><br />';
							}
						?>
						<p>
							<h3>Instructions:</h3><br />
							<b>EFT Fits:</b><br />
							You can generate in game fits from copy pasting EFT compatible fits or copy pasting contract contents.<br />
							EFT compatible fits can be from the web or using the copy to clipboard option in EFT.<br /><br />
							<b>Contracts:</b><br />
							Generating fits from contracts is done by selecting all (relevant) lines from a contract and copy pasting it in the box.<br />
							To generate a fit from a contract you have to select one of the radio buttons under the box so it knows its a contract.<br />
							Guess Fit: This option constructs a fit based on all the mods in a contract. It guesses what the fit could be.(not always accurate)<br />
							Only show fitted: This option makes a fit based on fitted modules in a contract.(very accurate for fitted ship contracts)<br /><br />
							<b>Notes:</b><br /> Old EFT fits with modules whose names have changed will show a box with the unknown modules.<br />
							Loaded charges in an EFT fit will be shows in multiples of 1 for each mod that had it preloaded in the EFT fit.<br />
							For fitted ship contracts with multiple ships it will be inaccurate. 
						</p>
					</td>
					<td valign="top">
						<form action="index.php" method="post">
							<textarea name="fit" cols="50" rows="30"><?php if(isset($_POST['fit'])) { echo $_POST['fit'];}?></textarea><br />
							Contract: <br />
							<input type="radio" name="contract" value="guessfit" title="Click this for if you want it to guess the fit based on the contract contents" />Guess Fit 
							<input type="radio" name="contract" value="showfitted" title="Click this for if you want it to only show the fit based on fitted modules on the contract" />Only show fitted <br />
							<input type="submit" value="Show Fit" />
						</form>
					</td>
					<?php
						if(count($unknownModules) > 0)
						{
							echo '<td valign="top"><textarea cols="40" rows="30" disabled>';
							foreach($unknownModules as $k=>$v)
							{
								echo $k.' x'.$v.PHP_EOL;
							}
							echo '</textarea></td>';
						}
					?>
				</tr>
			</table>
		
			<br />
			Made by: <a href="#" onclick="JavaScript:madeByLink()">Krastin Kernatastere</a>
			<br /> if you love it you are welcome to send me some isk ;)
			<br />
			<br />Sat, 13 Sep 14 13:13:51 +0000
			<br />
			<?php echo GetFitsParsed(); ?>
		</center>
	</body>
</html>