package org.arkiwi.app.utility;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;

import org.arkiwi.app.interfaces.SDialogInterface;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

public class SMethod implements SDialogInterface {
	public static final String LOG_TAG = "SimpleMethod";

	
	public static Date stringToDate(String format,String stringToConvert){
		Date date = null;
		
		SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.US);
	    try {
			date = sdf.parse(stringToConvert);
		} catch (ParseException e) {
			Log.d(LOG_TAG, "Errore nel parsing della data");
			e.printStackTrace();
		}
	    
	    return date;
	}
	
	public static int nameSearch(String[] names, String name){
		for (int n = 0; n < names.length; n++) {
			if (names[n].equals(name)){
				return n;
			}
		}
		
		return -1;
	}
	
    public static String arrayListToString(ArrayList<String> array){
    	return array.toString().replace("[", "").replace("]", "");
    }
	
    public static void closeSoftKeyboard(InputMethodManager imm, View v){
    	imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }

	public static boolean appExist(android.content.pm.PackageManager mPm, String appName){
		Boolean  installed = false;
		
		PackageInfo info = null;
		try {
			info = mPm.getPackageInfo(appName, 0);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		installed = info != null;
		
		return installed;
	}
	

	public static boolean isIntentAvailable(Context context, String action, String mime) {
		final PackageManager packageManager = context.getPackageManager();
		final Intent intent = new Intent(action).setType(mime);
		List<ResolveInfo> list = packageManager.queryIntentActivities(intent,
				PackageManager.MATCH_DEFAULT_ONLY);
		//Log.d(LOG_TAG, "intent size:"+list.size()+" list:"+list.toString());
		
		return list.size() > 0;
	}
}
