using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using CensusParser;
using Amazon.SimpleDB;
using Amazon.SimpleDB.Model;
using Ionic.Utils.Zip;
using Attribute = Amazon.SimpleDB.Model.Attribute;
using CensusDB;

namespace SimpleCensusParser
{


    class Program
    {
        static private string datasetDrive = @"F:";

        static private string simpleDBDomain = "CensusDomain";

        private static string FormatDirs(string s)
        {
            string ret = string.Empty;

            // Remove the 0_ from those Regions that start with it
            if (s.IndexOf("0_") == 0)
            {
                s = s.Substring(2);
            }

            s = s.Replace('_', ' ');

            ret = s;

            return s;
        }

        static string pdsInvalidDrive = @"The drive you pointed at does not contain the Year 2000 Census Public Data Set.  Please set the value of 'datasetDrive'.";

        private static  string accessKeyId = "Enter your AWS Key here";
        private static string secretAccessKey = "Enter your AWS Private Key here";

        public static AmazonSimpleDB service;
        
        
        static void Main(string[] args)
        {
            if ((accessKeyId == "") || (secretAccessKey == ""))
            {
                Console.WriteLine("Please set the accessKeyId and secretAccessKey memebers and rerun this sample.");
            }
            else
            {
                service = new AmazonSimpleDBClient(accessKeyId, secretAccessKey);

                ConsoleKeyInfo option = new ConsoleKeyInfo();
                while (option.KeyChar != '0')
                {
                    Console.Clear();
                    Console.WriteLine("");
                    Console.WriteLine(" 1. List Census SimpleDB information");
                    Console.WriteLine(" 2. Add Simple Data Set into SimpleDB");
                    Console.WriteLine(" 3. Clean Census SimpleDB information");
                    Console.WriteLine("");
                    Console.WriteLine(" 0. Quit");
                    Console.WriteLine("");
                    Console.Write(" Selection : ");
                    option = Console.ReadKey();
                    Console.WriteLine();

                    switch (option.KeyChar)
                    {
                        case '1':
                            ListSimpleDBInfo();
                            break;
                        case '2':
                            ParseIntoDB();
                            break;
                        case '3':
                            CleanCensusDB();
                            break;
                        default:
                            break;
                    }
                }
            
            }
        }

        private static void CleanCensusDB()
        {
            Console.WriteLine();
            Console.WriteLine("Clearing the Census data from your SimpleDB account.");

            CensusSimpleDB csdb = new CensusSimpleDB(service);

            csdb.CleanCensusSimpleDB(simpleDBDomain);

            Console.WriteLine();
            Console.WriteLine("Press ENTER to continue.");
            Console.ReadLine();
        }

        private static void ParseIntoDB()
        {
            CensusSimpleDB csdb = new CensusSimpleDB(service);

            if (!csdb.DoesSimpleDBDomainExist(simpleDBDomain))
            {
                csdb.CreateSimpleDBDomain(simpleDBDomain);
            }

            StringBuilder regionsPath = new StringBuilder();
            regionsPath.Append(datasetDrive);
            regionsPath.Append(@"/census_2000/datasets/demographic_profile");

            string dirpath = regionsPath.ToString();

            DirectoryInfo regionsDI = new DirectoryInfo(dirpath);

            if (regionsDI.Exists == false)
            {
                Console.WriteLine(pdsInvalidDrive);
            }
            else
            {

                DirectoryInfo[] dirs = regionsDI.GetDirectories();

                foreach (DirectoryInfo d in dirs)
                {
                    string formattedRegion = FormatDirs(d.Name);

                    if (formattedRegion == "Washington")
                    {

                        csdb.AddDemoRegionToSimpleDB(formattedRegion, simpleDBDomain);
                        Console.WriteLine("Adding Region: " + formattedRegion);

                        State currentRegion = CensusDemographicsParser.UnzipCensusDataAndParse(d.FullName);

                        // Add the RegionDetail into SimpleDB from the currentRegionObject
                        csdb.AddDemoRegionDetailsToSimpleDB(currentRegion, simpleDBDomain);
                    }
                }

                Console.WriteLine("Number of Regions added : " + dirs.Length);
            }

            Console.WriteLine();
            Console.WriteLine("Press ENTER to continue.");
            Console.ReadLine();

        }

        private static void ListSimpleDBInfo()
        {
            Console.WriteLine();
            Console.WriteLine("Listing the Census data from your SimpleDB account.");
            Console.WriteLine();

            CensusSimpleDB csdb = new CensusSimpleDB(service);

            if (!csdb.DoesSimpleDBDomainExist(simpleDBDomain))
            {
                Console.WriteLine("The SimpleDB Domain does not exist.  Run option 2 to add data to your account.");
            }
            else
            {
                List<string> regions = csdb.GetCensusRegions(simpleDBDomain);

                int i = 0;
                foreach (string region in regions)
                {
                    Console.WriteLine(i + " Region : " + region);
                    i++;
                }

                ListDetails(regions);
            }

            Console.WriteLine("Press ENTER to continue.");
            Console.ReadLine();
        }

        private static void ListDetails(List<string> regions)
        {

            CensusSimpleDB csdb = new CensusSimpleDB(service);

            if (!csdb.DoesSimpleDBDomainExist(simpleDBDomain))
            {
                Console.WriteLine("The SimpleDB Domain ('" + simpleDBDomain + "') does not exist.  Run option 2 to add data to your account.");
            }
            else
            {
                foreach(string region in regions)
                {
                    State s = csdb.GetCensusRegionFromSimpleDB(region, simpleDBDomain);
                }
            }
            
        }

    }
}
