package cgeo.geocaching.connector.trackable;

import cgeo.geocaching.Trackable;
import cgeo.geocaching.connector.UserAction;
import cgeo.geocaching.connector.gc.GCConnector;
import cgeo.geocaching.connector.gc.GCParser;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;

import java.util.List;
import java.util.regex.Pattern;

public class TravelBugConnector extends AbstractTrackableConnector {

    /**
     * TB codes really start with TB1, there is no padding or minimum length
     */
    private final static Pattern PATTERN_TB_CODE = Pattern.compile("(TB[0-9A-Z]+)|([0-9A-Z]{6})", Pattern.CASE_INSENSITIVE);

    @Override
    public boolean canHandleTrackable(String geocode) {
        return TravelBugConnector.PATTERN_TB_CODE.matcher(geocode).matches() && !StringUtils.startsWithIgnoreCase(geocode, "GC");
    }

    @Override
    public String getUrl(Trackable trackable) {
        return "http://www.geocaching.com//track/details.aspx?tracker=" + trackable.getGeocode();
    }

    @Override
    public boolean isLoggable() {
        return true;
    }

    @Override
    public Trackable searchTrackable(String geocode, String guid, String id) {
        return GCParser.searchTrackable(geocode, guid, id);
    }

    /**
     * initialization on demand holder pattern
     */
    private static class Holder {
        private static final TravelBugConnector INSTANCE = new TravelBugConnector();
    }

    private TravelBugConnector() {
        // singleton
    }

    public static TravelBugConnector getInstance() {
        return Holder.INSTANCE;
    }

    @Override
    public @Nullable
    String getTrackableCodeFromUrl(@NonNull String url) {
        // coord.info URLs
        String code = StringUtils.substringAfterLast(url, "coord.info/");
        if (code != null && canHandleTrackable(code)) {
            return code;
        }
        code = StringUtils.substringAfterLast(url, "?tracker=");
        if (code != null && canHandleTrackable(code)) {
            return code;
        }
        return null;
    }

    @Override
    public @NonNull
    List<UserAction> getUserActions() {
        // travel bugs should have the same actions as GC caches
        return GCConnector.getInstance().getUserActions();
    }
}
@@ -736,32 +736,38 @@
 HandleTrackable(
+final 
 String geocode) 
@@ -944,16 +944,22 @@
  getUrl(
+final 
 Trackabl
@@ -1205,16 +1205,22 @@
 ackable(
+final 
 String g
@@ -1226,16 +1226,22 @@
 geocode,
+ final
  String 
@@ -1245,16 +1245,22 @@
 ng guid,
+ final
  String 
@@ -1755,16 +1755,22 @@
 NonNull 
+final 
 String u
@@ -1806,24 +1806,30 @@
 RLs%0A        
+final 
 String code 
@@ -1823,24 +1823,25 @@
  String code
+1
  = StringUti
@@ -1895,32 +1895,16 @@
     if (
-code != null && 
 canHandl
@@ -1910,32 +1910,33 @@
 leTrackable(code
+1
 )) %7B%0A           
@@ -1939,32 +1939,33 @@
      return code
+1
 ;%0A        %7D%0A    
@@ -1968,20 +1968,34 @@
         
+final String 
 code
+2
  = Strin
@@ -2055,24 +2055,8 @@
 if (
-code != null && 
 canH
@@ -2074,16 +2074,17 @@
 ble(code
+2
 )) %7B%0A   
@@ -2103,16 +2103,17 @@
 urn code
+2
 ;%0A      

Remove useless null checks