package com.fsryan.forsuredb.provider;

import android.net.Uri;

import java.util.HashSet;
import java.util.Set;

/*package*/ class JoinUriParser {

    private final Uri uri;
    private final String baseTableName;

    public JoinUriParser(Uri uri) {
        this.uri = uri;
        baseTableName = getBaseTableName();
    }

    public Set<String> getJoinedTableNames() {
        if (uri == null || uri.getQuery() == null || uri.getQuery().isEmpty() || baseTableName == null || baseTableName.isEmpty()) {
            Set<String> ret = new HashSet<>();
            ret.add(baseTableName);
            return ret;
        }

        final String[] parsedQuery = uri.getQuery().split("&");
        final Set<String> retSet = new HashSet<>();
        retSet.add(baseTableName);
        for (String query : parsedQuery) {
            final String tableName = joinedTableNameFromIndividualQuery(query);
            if (tableName != null && !tableName.isEmpty() && !retSet.contains(tableName)) {
                retSet.add(tableName);
            }
        }

        return retSet;
    }

    private String joinedTableNameFromIndividualQuery(String query) {
        if (query != null && !query.isEmpty()) {
            return null;
        }

        final String[] splitQuery = query.split("=");
        if (splitQuery.length < 2) {
            return null;
        }

        return joinedTableNameFromRhs(splitQuery[1]);
    }

    private String joinedTableNameFromRhs(String queryRhs) {
        final String[] splitRhs = queryRhs.split(" ");
        if (splitRhs.length == 0) {
            return null;
        }

        return splitRhs[0];
    }

    private String getBaseTableName() {
        if (uri == null) {
            return null;
        }
        return uri.getPathSegments().get(0);
    }
}

@@ -294,33 +294,64 @@
 eName = 
-getBaseTableName(
+uri == null ? null : uri.getPathSegments().get(0
 );%0A    %7D
@@ -411,81 +411,61 @@
     
-if (uri == null %7C%7C uri.getQuery() == null %7C%7C uri.getQuery().isEmpty() %7C%7C 
+final Set%3CString%3E ret = new HashSet%3C%3E();%0A        if (
 base
@@ -478,19 +478,20 @@
 ame 
-=
+!
 = null 
-%7C%7C 
+&& !
 base
@@ -529,40 +529,29 @@
     
-Set%3CString%3E ret = new HashSet%3C%3E(
+ret.add(baseTableName
 );%0A 
@@ -559,37 +559,92 @@
     
+  %7D%0A
       
-ret.add(baseTableName);
+  if (uri == null %7C%7C uri.getQuery() == null %7C%7C uri.getQuery().isEmpty()) %7B
 %0A   
@@ -743,95 +743,8 @@
 %22);%0A
-        final Set%3CString%3E retSet = new HashSet%3C%3E();%0A        retSet.add(baseTableName);%0A
     
@@ -923,39 +923,8 @@
 ty()
- && !retSet.contains(tableName)
 ) %7B%0A
@@ -942,19 +942,16 @@
      ret
-Set
 .add(tab
@@ -1002,19 +1002,16 @@
 turn ret
-Set
 ;%0A    %7D%0A
@@ -1099,28 +1099,27 @@
  (query 
-!
+=
 = null 
-&& !
+%7C%7C 
 query.is
@@ -1578,161 +1578,7 @@
    %7D
-%0A%0A    private String getBaseTableName() %7B%0A        if (uri == null) %7B%0A            return null;%0A        %7D%0A        return uri.getPathSegments().get(0);%0A    %7D
 %0A%7D%0A

fix bug with failing to evaluate the joins in a URI properly