package hh.dejsem.hack;

import android.support.v4.provider.DocumentFile;

import java.io.File;

import hh.dejsem.K;
import hh.dejsem.fm.FragFM;
import hh.dejsem.fm.FSLocal;
import hh.lib.D;

public class HackFS {
	D d;
	FSLocal fs;

	public HackFS(D d) {
		this.d = d.klon(this);
		fs = new FSLocal(d);
	}

	/**
	 * Zkouška chování caseless filesystému při kolizi jmen v situacích vytváření file a directory
	 * při použití alternativně DocumentFile a File.
	 */
	public void caselessFS() {
		String cwd = "/storage/extSdCard/HH/odpad";
		d.l(4, String.format("caselessFS() on SD card, cwd path=%s, SD card root path=%s", cwd, K.SD_CARD_MOUNT_POINT));
		try {
			fs.doCreateDir(cwd);
			operateOnDocFiles(fs.getSdCardDocFile(cwd), cwd);
		}
		catch(Exception e) {
			d.l(String.format("working directory %s not found or isn't a directory", cwd));
		}

		cwd = "/storage/emulated/0/_HH/odpad";
		d.l(4, String.format("caselessFS() on doc files in builtin storage, cwd path=%s", cwd));
		if(new File(cwd).mkdirs() || new File(cwd).isDirectory())
			operateOnDocFiles(DocumentFile.fromFile(new File(cwd)), cwd);
			//operateOnFiles(cwd);
		else d.l(String.format("working directory %s not found or isn't a directory", cwd));
	}

	void operateOnDocFiles (DocumentFile parentDf, String parentPath) { // parentPath je absolutní
		//DocumentFile parentDf = DocumentFile.fromFile(cwdF);
		for(String fn : new String[]{"hH", "Hh", "hh", "HH"}) {
			File f = new File(parentPath, fn);
			d.l(String.format("path %s exists=%b, is directory=%b, found=%b", fn, f.exists(), f.isDirectory(), parentDf.findFile(fn) != null));
			createDocfDir(parentDf, fn);
			createDocfFile(parentDf, fn);
		}
		FragFM.refreshNotify(true);
	}

	void createDocfFile(DocumentFile parentDf, String fn) {  // fn bez cesty
		DocumentFile df = parentDf.findFile(fn);
		if(df != null && df.isFile()) d.l(String.format("file %s is ready", fn));
		else if(df == null) {
			df = parentDf.createFile("application/octet-stream", fn);
			if(df != null) {
				if(df.getName().equals(fn)) d.l(String.format("file %s created", fn));
				else {
					df.delete();
					d.l(String.format("file %s can't be created, name already used", fn));
				}
			}
			else d.l(String.format("file %s not created for unknown reason", fn));
		}
		else d.l(String.format("file  %s can't be created, name already in use", fn));
	}

	void createDocfDir(DocumentFile parentDf, String fn) {  // fn bez cesty
		DocumentFile df = parentDf.findFile(fn);
		if(df != null && df.isDirectory()) d.l(String.format("dir  %s is ready", fn));
		else if(df == null) {
			df = parentDf.createDirectory(fn);
			if(df != null) {
				if(df.getName().equals(fn)) d.l(String.format("dir  %s created", fn));
				else {
					df.delete();
					d.l(String.format("dir  %s can't be created, name already in use", fn));
				}
			}
			else d.l(String.format("dir  %s not created for unknown reason", fn));
		}
		else d.l(String.format("dir  %s can't be created, name already in use", fn));
	}

	void operateOnFiles (String parentPath) {
		for(String fn : new String[]{"hh", "hH", "Hh", "HH"}) {
			File f = new File(parentPath, fn);
			d.l(String.format("path %s found=%b, is directory=%b", fn, f.exists(), f.isDirectory()));
			try {
				if(f.createNewFile()) d.l(String.format("file %s created", fn));
				else d.l(String.format("file %s can't be created, name already used", fn));
			} catch(Exception e) {
				d.lmwa(e.getMessage());
			}

			if(f.mkdir()) d.l(String.format("dir  %s created", fn));
			else d.l(String.format("dir  %s can't be created, name already used", fn));
		}
	}
}
