package com.dubiousoft.sakana;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

import android.app.Activity;
import android.util.DisplayMetrics;
import android.util.Log;

public class Utils {

	public static void LOG( String msg ) {
		//Log.v("Sakana", msg );
	}
	
	public static void dumpScreenInfo(Activity activity) {
		DisplayMetrics metrics = activity.getResources().getDisplayMetrics();
		LOG( "DisplayMetrics:density = " + Float.toString( metrics.density ));
		LOG( "DisplayMetrics:densityDpi = " + Integer.toString( metrics.densityDpi ));
		LOG( "DisplayMetrics:scaledDensity = " + Float.toString( metrics.scaledDensity ));
		LOG( "DisplayMetrics:heightPixels = " + Integer.toString( metrics.heightPixels ));
		LOG( "DisplayMetrics:widthPixels = " + Integer.toString( metrics.widthPixels ));
		LOG( "DisplayMetrics:xdpi = " + Float.toString( metrics.xdpi ));
		LOG( "DisplayMetrics:ydpi = " + Float.toString( metrics.ydpi ));
	}
	
	public static int px2dip(Activity activity, int pixel){
		double scale = activity.getResources().getDisplayMetrics().density;
		return (int) ((double)pixel / scale);
	}
	    
	public static int dip2px( Activity activity, int dip ) {
		double scale = activity.getResources().getDisplayMetrics().density;
		return (int)(((double)dip) * scale);
	}
	    
	public static Node getChildNode( Node node, String name )
	{
		NodeList nodes = node.getChildNodes();
		for (int i=0; i<nodes.getLength(); ++i) {
			Element child = (Element)(nodes.item(i));
			if (child.getTagName().equals(name) ) {
				return child;
			}
		}
		throw new RuntimeException( "Could not find the child node specified" );
	}
	
	public static void cleanNode( Node node )
	{
		if (node.getNodeType() == Node.TEXT_NODE) {
			String text = ((Text)node).getWholeText();
			if (text.charAt(0) == '\n') {
				node.getParentNode().removeChild(node);
			}
		}
		else if (node.getNodeType() == Node.COMMENT_NODE) {
			node.getParentNode().removeChild(node);
		}
		else {
			NodeList children = node.getChildNodes();
			for (int i=0; i<children.getLength(); ++i) {
				cleanNode( children.item(i) );
			}
		}		
	}
	

}
