/*
 * DomainParser.java
 *
 * Copyright (c) 2016 Auth0 (http://auth0.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.auth0.android.lock.utils;

import android.support.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

/**
 * Helper class to find out which email domains can be valid for the current Auth0 configuration.
 */
public class EnterpriseConnectionMatcher {

    private static final String DOMAIN_KEY = "domain";
    private static final String DOMAIN_ALIASES_KEY = "domain_aliases";

    private List<Strategy> strategies;

    public EnterpriseConnectionMatcher(List<Strategy> strategies) {
        this.strategies = new ArrayList<>();
        if (strategies == null) {
            return;
        }

        for (Strategy s : strategies) {
            if (s.getType() == Strategies.Type.ENTERPRISE) {
                this.strategies.add(s);
            }
        }
    }

    /**
     * Tries to find a valid domain with the given input.
     *
     * @param email to search the Domain for.
     * @return a Connection if found, null otherwise.
     */
    @Nullable
    public Connection parse(String email) {
        String domain = extractDomain(email);
        if (domain == null) {
            return null;
        }

        domain = domain.toLowerCase();
        for (Strategy s : strategies) {
            for (Connection c : s.getConnections()) {
                String mainDomain = c.getValueForKey(DOMAIN_KEY);
                if (mainDomain != null && mainDomain.contains(domain)) {
                    return c;
                }

                List<String> aliases = c.getValueForKey(DOMAIN_ALIASES_KEY);
                if (aliases != null) {
                    for (String d : aliases) {
                        if (d.contains(domain)) {
                            return c;
                        }
                    }
                }
            }
        }
        return null;
    }

    /**
     * Extracts the username part from the email
     *
     * @param email to parse
     * @return the username String if found, an empty String otherwise
     */
    @Nullable
    public String extractUsername(String email) {
        int indexAt = email.indexOf("@");
        if (indexAt == -1) {
            return null;
        }
        return email.substring(0, indexAt);
    }

    /**
     * Extracts the domain part from the email
     *
     * @param email to parse
     * @return the domain String if found, an empty String otherwise
     */
    @Nullable
    private String extractDomain(String email) {
        int indexAt = email.indexOf("@") + 1;
        if (indexAt == 0) {
            return null;
        }
        int indexDot = email.indexOf(".", indexAt);
        String domain;
        if (indexDot == -1) {
            domain = email.substring(indexAt);
        } else {
            domain = email.substring(indexAt, indexDot);
        }
        if (domain.isEmpty()) {
            return null;
        }
        return domain;
    }

}

@@ -1558,16 +1558,65 @@
 liases%22;
+%0A    private static final String AT_SYMBOL = %22@%22;
 %0A%0A    pr
@@ -2602,24 +2602,32 @@
 nDomain.
-contains
+equalsIgnoreCase
 (domain)
@@ -2876,16 +2876,24 @@
  (d.
-contains
+equalsIgnoreCase
 (dom
@@ -3328,19 +3328,25 @@
 indexOf(
-%22@%22
+AT_SYMBOL
 );%0A     
@@ -3722,19 +3722,25 @@
 indexOf(
-%22@%22
+AT_SYMBOL
 ) + 1;%0A 
@@ -3813,235 +3813,49 @@
     
-int indexDot = email.indexOf(%22.%22, indexAt);%0A        String domain;%0A        if (indexDot == -1) %7B%0A            domain = email.substring(indexAt);%0A        %7D else %7B%0A            domain = email.substring(indexAt, indexDot);%0A        %7D
+String domain = email.substring(indexAt);
 %0A   

fix domain extraction and matching