import java.util.Scanner;

public class SpyTest
{
    public static void main(String[]  args)
    {
        Scanner scan = new Scanner(System.in); 
        /**
         * Get the user to enter the names and passwords for five spies.
         * Create 5 Spy objects, store the objects in an array (of type Spy) called spies.
         * Furthermore, store their passwords in an array
         * of strings called passwords.
         * */
        Spy[] spies = new Spy [5];
        String[] passwords = new String[5];
        
        String currentName = "";
        
        for (int i = 0; i < 5; i++)    
            {
                System.out.println("Welcome spy" + (i+1));
                System.out.println("Please enter your name");
                currentName = scan.nextLine();
                System.out.println("Please enter your passwords");
                passwords[i] = scan.nextLine();
                spies[i] = new Spy(currentName, passwords[i], (i+1));    
                System.out.println("Your assigned serialID is: " + spies[i].getID());
                System.out.println();
            }
        
        
        //Set phoenix  
        Spy.setPhoenix(passwords);
        
        /**
         * At some other time, when all the spies are present, they'd like to learn
         * the secret. In this case, the spies creat a string consisting of the concatenation of 
         * their encoded passwords in order from the first spy to the last, and call the unveil method 
         * on this string.
         * */
        
        
        //Authentication
        String fiveEncodedPass = "";
        
        System.out.println("^--------------------------------------------^");
        System.out.println("||||||||||||||||||||||||||||||||||||||||||||||");
        for (int i = 0; i < 5; i++)
            {
                System.out.println("Spy " + (i+1) + " please enter your serial ID:");
                if (scan.nextInt() != (spies[i]).getID())
                    {
                        //Authentication fails for the ith spy
                        System.out.println("There's a foe among friends!");
                        System.out.println("Watch out for # " + (i+1)); 
                        break;
                    }
                else
                    {
                        scan.nextLine();
                        System.out.println("Please enter your password");
                        fiveEncodedPass += scan.nextLine();
                    }
            }
        
        //Encode the concatenation of all spies' passwords (in order)
        fiveEncodedPass = Spy.encode(fiveEncodedPass);
        
        
        /**
         * Check that the encoding of the concatenation of all 
         * five passwords equals phoenix and if there 's a mismatch, display an message 
         * indicating that the secret has been lost forever and set the Master secret to be 
         * the null string. Otherwise, call the unveilSecret method and let the 
         * masterSecret be the output of this method. 
         **/
        if (fiveEncodedPass.equals(Spy.getPhoenix()))
            
            
            {
                String msg = "";
                for (int i = 0; i < 5; i++)
                    msg += spies[i].getSecretPortion();
                System.out.println();
                System.out.println("The Master Secret is: ");
                System.out.println();
                System.out.println(Spy.unveilSecret(msg));
            }
        else
            {
                System.out.println("Secret has been lost forever!");
                Secret.secret = null;
            }
        
    }
}
