package com.jfixby.psd.unpacker.run;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import com.jfixby.cmns.api.file.ChildrenList;
import com.jfixby.cmns.api.file.File;
import com.jfixby.cmns.api.file.FileFilter;
import com.jfixby.cmns.api.file.FileOutputStream;
import com.jfixby.cmns.api.file.LocalFileSystem;
import com.jfixby.cmns.api.log.L;
import com.jfixby.cmns.api.sys.Sys;
import com.jfixby.psd.unpacker.api.PSDFileContent;
import com.jfixby.psd.unpacker.api.PSDLayer;
import com.jfixby.psd.unpacker.api.PSDRaster;
import com.jfixby.psd.unpacker.api.PSDRootLayer;
import com.jfixby.psd.unpacker.api.PSDUnpacker;
import com.jfixby.psd.unpacker.api.PSDUnpackingParameters;
import com.jfixby.red.desktop.DesktopAssembler;

public class UnpackAll {

	private static final FileFilter filter = new FileFilter() {

		@Override
		public boolean fits(File child) {
			return child.getName().toLowerCase().endsWith(".psd");
		}
	};

	public static void main(String[] args) throws IOException {

		DesktopAssembler.setup();
		File home = LocalFileSystem.ApplicationHome();

		File input_folder = home.child("unpacker-input");
		if (!input_folder.exists()) {
			L.d("Folder not found", input_folder);
			Sys.exit();
		}
		if (!input_folder.isFolder()) {
			L.d("Folder not found", input_folder);
			Sys.exit();
		}

		ChildrenList input_files = input_folder.listChildren();
		input_files = input_files.filter(filter);

		File output_folder = home.child("unpacker-output");
		output_folder.makeFolder();

		for (int i = 0; i < input_files.size(); i++) {
			File psd_file_i = input_files.getElementAt(i);
			unpack(psd_file_i, output_folder);
		}

	}

	private static void unpack(File psd_file_i, File output_folder)
			throws IOException {

		PSDUnpackingParameters specs = PSDUnpacker.newUnpackingSpecs();
		specs.setPSDFile(psd_file_i);

		String psd_name = psd_file_i.nameWithoutExtension();

		PSDFileContent result = PSDUnpacker.unpack(specs);

		result.print();

		PSDRootLayer root = result.getRootlayer();

		for (int i = 0; i < root.numberOfChildren(); i++) {
			PSDLayer child = root.getChild(i);
			process_child(psd_name, child, output_folder);
		}
	}

	private static void process_folder(String psd_name, PSDLayer root,
			File output_path) throws IOException {
		for (int i = 0; i < root.numberOfChildren(); i++) {
			PSDLayer child = root.getChild(i);
			process_child(psd_name, child, output_path);
		}
	}

	private static void process_child(String psd_name, PSDLayer child,
			File output_path) throws IOException {
		if (child.isVisible()) {
			if (child.isFolder()) {
				process_folder(psd_name, child, output_path);
			} else {
				PSDRaster raster = child.getRaster();
				BufferedImage java_image = raster.getBufferedImage();
				String raster_name = child.getName();
				File output_file = output_path.child(psd_name + "."
						+ raster_name + ".png");

				L.d("writing", output_file);
				FileOutputStream os = output_file.newOutputStream();
				OutputStream java_stream = os.toJavaOutputStream();
				ImageIO.write(java_image, "png", java_stream);
				java_stream.close();
			}
		}
	}
}

@@ -1,12 +1,13 @@
+%0A
 package com.
@@ -923,16 +923,17 @@
 ean fits
+ 
 (File ch
@@ -1030,16 +1030,17 @@
 oid main
+ 
 (String%5B
@@ -1480,16 +1480,21 @@
 s.filter
+Files
 (filter)
@@ -1754,16 +1754,17 @@
 d unpack
+ 
 (File ps
@@ -1792,20 +1792,17 @@
 _folder)
-%0A%09%09%09
+ 
 throws I
@@ -2274,16 +2274,17 @@
 s_folder
+ 
 (String 
@@ -2307,20 +2307,17 @@
 er root,
-%0A%09%09%09
+ 
 File out
@@ -2529,16 +2529,17 @@
 ss_child
+ 
 (String 
@@ -2563,20 +2563,17 @@
 r child,
-%0A%09%09%09
+ 
 File out
@@ -2920,15 +2920,9 @@
  %22.%22
-%0A%09%09%09%09%09%09
+ 
 + ra

fix files filtering