/*
 * Copyright 2017 Midhun Harikumar
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ae.apps.randomcontact.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.format.DateFormat;
import android.text.format.DateUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import static android.net.Uri.withAppendedPath;

/**
 * Temporary utility methods that should be moved to libAeApps
 */
public class Utils {

    /**
     * Shows this contact in the Android's Contact Manager
     *
     * @param contactId contactId
     */
    public static void showContactInAddressBook(@NonNull Context context, @Nullable String contactId) {
        if (null != contactId) {
            Intent intent = new Intent(Intent.ACTION_VIEW);

            Uri uri = withAppendedPath(ContactsContract.Contacts.CONTENT_URI, contactId);
            intent.setData(uri);

            context.startActivity(intent);
        }
    }

    public static String friendlyDateFormat(final Context context, final String formattedTimeString){
        String friendlyDateString = formattedTimeString;
        if (formattedTimeString != null && formattedTimeString.trim().length() > 0) {
            // Un format and convert to a date object
            SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy hh:mm a", Locale.getDefault());
            friendlyDateString = DateUtils.getRelativeDateTimeString(context,
                    dateFormat.getCalendar().getTimeInMillis(),
                    DateUtils.MINUTE_IN_MILLIS,
                    DateUtils.WEEK_IN_MILLIS,
                    0)
                        .toString();
        }

        return friendlyDateString;
    }
}

@@ -921,24 +921,57 @@
 DateUtils;%0A%0A
+import java.text.ParseException;%0A
 import java.
@@ -989,24 +989,24 @@
 DateFormat;%0A
-
 import java.
@@ -1188,16 +1188,95 @@
 tils %7B%0A%0A
+    private static final String DEFAULT_DATE_FORMAT = %22MMM dd, yyyy hh:mm a%22;%0A%0A
     /**%0A
@@ -2064,16 +2064,38 @@
  object%0A
+            try %7B%0A    
         
@@ -2149,38 +2149,35 @@
 eFormat(
-%22MMM dd, yyyy hh:mm a%22
+DEFAULT_DATE_FORMAT
 , Locale
@@ -2192,16 +2192,171 @@
 ult());%0A
+                Calendar calendar = Calendar.getInstance();%0A                calendar.setTimeInMillis(dateFormat.parse(formattedTimeString).getTime());%0A    
         
@@ -2425,16 +2425,20 @@
 ontext,%0A
+    
         
@@ -2505,32 +2505,36 @@
                 
+    
 DateUtils.MINUTE
@@ -2557,32 +2557,36 @@
                 
+    
 DateUtils.WEEK_I
@@ -2615,16 +2615,20 @@
         
+  
+  
 0)%0A     
@@ -2655,24 +2655,126 @@
 toString();%0A
+            %7D catch(ParseException ex)%7B%0A                // Silently catch the exception%0A            %7D%0A
         %7D%0A%0A 

parse the date