package com.appideas.base;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

//import android.util.Log;

/**
 * The APPideas String Transformer.
 *
 * @author		costmo
 * @version		20111213
 * @since		20111213
 */

public class AiSTr
{

	/**
	 * Class constructor
	 *
	 * @since		20111213
	 */
	public AiSTr()
	{

	}

	/**
	 * Escapes a string for database queries
	 *
	 * @since		20111213
	 * @return		String
	 * @param		input			The string to transform
	 */
	public static String dbEscape( String input )
	{
		String returnValue = new String( input );

		returnValue = returnValue.replaceAll( "[']", "''" );

		return returnValue;
	}

	/**
	 * Makes phone numbers into formatted strings
	 *
	 * TODO: This is not yet implemented
	 *
	 * @since		20111213
	 * @return		String
	 * @param		input			The string to transform
	 */
	public static String fixPhone( String input )
	{
		String returnValue = new String( input );

		return returnValue;
	}

	/**
	 * Converts a native boolean to a database value (0 or 1)
	 *
	 * @since		20111213
	 * @return		int
	 * @param		input			The input to transform
	 */
	public static int boolToDb( boolean input )
	{
		int returnValue = 0;

		if( input == true )
		{
			returnValue = 1;
		}

		return returnValue;
	}

	/**
	 * Converts a database boolean value (0 or 1) into a native boolean
	 *
	 * @since		20111213
	 * @return		boolean
	 * @param		input			The input to transform
	 */
	public static boolean dbToBool( int input )
	{
		boolean returnValue = false;

		if( input == 1 )
		{
			returnValue = true;
		}

		return returnValue;
	}

	/**
	 * If the input is null, returns an empty string, returns the string otherwise
	 *
	 * @since		20111213
	 * @return		String
	 * @param		input			The string to transform
	 */
	public static String denullifyString( String input )
	{
		if( input == null )
		{
			return "";
		}
		else
		{
			return input;
		}
	}

	/**
	 * If the input is null, returns 0, returns int.<p/>
	 *
	 * This accepts only string numbers to be easier to use with the database.
	 *
	 * @since		20111213
	 * @return		Integer
	 * @param		input			The string to transform
	 */
	public static int denullifyInt( String input )
	{
		if( input == null || input.length() < 1 || !AiSTr.isNumeric( input ) )
		{
			return 0;
		}
		else
		{
			return Integer.valueOf( input ).intValue();
		}
	}

	/**
	 * If the input string is null or NaN, returns 0 (int), otherwise, returns the number as an int<p/>
	 *
	 * This accepts only string numbers to be easier to use with the database.
	 *
	 * @since		20111213
	 * @return		Integer
	 * @param		input			The string to transform
	 */
	public static int stringToInt( String input )
	{
		if( input == null || input.length() < 1 || !AiSTr.isNumeric( input ) )
		{
			return 0;
		}
		else
		{
			return Integer.valueOf( input ).intValue();
		}
	}

	/**
	 * Checks to see if the string input is a number
	 *
	 * @since		20111213
	 * @return		boolean
	 * @param		input			The string to transform
	 */
	public static boolean isNumeric( String input )
	{
		try
		{
			Integer.parseInt( input );
			return true;
		}
		catch( Exception e )
		{
			return false;
		}
	}

	/**
	 * Get an MD5 hash of a string
	 *
	 * @since		20111213
	 * @return		String
	 * @param		input			The string to transform
	 */
	public static String md5Hash( String input )
	{
		MessageDigest m = null;

		try
		{
			m = MessageDigest.getInstance( "MD5" );
		}
		catch( NoSuchAlgorithmException e )
		{
			e.printStackTrace();
		}

		m.update( input.getBytes(), 0, input.length() );
		String hash = new BigInteger( 1, m.digest() ).toString( 16 );
		return hash;
	}

	public static String stripImagesHTML(String original)
	{
		return original.replaceAll( "<img.*>","" );
	}

	public static String getEncodedData(Map<String,String> data) {
		StringBuilder sb = new StringBuilder();
		for(String key : data.keySet()) {
			String value = null;
			try {
				value = URLEncoder.encode(data.get(key),"UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}

			if(sb.length()>0)
				sb.append("&");

			sb.append(key + "=" + value);
		}
		return sb.toString();
	}

}
