class IconListPresenter extends \App\Presenters\BasePresenter
{

	/**
	 * @var Context
	 * @inject
	 */
	public $connection;

	public function actionDefault()
	{

		$icons = scandir(INDEX_DIR . '/../webapp/frontapp/svg');
		$list = [];
		foreach ($icons as $icon) {
			if (!strpos($icon, 'svg')) {
				continue;
			}

			$list[] = $icon;
		}


		$this->template->occurrences = $this->findOccurrences($list);
		$this->template->list = $list;

		
		$scan = array_diff(scandir(INDEX_DIR.'/../app/ServiceModule/templates/IconList/icons-list/'), ['.', '..']);
		$urls = [];
		foreach ($scan as $iconName) {
			if (is_file($file = INDEX_DIR.'/../app/ServiceModule/templates/IconList/icons-list/' . $iconName)) {
				$urls[str_replace('.txt', '', $iconName)] = file_get_contents($file);
			}
		}

		$this->template->urls = $urls;
	}

	public function actionUpdateIcons()
	{

		$icons = scandir(INDEX_DIR . '/../webapp/frontapp/svg');
		$list = [];
		foreach ($icons as $icon) {
			if (!strpos($icon, 'svg')) {
				continue;
			}

			$list[] = $icon;
		}
		
		$scan = array_diff(scandir(INDEX_DIR.'/../app/ServiceModule/templates/IconList/icons-list/'), ['.', '..']);
		$urls = [];
		foreach ($scan as $iconName) {
			if (is_file($file = INDEX_DIR.'/../app/ServiceModule/templates/IconList/icons-list/' . $iconName)) {
				$urls[str_replace('.txt', '', $iconName)] = file_get_contents($file);
			}
		}

		foreach ($list as $icon) {
			if (isset($urls[$icon])) {
				$url = $urls[$icon];
				$url = str_replace('48px.svg', '20px.svg', $url);

				$fetchIcon = file_get_contents($url);
				$fetchIcon = str_replace('><path', ' viewBox="0 0 20 20"><path fill="currentColor" ', $fetchIcon);
				file_put_contents(INDEX_DIR.'/../webapp/frontapp/svg/' . $icon, $fetchIcon);

				// save what we saved
				//file_put_contents(INDEX_DIR.'/../app/ServiceModule/templates/IconList/icons-list/' . $icon . '.txt', $url);
			}
		}

		$this->terminate();
	}


	private function createListFiles()
	{
		$allFiles = [];

		foreach (Finder::findFiles('*.latte')->from(INDEX_DIR . '/../app/BrandCloudModule/') as $key => $splFile) {
			$fileResouce = file_get_contents($key);
			$allFiles[$key] = $fileResouce;
		}

		foreach (Finder::findFiles('*.vue')->from(INDEX_DIR . '/../webapp/frontapp/components/') as $key => $splFile) {
			$fileResouce = file_get_contents($key);
			$allFiles[$key] = $fileResouce;
		}

		return $allFiles;
	}

	private function findOccurrences($icons)
	{
		$allFiles = $this->createListFiles();
		$all = [];

		foreach ($allFiles as $filePath => $fileResource) {
			foreach ($icons as $icon) {
				$icon = str_replace('.svg', '', $icon);
				//$nicePath = explode('..', $filePath)[1];
				$nicePath = $filePath;

				preg_match_all('~<aw-icon.+icon=("|\')' . preg_quote($icon) . '("|\')~', $fileResource, $m1);
				preg_match_all('~("|\')' . preg_quote($icon) . '("|\')~', $fileResource, $m2);

				if (!isset($all[$icon][$nicePath])) {
					$all[$icon][$nicePath] = 0;
				}

				if (count($m1[0])) {
					$all[$icon][$nicePath] += count($m1[0]);
				}

				if (count($m2[0])) {
					$all[$icon][$nicePath] += count($m2[0]);
				}
			}
		}

		return $all;
	}


	public function actionSaveUrlIcon()
	{
		// RAW POST DATA
		$data = json_decode(file_get_contents("php://input"));

		// icon fetch & save
		$url = str_replace('48px.svg', '20px.svg', $data->url);
		$fetchIcon = file_get_contents($url);
		$fetchIcon = str_replace('><path', ' viewBox="0 0 20 20"><path fill="currentColor" ', $fetchIcon);
		file_put_contents(INDEX_DIR.'/../webapp/frontapp/svg/' . $data->icon, $fetchIcon);

		// save what we saved
		file_put_contents(INDEX_DIR.'/../app/ServiceModule/templates/IconList/icons-list/'.$data->icon . '.txt', $data->url);

		// data
		echo json_encode($data);

		$this->terminate();
	}
}