/*
 * Copyright 2013 Alex Lin.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.opoo.press.source.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.opoo.press.source.Source;
import org.opoo.press.source.SourceEntry;
import org.opoo.press.source.SourceManager;
import org.yaml.snakeyaml.Yaml;

/**
 * @author Alex Lin
 *
 */
public class SourceManagerImpl implements SourceManager {
	private Yaml yaml;// = new Yaml();

	public void setYaml(Yaml yaml) {
		this.yaml = yaml;
	}

	/* (non-Javadoc)
	 * @see org.opoo.press.source.SourceManager#saveSourceToFile(org.opoo.press.source.Source)
	 */
	@Override
	public void saveSourceToFile(Source source) {
		File file = source.getSourceEntry().getFile();
		File dir = file.getParentFile();
		if(!dir.exists()){
			dir.mkdirs();
		}else{
			if(!dir.isDirectory()){
				throw new RuntimeException("Can not save file, '" + dir + "' is not a valid directory.");
			}
		}
		List<String> lines = new ArrayList<String>();
		lines.add(Source.TRIPLE_DASHED_LINE);
		//for(Map.Entry<String, Object> en: source.getMeta().entrySet()){
		//	
		//}
		String line = yaml.dumpAsMap(source.getMeta());
		lines.add(line);
		lines.add(Source.TRIPLE_DASHED_LINE);
		
		lines.add(source.getContent());

		try {
			FileUtils.writeLines(file, "UTF-8", lines);
		} catch (IOException e) {
			throw new RuntimeException("Write file exception", e);
		}
	}

	/* (non-Javadoc)
	 * @see org.opoo.press.source.SourceManager#buildEntry(java.io.File, java.lang.String)
	 */
	@Override
	public SourceEntry buildEntry(File sourceDir, String path) {
		path = FilenameUtils.separatorsToUnix(path);
		String[] arr = StringUtils.split(path, "/");
		
		SourceEntry entry = null;
		int i = 0;
		for(String s: arr){
			System.out.println("[" + (i++) + "]: " + s);
			sourceDir = new File(sourceDir, s);
			entry = new SourceEntry(entry, sourceDir);
		}
		
		System.out.println(entry.getPath());
		System.out.println(entry.getName());
		System.out.println(entry.getFile().getAbsolutePath());
		System.out.println(entry.getParent().getFile().getAbsolutePath());
		return entry;
	}

	/* (non-Javadoc)
	 * @see org.opoo.press.source.SourceManager#buildSource(java.io.File, java.lang.String, java.util.Map, java.lang.String)
	 */
	@Override
	public Source buildSource(File sourceDir, String path, Map<String, Object> meta, String content) {
		SourceEntry entry = buildEntry(sourceDir, path);
		return new SimpleSource(entry, meta, content);
	}
	
	public static void main(String[] args){
		SourceEntry entry = new SourceManagerImpl().buildEntry(new File("."), "article/2013/07/2013-07-31-hello-world.markdown");
		System.out.println(entry);
	
		Yaml yaml = new Yaml();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("abcd", "aaaaaaaaaa");
		map.put("published", true);
		map.put("date", new Date());
		map.put("tags", new String[]{"tag1", "Tag2"});
		String string = yaml.dumpAsMap(map);
		System.out.println("[" + string + "]");
	}
}

@@ -705,57 +705,8 @@
 st;%0A
-import java.util.Date;%0Aimport java.util.HashMap;%0A
 impo
@@ -2426,21 +2426,8 @@
 ll;%0A
-%09%09int i = 0;%0A
 %09%09fo
@@ -2448,56 +2448,8 @@
 r)%7B%0A
-%09%09%09System.out.println(%22%5B%22 + (i++) + %22%5D: %22 + s);%0A
 %09%09%09s
@@ -2540,212 +2540,8 @@
 %0A%09%09%0A
-%09%09System.out.println(entry.getPath());%0A%09%09System.out.println(entry.getName());%0A%09%09System.out.println(entry.getFile().getAbsolutePath());%0A%09%09System.out.println(entry.getParent().getFile().getAbsolutePath());%0A
 %09%09re
@@ -2863,16 +2863,16 @@
  path);%0A
+
 %09%09return
@@ -2919,514 +2919,6 @@
 %0A%09%7D%0A
-%09%0A%09public static void main(String%5B%5D args)%7B%0A%09%09SourceEntry entry = new SourceManagerImpl().buildEntry(new File(%22.%22), %22article/2013/07/2013-07-31-hello-world.markdown%22);%0A%09%09System.out.println(entry);%0A%09%0A%09%09Yaml yaml = new Yaml();%0A%09%09Map%3CString,Object%3E map = new HashMap%3CString,Object%3E();%0A%09%09map.put(%22abcd%22, %22aaaaaaaaaa%22);%0A%09%09map.put(%22published%22, true);%0A%09%09map.put(%22date%22, new Date());%0A%09%09map.put(%22tags%22, new String%5B%5D%7B%22tag1%22, %22Tag2%22%7D);%0A%09%09String string = yaml.dumpAsMap(map);%0A%09%09System.out.println(%22%5B%22 + string + %22%5D%22);%0A%09%7D%0A
 %7D%0A

Remove debug output in java source code.