/**
 *
 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either 
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 * 
 **/
package lucee.runtime.functions.image;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import lucee.runtime.PageContext;
import lucee.runtime.exp.PageException;
import lucee.runtime.img.Image;
import lucee.runtime.type.Struct;
import lucee.runtime.type.StructImpl;

public class ImageGetEXIFMetadata {

	public static Struct call(PageContext pc, Object name) throws PageException {
		//if(name instanceof String) name=pc.getVariable(Caster.toString(name));
		Image img = Image.toImage(pc,name);
		return img.info();//getData(img);
	}

	public static Struct flatten(Struct sct) throws PageException {
		
		Struct data=new StructImpl();
		Iterator it = sct.entrySet().iterator();
		Map.Entry entry;
		while(it.hasNext()){
			entry=(Entry) it.next();
			if(entry.getValue() instanceof Map) 
				fill(data,(Map)entry.getValue());
			else if(entry.getValue() instanceof List) 
				fill(data,entry.getKey(),(List)entry.getValue());
			else
				data.put(entry.getKey(),entry.getValue());
		}
		
		return data;
	}

	private static void fill(Struct data, Map map) throws PageException {
		Iterator it = map.entrySet().iterator();
		Map.Entry entry;
		while(it.hasNext()){
			entry=(Entry) it.next();
			if(entry.getValue() instanceof Map) 
				fill(data,(Map)entry.getValue());
			else if(entry.getValue() instanceof List) 
				fill(data,entry.getKey(),(List)entry.getValue());
			else
				data.put(entry.getKey(),entry.getValue());
		}
	}

	private static void fill(Struct data, Object key, List list) throws PageException {
		data.put(
				key,
				lucee.runtime.type.util.ListUtil.listToList(list, ","));
	}
}
@@ -893,16 +893,54 @@
 Entry;%0A%0A
+import lucee.commons.lang.StringUtil;%0A
 import l
@@ -1798,32 +1798,39 @@
 (entry.getKey(),
+unwrap(
 entry.getValue()
@@ -1826,24 +1826,25 @@
 .getValue())
+)
 ;%0A%09%09%7D%0A%09%09%0A%09%09r
@@ -1855,24 +1855,170 @@
 n data;%0A%09%7D%0A%0A
+%09private static Object unwrap(Object value) %7B%0A%09%09if(value instanceof CharSequence) return StringUtil.unwrap(value.toString());%0A%09%09return value;%0A%09%7D%0A%0A
 %09private sta
@@ -2540,16 +2540,23 @@
 ey,%0A%09%09%09%09
+unwrap(
 lucee.ru
@@ -2602,14 +2602,15 @@
 t, %22,%22))
+)
 ;%0A%09%7D%0A%7D

test newer postgre driver