package com.sam_chordas.android.stockhawk.rest;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.preference.PreferenceManager;
import android.text.format.DateUtils;

import com.sam_chordas.android.stockhawk.R;
import com.sam_chordas.android.stockhawk.service.StockTaskService;

import org.threeten.bp.Instant;

import java.text.DecimalFormat;
import java.util.Locale;

/**
 * Created by sam_chordas on 10/8/15.
 */
public class Utils {
    public static boolean showPercent = true;

    public static String truncateBidPrice(String bidPrice) {
        bidPrice = String.format(Locale.ENGLISH, "%.2f", Float.parseFloat(bidPrice));
        return bidPrice;
    }

    public static String truncateChange(String change, boolean isPercentChange) {
        String weight = change.substring(0, 1);
        String ampersand = "";
        if (isPercentChange) {
            ampersand = change.substring(change.length() - 1, change.length());
            change = change.substring(0, change.length() - 1);
        }
        change = change.substring(1, change.length());
        double round = (double) Math.round(Double.parseDouble(change) * 100) / 100;
        change = String.format(Locale.ENGLISH, "%.2f", round);
        StringBuffer changeBuffer = new StringBuffer(change);
        changeBuffer.insert(0, weight);
        changeBuffer.append(ampersand);
        change = changeBuffer.toString();
        return change;
    }

    /**
     * Returns true if the network is available or about to become available.
     *
     * @param c Context used to get the ConnectivityManager
     * @return true if the network is available
     */
     public static boolean isNetworkAvailable(Context c) {
        ConnectivityManager cm =
                (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        return activeNetwork != null &&
                activeNetwork.isConnectedOrConnecting();
    }

    /**
     * Retrieves application status from shared preference.
     *
     * @param context Context used to get the SharedPreferences
     * @return hawk status integer type
     */
    @SuppressWarnings("ResourceType")
    static public @StockTaskService.HawkStatus
    int getHawkStatus(Context context){
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        return sp.getInt(context.getString(R.string.pref_hawk_status_key), StockTaskService.HAWK_STATUS_UNKNOWN);
    }

    /**
     * Resets stock hawk status to HAWK_STATUS_UNKNOWN
     *
     * @param context Context used to get the SharedPreferences
     */
    static public void resetHawkStatus(Context context){
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor spe = sp.edit();
        spe.putInt(context.getString(R.string.pref_hawk_status_key), StockTaskService.HAWK_STATUS_UNKNOWN);
        spe.apply();
    }

    static public String formatGraphDateLabels(String date) {
        String currentDate = Instant.now().toString();
        String formattedDate;

        if (currentDate.substring(0, 9).equals(date.substring(0, 9))) {
            if (Integer.parseInt(currentDate.substring(9, 10)) == Integer.parseInt(date.substring(9, 10))
                || Integer.parseInt(currentDate.substring(9, 10)) - 1 == Integer.parseInt(date.substring(9, 10)))
            {
                formattedDate = date.substring(11, 16);
            } else {
                formattedDate = date.substring(5, 10);
            }
        } else {
            formattedDate = date.substring(5, 10);
        }

        return formattedDate;
    }

    /** Returns relative time since update time */
    static public String formatLastUpdateTime(Context context, String updateTime) {
        String formattedUpdateTime = DateUtils.getRelativeTimeSpanString(Instant.parse(updateTime).toEpochMilli(),
                Instant.now().toEpochMilli(), DateUtils.MINUTE_IN_MILLIS).toString();
        return formattedUpdateTime.charAt(0) == '0' ? context.getString(R.string.last_updated_minute)
                : formattedUpdateTime;
    }

    /** Formats stock bid price for device's locale */
    static public String formatBidPrice(Context context, String bidPrice) {
        if (!bidPrice.equals(context.getResources().getString(R.string.data_not_available))) {
            DecimalFormat decimalFormat = new DecimalFormat("'$'#.00");
            bidPrice = decimalFormat.format(Double.parseDouble(bidPrice));
        } else {
            bidPrice = context.getString(R.string.data_not_available_label);
        }
        return bidPrice;
    }

    /** Formats stock change for device's locale */
    static public String formatChange(Context context, String change) {
        if (!change.equals(context.getResources().getString(R.string.data_not_available))) {
            DecimalFormat decimalFormat = new DecimalFormat("+#0.00;-#");
            change = decimalFormat.format(Double.parseDouble(change));
        } else {
            context.getString(R.string.data_not_available_label);
        }
        return change;
    }

    /** Formats stock change in percent for device's locale */
    static public String formatChangeInPercent(Context context, String changeInPercent) {
        if (!changeInPercent.equals(context.getResources().getString(R.string.data_not_available))) {
            DecimalFormat decimalFormat = new DecimalFormat("+#0.00%;-#%");
            decimalFormat.setMultiplier(1);
            changeInPercent = decimalFormat.format(Double.parseDouble(
                    changeInPercent.substring(0, changeInPercent.length() - 1)));
        } else {
            context.getString(R.string.data_not_available_label);
        }
        return changeInPercent;
    }
}

@@ -435,28 +435,115 @@
 a.text.D
-ecimalFormat
+ateFormat;%0Aimport java.text.DecimalFormat;%0Aimport java.text.SimpleDateFormat;%0Aimport java.util.Date
 ;%0Aimport
@@ -3274,32 +3274,331 @@
 w().toString();%0A
+        SimpleDateFormat df = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT);%0A        // Removes year from formatted date%0A        String pattern = df.toLocalizedPattern().replaceAll(%22.?%5BYy%5D.?%22, %22%22);%0A        SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.getDefault());%0A
         String f
@@ -3938,45 +3938,151 @@
     
-formattedDate = date.substring(11, 16
+df = new SimpleDateFormat(%22HH:mm%22, Locale.getDefault());%0A                formattedDate = df.format(new Date(Instant.parse(date).toEpochMilli())
 );%0A 
@@ -4093,32 +4093,33 @@
        %7D else %7B%0A
+%0A
                 
@@ -4134,36 +4134,71 @@
 dDate = 
-date.substring(5, 10
+sdf.format(new Date(Instant.parse(date).toEpochMilli())
 );%0A     
@@ -4255,28 +4255,63 @@
 e = 
-date.substring(5, 10
+sdf.format(new Date(Instant.parse(date).toEpochMilli())
 );%0A 

Format graph date labels according to device's locale