/*
 * This file is part of Bitsquare.
 *
 * Bitsquare is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 *
 * Bitsquare is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
 */

package io.bitsquare.gui.util.validation;

/*
 * Interac e-Transfer requires a mail address or Canadian (mobile) phone number
 *
 * Mail addresses are covered with class EmailValidator
 *
 * Phone numbers have 11 digits, expected format is +1 NPA xxx-xxxx
 * Plus, spaces and dash might be omitted
 * Canadian area codes (NPA) taken from http://www.cnac.ca/canadian_dial_plan/Current_&_Future_Dialling_Plan.pdf
 * Valid (as of 2017-06-27) NPAs are hardcoded here
 * They are to change in some future (according to the linked document around 2019/2020)
 */

public final class InteracETransferValidator extends InputValidator {

    private static final String[] NPAS = {"204", "226", "236", "249", "250", "289", "306", "343", "365", "403", "416", "418", "431", "437", "438", "450", "506", "514", "519", "548", "579", "581", "587", "604", "613", "639", "647", "705", "709", "778", "780", "782", "807", "819", "825", "867", "873", "902", "905"};
    private final EmailValidator emailValidator;


    ///////////////////////////////////////////////////////////////////////////////////////////
    // Public methods
    ///////////////////////////////////////////////////////////////////////////////////////////

    public InteracETransferValidator() {
        super();
        emailValidator = new EmailValidator();
    }

    @Override
    public ValidationResult validate(String input) {
        if (input == null)
            return new ValidationResult(false, "input is null");
        ValidationResult emailResult = emailValidator.validate(input);
        if (!emailResult.isValid)
            return validatePhoneNumber(input);
        return emailResult;
    }


    ///////////////////////////////////////////////////////////////////////////////////////////
    // Private methods
    ///////////////////////////////////////////////////////////////////////////////////////////

    private ValidationResult validatePhoneNumber(String input) {

        // check for correct format and strip +, space and -
        if (input.matches("\\+?1[ -]?\\d{3}[ -]?\\d{3}[ -]?\\d{4}")) {
            input = input.replace("+", "");
            input = input.replace(" ", "");
            input = input.replace("-", "");

            String inputAreaCode = input.substring(1, 4);
            for (String s : NPAS) {
                // check area code agains list and return if valid
                if (inputAreaCode.compareTo(s) == 0)
                    return new ValidationResult(true);
            }
        } else
            return new ValidationResult(false, "Invalid phone number format and not an email address");
        return new ValidationResult(false, "Non-Canadian area code");
    }
}

@@ -1950,25 +1950,8 @@
 ) %7B%0A
-        super();%0A
     
@@ -1993,16 +1993,16 @@
 ator();%0A
+
     %7D%0A%0A 
@@ -2079,92 +2079,140 @@
     
-if (input == null)%0A            return new ValidationResult(false, %22input is null%22);%0A
+ValidationResult result = validateIfNotEmpty(input);%0A        if (!result.isValid) %7B%0A            return result;%0A        %7D else %7B%0A    
     
@@ -2270,32 +2270,36 @@
 alidate(input);%0A
+    
         if (!ema
@@ -2294,17 +2294,16 @@
     if (
-!
 emailRes
@@ -2315,16 +2315,73 @@
 sValid)%0A
+                return emailResult;%0A            else%0A    
         
@@ -2427,35 +2427,17 @@
         
-return emailResult;
+%7D
 %0A    %7D%0A%0A
@@ -2718,17 +2718,16 @@
 nput) %7B%0A
-%0A
         
@@ -3262,24 +3262,98 @@
           %7D%0A
+            return new ValidationResult(false, %22Non-Canadian area code%22);%0A
         %7D el
@@ -3350,24 +3350,26 @@
       %7D else
+ %7B
 %0A           
@@ -3473,69 +3473,9 @@
     
-return new ValidationResult(false, %22Non-Canadian area code%22);
+%7D
 %0A   

Use validateIfNotEmpty