package com.sendsms.utills;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.sendsms.R;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * this class having all the common function over all in the application.
 */

public class Util {

	private static ProgressDialog progressDialog = null;
	private static Dialog dialog=null;

	// will check whehter device is connected to any internet connection or not.
	public static boolean isDeviceOnline(Context context) {
		try {
			ConnectivityManager cm = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			return cm.getActiveNetworkInfo().isConnectedOrConnecting();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	// will check whehter device is connected to any internet connection or not.
	public static boolean isNetworkAvailable(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = cm.getActiveNetworkInfo();
		if (networkInfo != null && networkInfo.isConnected()) {
			return true;
		}
		return false;
	}

	public static String getMacAddress(Context context) {
//		WifiManager manager = (WifiManager) context
//				.getSystemService(Context.WIFI_SERVICE);
//		WifiInfo info = manager.getConnectionInfo();
		String 	deviceId = null;
		try {
			TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
			deviceId = telephonyManager.getDeviceId();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
//		TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
//		  telephonyManager.getDeviceId();
		
		  
		return deviceId;
	}

	public static void customDialog(Context ctx, String Message) {

		try {

			// dialog.getWindow().setTitle()
			if (!(Util.isEmptyString(Message))) {
				AlertDialog dialog;
				Builder builder = new Builder(ctx);
				builder.setMessage(Message);
				builder.setCancelable(true);
				builder.setPositiveButton("OK", new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						try {
							if (dialog != null)
								dialog.dismiss();

						} catch (Exception e) {
							// TODO: handle exception
						}

					}

				});
				dialog = builder.create();
				dialog.show();
			}

		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public static boolean isEmptyString(String text) {
		return (text == null || text.trim().equals("null") || text.trim()
				.length() <= 0);
	}

	// to show the alert dialog.
	public static void showAlertDialog(Context context, OnClickListener retry,
			OnClickListener quit) {
		Builder builder1 = new Builder(context);
		builder1.setTitle("error!!");
		builder1.setMessage("Internet is not working!!");
		builder1.setCancelable(true);
		builder1.setPositiveButton("Retry", retry);
		builder1.setNegativeButton("Quit", quit);
		AlertDialog alert11 = builder1.create();
		alert11.show();
	}

	// to show the progress dialog
	public static void showProDialog(Context context) {
		/*progressDialog = new ProgressDialog(context);
		progressDialog.setCancelable(false);
		progressDialog.setMessage("Wait...");
		progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progressDialog.setProgress(0);
		progressDialog.show();*/

		dialog = new Dialog(context, android.R.style.Theme_Translucent);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.custom_progress_dialog);
		dialog.setCancelable(false);
		dialog.show();
	}

	// dismiss the progress dialog
	public static void dismissDialog() {
		/*if (progressDialog != null) {
			progressDialog.dismiss();
		}*/

		if(dialog!=null)
		{
			dialog.dismiss();
		}
	}

	// to show a center toast.
	public static void showCenterToast(Context context, String msg) {
		Toast toast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();
	}

	// hide soft key board
	public static void hideSoftKeyboard(Context ctx, EditText view) {
		InputMethodManager imm = (InputMethodManager) ctx
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(view.getWindowToken(),
				InputMethodManager.HIDE_NOT_ALWAYS);
	}

	// to start any activity.
	public static void startActivity(Context context, Class<?> class1) {
		Intent intent = new Intent();
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		intent.setClass(context, class1);
		((Activity) context).startActivity(intent);
		((Activity) context).finish();
	}

	// parse the response coming from server using gson library.
	// public static Object getJsonToClassObject(String jsonStr,Class<?>
	// classs){
	// try{
	// Gson gson = new GsonBuilder().create();
	// return (Object)gson.fromJson(jsonStr, classs);
	// }catch(Exception e){
	// e.printStackTrace();
	// return null;
	// }
	// }

	// this is to set height of listview to scroll inside scroll view.
	public static void setListViewHeightBasedOnChildren(ListView listView) {
		if (listView.getAdapter() != null) {
			int totalHeight = 0;
			for (int i = 0; i < listView.getAdapter().getCount(); i++) {
				View listItem = listView.getAdapter()
						.getView(i, null, listView);
				listItem.measure(0, 0);
				totalHeight += listItem.getMeasuredHeight();
			}
			ViewGroup.LayoutParams params = listView.getLayoutParams();
			params.height = totalHeight
					+ (listView.getDividerHeight() * (listView.getAdapter()
							.getCount() - 1));
			listView.setLayoutParams(params);
		} else {
			return;
		}
	}

	// converting string into date object.
	public static Date convertStringTODate(String dt) {
		Date date = null;
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			date = format.parse(dt);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	public static String formateNumber(double value){
		DecimalFormat nf = new DecimalFormat("##,##,##,##0.00");
	    String formatted = nf.format(value);
		return formatted;
	}
}
