using System;
using System.Collections.Generic;
using System.Text;
using Halo3Map.Data;

namespace Halo3Map.Map
{
    /// <summary>
    /// Holds and reads infomation from a halo3 string table
    /// </summary>
    public class StringsTable    
    {
        /// <summary>
        /// Holds a list of halo 3 strings
        /// </summary>
        private List<String> strings;
        /// <summary>
        /// Gets list of Strings 
        /// </summary>
        public List<String> Strings { get { return strings; } }
        /// <summary>
        /// Creates new instance of StringsTable Class
        /// </summary>
        public StringsTable()
        {

        }
        /// <summary>
        /// Reads strings from halo 3 map
        /// </summary>
        /// <param name="count">Number of strings to read</param>
        /// <param name="br">Stream to read from</param>
        public void Read(int count, Halo3Map.IO.EndianReader br)
        {
            //Initialize strings list
            strings = new List<String>();
            //For amount of strings specified to read
            for (int i = 0; i < count; i++)
            {
                //Create new 'String' container to hold the string
                String s = new String();
                //Read from map
                s.Read(br);
                //Add to list of strings
                strings.Add(s);
            }
        }

        public void ExtractToTXTFile(System.IO.StreamWriter sw)
        {
            for (int i = 0; i < strings.Count; i++)
            {
                sw.WriteLine(strings[i].Name + " Index: " + i.ToString() + " Length: " + strings[i].Name.Length);                
            }
            
        }

        /// <summary>
        /// Holds a halo 3 string and its information
        /// </summary>
        public class String : Chunk
        {
            //This specifies the layout of a halo 3 string so we can read it
            #region Layout
            public string Name;
            #endregion
            /// <summary>
            /// Read string from stream
            /// </summary>
            /// <param name="br">Stream to read from</param>
            public override void Read(Halo3Map.IO.EndianReader br)
            {
                //Read string
                Name = br.ReadNullTerminatedString();
            }
        }
    }
}
