package com.sanchez.fmf.util;

import com.sanchez.fmf.R;

import java.util.Random;

/**
 * Created by dakota on 8/31/15.
 */
public class MarketUtils {

    private static final Random RANDOM = new Random();

    // Non-instantiatable class
    private MarketUtils() {
    }

    // get distance (in miles) from 'marketName' json key
    public static String getDistanceFromMarketString(String marketString) {
        StringBuilder dist = new StringBuilder();
        int i = 0;
        while (marketString.charAt(i) != ' ') {
            dist.append(marketString.charAt(i));
            i++;
        }
        return dist.toString();
    }

    // strip distance (in miles) from 'marketName' json key
    public static String getNameFromMarketString(String marketString) {
        return marketString.substring(marketString.indexOf(" ") + 1);
    }

    public static double[] getCoordinatesFromMapUrl(String mapUrl) {
        int queryStart = mapUrl.indexOf("=") + 1;
        int nameStart = mapUrl.indexOf("(");

        String[] coordinates = mapUrl.substring(queryStart, nameStart).split("%20");
        coordinates[0] = coordinates[0].replace("%2C", "");

        return new double[] {Double.parseDouble(coordinates[0]), Double.parseDouble(coordinates[1])};
    }

    public static String getQueryFromMapUrl(String mapUrl) {
        int queryStart = mapUrl.indexOf("=") + 1;
        return mapUrl.substring(queryStart);
    }

    public static int getRandomMarketColor() {

        switch (RANDOM.nextInt(5)) {
            default:
            case 0:
                return R.color.market_blue;
            case 1:
                return R.color.market_red;
            case 2:
                return R.color.market_green;
            case 3:
                return R.color.market_orange;
            case 4:
                return R.color.market_pink;
        }
    }

    public static int getRandomMarketDrawable() {
        switch (RANDOM.nextInt(4)) {
            default:
            case 0:
                return R.drawable.market_1;
            case 1:
                return R.drawable.market_2;
            case 2:
                return R.drawable.market_3;
            case 3:
                return R.drawable.market_4;
        }
    }
}

@@ -277,898 +277,396 @@
     
-// get distance (in miles) from 'marketName' json key%0A    public static String getDistanceFromMarketString(String marketString) %7B%0A        StringBuilder dist = new StringBuilder();%0A        int i = 0;%0A        while (marketString.charAt(i) != ' ') %7B%0A            dist.append(marketString.charAt(i));%0A            i++;%0A        %7D%0A        return dist.toString();%0A    %7D%0A%0A    // strip distance (in miles) from 'marketName' json key%0A    public static String getNameFromMarketString(String marketString) %7B%0A        return marketString.substring(marketString.indexOf(%22 %22) + 1
+public static double haversine(double lat1, double lon1, double lat2, double lon2) %7B%0A        double dLat = Math.toRadians(lat2 - lat1);%0A        double dLon = Math.toRadians(lon2 - lon1);%0A        lat1 = Math.toRadians(lat1);%0A        lat2 = Math.toRadians(lat2
 );%0A
+%0A
     
-%7D%0A%0A
     
-public static double%5B%5D getCoordinatesFromMapUrl(String mapUrl) %7B%0A        int queryStart = mapUrl.indexOf(%22=%22) + 1;%0A        int nameStart = mapUrl.indexOf(%22(%22);%0A%0A        String%5B%5D coordinates = mapUrl.substring(queryStart, nameStart).split(%22%2520%22);%0A        coordinates%5B0%5D = coordinates%5B0%5D.replace(%22%252C%22, %22%22
+double a = Math.pow(Math.sin(dLat / 2),2) + Math.pow(Math.sin(dLon / 2),2) * Math.cos(lat1) * Math.cos(lat2
 );%0A
-%0A
         
 retu
@@ -665,263 +665,112 @@
     
-return new double%5B%5D %7BDouble.parseDouble(coordinates%5B0%5D), Double.parseDouble(coordinates%5B1%5D)%7D
+double c = 2 * Math.asin(Math.sqrt(a))
 ;%0A    
-%7D%0A%0A
     
-public static String getQueryFromMapUrl(String mapUrl) %7B%0A        int queryStart = mapUrl.indexOf(%22=%22) + 1;%0A        return mapUrl.substring(queryStart)
+double radius = 3959; // miles%0A        return radius * c
 ;%0A  

Remove unused functions and add haversine