using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Crawler
{
    class Program
    {
        static void Main(string[] args)
        {
            var settings = new Settings();
            var executer = new Executer(new Crawler(settings), settings);

            Console.WriteLine(" >> Connecting to {0}", settings.BaseUrl);
            Console.WriteLine(" >> Trying to authenticate User: {0}", settings.Username);

            if (executer.Login())
            {
                Console.WriteLine("");
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine(" >> User authenticated");

                while (true)
                {
                    Console.ForegroundColor = ConsoleColor.White;
                    Uri url;
                    Console.WriteLine(" >> Url:");

                    var input = Console.ReadLine();
                    if (!Uri.TryCreate(input, UriKind.Absolute, out url))
                    {
                        Console.WriteLine(" >> Invalid Url");
                    }
                    else
                    {
                        //Email Request
                        bool logAgain = executer.GetEmail(url);
                        if (logAgain) {
                            break;
                        }
                    }
                }
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Authentication Failed, Please check your credentials");
                Console.ReadKey();
            }
            
            //Restart the Login Process After Changing IP
            Console.WriteLine("Press Any Key to Login Again (After changing the IP)");
            Console.ReadKey();
            Main(null);
        }
    }
}
