<?php
class AscParseController
{
	public function Run ($ascFile, $jsonFile = "")
	{
		if ($jsonFile == "") {
			$jsonFile = substr($ascFile, 0, -4) . ".json";
		}
		if (!strpos($jsonFile, "\\")) {
			$jsonFile = dirname($ascFile) . "\\" . $jsonFile;
		}
		$report = $this->parseFile($ascFile);
		file_put_contents($jsonFile, json_encode($report));
	}

	public function parseFile ($fileName)
	{
		$ascFile = utf8_encode(file_get_contents($fileName));
		$layers = explode("\r\n\r\n", $ascFile);
		$meta = explode("\r\n", array_shift($layers));

		$report = array(
			'Titel' => str_replace("\"", "", $meta[0]),
			'Datum' => str_replace("\"", "", $meta[1]),
			'Filter' => explode(";", str_replace("\"", "", $meta[2])),
			'Bericht' => $this->parseLayers($layers)
		);

		return $report;
	}

	private function parseLayers ($layers)
	{
		$result = array();
		foreach ($layers as $layer) {
			$result[] = $this->parseLayer($layer);
		}
		return $result;
	}

	private function parseLayer ($layer)
	{
		$page = explode("\r\n", $layer);
		$result = array(
			'Ebene' => str_replace("\"", "", array_shift($page)),
			'Header' => $this->parseTableHeader(array_shift($page)),
			'Struktur' => $this->parseStructure($page),
			'Tabelle' => $this->parseTable($page)
		);

		return $result;
	}

	private function parseTableHeader ($header)
	{
		$title = explode(";", str_replace("\"", "", $header));
		$result = array();
		foreach ($title as $col) {
			$format = "Dezimal2";
			if (trim($col) == "") {
				$format = "Text";
			}
			if (strpos($col, "%") > 0) {
				$format = "Prozent1";
			}
			$result[] = array(
				'Titel' => $col,
				'Format' => $format
			);
		}
		return $result;
	}

	private function parseStructure ($rows)
	{
		$structure = array();
		$elements = array();
		foreach ($rows as $row) {
			if (trim($row) == "") {
				continue;
			}
			$cols = explode(";", str_replace("\"", "", $row));
			$title = array_shift($cols);
			$level = explode(".", $title);
			$structure = $this->addElement($structure, $level);

			$elements["." . $title] = $cols;
		}

		return $this->getNodes($structure, $elements);
	}

	private function addElement ($structure, $level)
	{
		if (count($level) == 0) {
			return $structure;
		}
		$title = array_shift($level);
		if (!isset($structure[$title])) {
			$structure[$title] = array();
		}
		$structure[$title] = $this->addElement($structure[$title], $level);
		return $structure;
	}

	private function getNodes ($structure, $elements, $title = "")
	{
		$result = array();
		foreach ($structure as $key => $sub) {
			$t = $title . "." . $key;
			$e = array('Ebene' => $key);
			if (count($sub) > 0) {
				$e['Struktur'] = $this->getNodes($sub, $elements, $t);
			}
			if (isset($elements[$t])) {
				$e['Zeile'] = $elements[$t];
			}
			$result[] = $e;
		}
		return $result;
	}

	private function parseTable ($rows)
	{
		$result = array();
		foreach ($rows as $row) {
			if (trim($row) == "") {
				continue;
			}
			$cols = explode(";", str_replace("\"", "", $row));
			$title = array_shift($cols);
			$level = explode(".", $title);
			$result[] = array(
				'Tiefe' => count($level),
				'Ebene' => array_pop($level),
				'Zeile' => $cols
			);
		}
		return $result;
	}
}