package com.mapbox.mapboxview;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Locale;
import java.util.Random;

import android.content.Context;
import android.util.AttributeSet;

import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;

public class MapboxView extends MapView {
	private String mapID;
	
	public MapboxView(Context context) {
		super(context);
	}
	
	public MapboxView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	public MapboxView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}
	
	public MapboxView(Context context, GoogleMapOptions options) {
		super(context, options);
	}
	
	public MapboxView(Context context, String mapID) {
		this(context);
		
		this.mapID = mapID;
		
		GoogleMap map = getMap();
		// TODO - Set the correct width, height
		MapboxTileProvider tileProvider = new MapboxTileProvider(200, 200, this.mapID);
		
		map.addTileOverlay(new TileOverlayOptions().tileProvider(tileProvider));
	}
	
	String getMapID() {
		return mapID;
	}
	
	public class MapboxTileProvider extends UrlTileProvider {
		private String mapID;
		
		public MapboxTileProvider(int width, int height, String mapID) {
			super(width, height);
			
			this.mapID = mapID;
		}

		@Override
		public URL getTileUrl(int x, int y, int zoom) {
			String[] letters = { "a", "b", "c", "d" };
			int randomIndex = new Random().nextInt(4);
			
			String URLString = String.format(Locale.ENGLISH, "https://%s.tiles.mapbox.com/v3/%s/%d/%d/%d%s.%s", letters[randomIndex], this.mapID, zoom, x, y, "2x", "png");
			URL url = null;
			
			try {
				url = new URL(URLString);
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return url;
		}
	}
}

@@ -807,16 +807,143 @@
 pID;%0A%09%09%0A
+%09%09// TODO - Get JSON tile dictionary from format(Locale.ENGLISH, %22https://a.tiles.mapbox.com/v3/%25s.json%22, this.mapID)%0A%09%09%0A%09%09%0A%09%09%0A
 %09%09Google
@@ -1761,24 +1761,63 @@
 = null;%0A%09%09%09%0A
+%09%09%09if (checkTileExists(x, y, zoom)) %7B%0A%09
 %09%09%09try %7B%0A%09%09%09
@@ -1813,16 +1813,17 @@
 %09%09try %7B%0A
+%09
 %09%09%09%09url 
@@ -1844,16 +1844,17 @@
 tring);%0A
+%09
 %09%09%09%7D cat
@@ -1892,90 +1892,386 @@
 %09%09%09%09
-// TODO Auto-generated catch block%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09return url
+%09throw new AssertionError(e);%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09return url;%0A%09%09%7D%0A%0A%09%09/*%0A%09%09 * Check that the tile server supports the requested x, y and zoom.%0A%09%09 */%0A%09%09private boolean checkTileExists(int x, int y, int zoom) %7B%0A%09%09%09int minZoom = 12;%0A%09%09%09int maxZoom = 16;%0A%09%09%09%0A%09%09%09boolean tileExists = true;%0A%0A%09%09%09if ((zoom %3C minZoom %7C%7C zoom %3E maxZoom)) %7B%0A%09%09%09%09tileExists = false;%0A%09%09%09%7D%0A%0A%09%09%09return tileExists
 ;%0A%09%09

Check to make sure tile is at appropriate zoom level before fetching