
package com.jfixby.scarabei.red.filesystem;

import java.nio.file.Path;
import java.util.regex.Pattern;

import com.jfixby.scarabei.api.collections.Collections;
import com.jfixby.scarabei.api.collections.List;
import com.jfixby.scarabei.api.debug.Debug;
import com.jfixby.scarabei.api.err.Err;
import com.jfixby.scarabei.api.file.File;
import com.jfixby.scarabei.api.file.FileInputStream;
import com.jfixby.scarabei.api.file.FileOutputStream;
import com.jfixby.scarabei.api.file.FileSystem;
import com.jfixby.scarabei.api.file.LocalFileSystemComponent;
import com.jfixby.scarabei.api.log.L;
import com.jfixby.scarabei.api.util.Utils;
import com.jfixby.scarabei.api.util.path.AbsolutePath;
import com.jfixby.scarabei.api.util.path.RelativePath;
import com.jfixby.scarabei.red.io.JavaFileInputStream;
import com.jfixby.scarabei.red.io.JavaFileOutputStream;

public class LocalFileSystem extends AbstractFileSystem implements LocalFileSystemComponent {

	@Override
	final public FileInputStream newFileInputStream (final File input_file) {
		Debug.checkNull("File", input_file);
		Debug.checkTrue("File belongs to this filesystem?", input_file.getFileSystem() == this);
		return new JavaFileInputStream(input_file.toJavaFile());
	}

	@Override
	final public FileOutputStream newFileOutputStream (final File output_file, final boolean append) {
		Debug.checkNull("File", output_file);
		Debug.checkTrue("File belongs to this filesystem?", output_file.getFileSystem() == this);
		return new JavaFileOutputStream(output_file.toJavaFile(), append);
	}

	private AbsolutePath<FileSystem> resolve (java.io.File file) {
		Debug.checkNull("file", file);
		file = file.getAbsoluteFile();

		final RelativePath relative = this.pathToRelative(file.toPath());
		final AbsolutePath<FileSystem> path = Utils.newAbsolutePath((FileSystem)this, relative);
		return path;
	}

	private RelativePath pathToRelative (final Path path) {
		final List<String> steps = Collections.newList();
		for (final Path p : path) {
			steps.add(p.toFile().getName());
		}
		final RelativePath relative = Utils.newRelativePath(steps);
		return relative;
	}

	@Override
	public LocalRedFile newFile (final AbsolutePath<FileSystem> file_path) {
		if (file_path == null) {
			Err.reportError("Filepath is null.");
		}
		if (file_path.getMountPoint() != this) {
			L.e("file_path", file_path);
			L.e("FileSystem", file_path.getMountPoint());
			Err.reportError("Path does not belong to this filesystem: " + this);
		}
		return new LocalRedFile(file_path, this);
	}

	@Override
	final public File newFile (final String java_file_path) {
		Debug.checkNull("java_file_path", java_file_path);

		final String splitRegex = Pattern.quote(System.getProperty("file.separator"));
		final RelativePath splittedFileName = Utils.newRelativePath(Collections.newList(java_file_path.split(splitRegex)));
// splittedFileName.steps().print("steps");

		final File file = this.ROOT().proceed(splittedFileName);
		return file;
	}

	@Override
	final public File newFile (final java.io.File file) {
		return this.newFile(this.resolve(file));
	}

	@Override
	final public File ApplicationHome () {
		return this.newFile(this.application_home_path_string);
	}

	final String application_home_path_string = System.getProperty("user.dir");
}

@@ -1715,77 +1715,425 @@
 );%0D%0A
-%0D%0A%09%09final RelativePath relative = this.pathToRelative(file.toPath());
+%09%09RelativePath relative = null;%0D%0A%09%09try %7B%0D%0A%09%09%09final Path javaPath = file.toPath();%0D%0A%09%09%09relative = this.pathToRelative(javaPath);%0D%0A%09%09%7D catch (final Throwable e) %7B// Fuck you Android!%0D%0A%09%09%09final String androidAbsolutePathString = file.getAbsolutePath();%0D%0A%09%09%09relative = Utils.newRelativePath(androidAbsolutePathString);%0D%0A%09%09%09L.e(%22Android file failed to return file.toPath(). Use abs path string instead: %22 + relative);%0D%0A%09%09%7D
 %0D%0A%09%09

fix android