﻿using System;
using System.Collections.Generic;

public static class Book
{
    public const string DATA_FOLDER = "Data";
    public const string METADATA_FILE = "quran-lab.txt";

    public static int CHAPTERS = 0;
    public static int CHAPTER_SUM = 0;
    public static int VERSES = 0;
    public static int WORDS = 0;
    public static int LETTERS = 0;
    public static int C_PLUS_V_SUM = 0;
    public static int C_MINUS_V_SUM = 0;
    public static int C_TIMES_V_SUM = 0;
    public static int C2_MINUS_C1_SUM = 0;
    public static int V2_MINUS_V1_SUM = 0;
    public static int W2_MINUS_W1_SUM = 0;
    public static int L2_MINUS_L1_SUM = 0;

    static Book()
    {
        if (!Initialized)
        {
            string path = Book.DATA_FOLDER + "/" + Book.METADATA_FILE;
            s_initialized = LoadChapters(path);
        }
    }

    private static bool s_initialized = false;
    public static bool Initialized
    {
        get { return s_initialized; }
    }

    public static Chapter[] Chapters = null;
    private static bool LoadChapters(string filename)
    {
        try
        {
            List<string> lines = FileHelper.LoadLines(filename);
            if (lines != null)
            {
                CHAPTERS = lines.Count;
                Chapters = new Chapter[CHAPTERS];
                int max_chapter_number = int.MinValue;
                int min_chapter_number = int.MaxValue;
                int max_chapter_verses = int.MinValue;
                int min_chapter_verses = int.MaxValue;
                int max_chapter_words = int.MinValue;
                int min_chapter_words = int.MaxValue;
                int max_chapter_letters = int.MinValue;
                int min_chapter_letters = int.MaxValue;

                for (int i = 0; i < CHAPTERS; i++)
                {
                    string[] parts = lines[i].Split('\t');
                    if (parts.Length == 4) // Chapter\tVerses\tWords\tLetters
                    {
                        int number = int.Parse(parts[0]);
                        int verses = int.Parse(parts[1]);
                        int words = int.Parse(parts[2]);
                        int letters = int.Parse(parts[3]);
                        Chapters[i] = new Chapter(number, verses, words, letters);

                        CHAPTER_SUM += number;
                        VERSES += verses;
                        WORDS += words;
                        LETTERS += letters;

                        C_PLUS_V_SUM += number + verses;
                        C_MINUS_V_SUM += Math.Abs(number - verses);
                        C_TIMES_V_SUM += number * verses;

                        if (number > max_chapter_number) max_chapter_number = number;
                        if (number < min_chapter_number) min_chapter_number = number;
                        if (verses > max_chapter_verses) max_chapter_verses = verses;
                        if (verses < min_chapter_verses) min_chapter_verses = verses;
                        if (words > max_chapter_words) max_chapter_words = words;
                        if (words < min_chapter_words) min_chapter_words = words;
                        if (letters > max_chapter_letters) max_chapter_letters = letters;
                        if (letters < min_chapter_letters) min_chapter_letters = letters;
                    }
                    else
                    {
                        throw new Exception(filename + "must be of the following format:"
                                                     + "\r\n"
                                                     + "Chapter\tVerses\tWords\tLetters");
                    }
                }

                C2_MINUS_C1_SUM = max_chapter_number - min_chapter_number;
                V2_MINUS_V1_SUM = max_chapter_verses - min_chapter_verses;
                W2_MINUS_W1_SUM = max_chapter_words - min_chapter_words;
                L2_MINUS_L1_SUM = max_chapter_letters - min_chapter_letters;
            }
        }
        catch (Exception /*ex*/)
        {
            return false;
        }
        return true;
    }
}
