using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace CensusData
{

    public class PopulationAgeRange
    {
        public PopulationAgeRange() : this(0, 0, 0) { }

        public PopulationAgeRange(int pop, int start, int end)
        {
            Population = pop;
            StartRange = start;
            EndRange = end;
        }

        public int Population { get; set; }
        public int StartRange { get; set; }
        public int EndRange { get; set; }
    }

    public class Region
    {
        public Region()
        {
            popRanges = new List<PopulationAgeRange>();
        }

        public string Name { get; set; }
        public int TotalPopulation { get; set; }
        public int MalePopulation { get; set; }
        public int FemalePopulation { get; set; }

        private List<PopulationAgeRange> popRanges;

        public List<PopulationAgeRange> PopRanges
        {
            get { return popRanges; }
            set { popRanges = value; }
        }
    }

    public class State
    {
        public State()
        {
            regionsInState = new List<Region>();
        }

        private List<Region> regionsInState;

        public List<Region> RegionsInState
        {
            get { return regionsInState; }
            set { regionsInState = value; }
        }
    }

    public class CensusDemographicsParser
    {
        public static State ParseFile(string file)
        {
            State ret = new State();

            using (StreamReader sr = new StreamReader(file))
            {
                string line = String.Empty;

                while ((line = sr.ReadLine()) != null)
                {
                    string[] lineParts = line.Split(',');

                    // Grab the parts of the line that we care about
                    Region r = new Region();
                    r.Name = RemoveQuotes(lineParts[14]);

                    r.PopRanges.Add(new PopulationAgeRange(Convert.ToInt32(lineParts[18]), 0, 4));
                    r.PopRanges.Add(new PopulationAgeRange(Convert.ToInt32(lineParts[19]), 5, 9));
                    r.PopRanges.Add(new PopulationAgeRange(Convert.ToInt32(lineParts[20]), 10, 14));
                    r.PopRanges.Add(new PopulationAgeRange(Convert.ToInt32(lineParts[21]), 15, 19));
                    r.PopRanges.Add(new PopulationAgeRange(Convert.ToInt32(lineParts[22]), 20, 24));
                    r.PopRanges.Add(new PopulationAgeRange(Convert.ToInt32(lineParts[23]), 25, 34));
                    r.PopRanges.Add(new PopulationAgeRange(Convert.ToInt32(lineParts[24]), 35, 44));
                    r.PopRanges.Add(new PopulationAgeRange(Convert.ToInt32(lineParts[25]), 45, 54));
                    r.PopRanges.Add(new PopulationAgeRange(Convert.ToInt32(lineParts[26]), 55, 59));
                    r.PopRanges.Add(new PopulationAgeRange(Convert.ToInt32(lineParts[27]), 60, 64));
                    r.PopRanges.Add(new PopulationAgeRange(Convert.ToInt32(lineParts[28]), 65, 74));
                    r.PopRanges.Add(new PopulationAgeRange(Convert.ToInt32(lineParts[29]), 75, 84));
                    r.PopRanges.Add(new PopulationAgeRange(Convert.ToInt32(lineParts[30]), 85, 200));

                    //  Add all the Ranges to the Region

                    ret.RegionsInState.Add(r);



                }

            }
            return ret;
        }

        private static string RemoveQuotes(string s)
        {
            StringBuilder result = new StringBuilder();

            foreach (char c in s)
            {
                if (c != '\"')
                    result.Append(c);
            }

            return result.ToString();
        }
    }
}