using System;
using System.Collections.Generic;
using System.Text;
using SokobanResultSearch.BusinessEntities;
using System.IO;

namespace SokobanResultSearch
{
    class SokobanTableProvider : TableProvider
    {
        #region Private fields

        /// <summary>
        /// Lists tables.
        /// </summary>
        private List<ITable> _tables;

        #endregion

        #region  Constanses

        /// <summary>
        /// The name of the file which contains the tables.
        /// </summary>
        public const string TablesFileName = "tablak.skb";

        #endregion

        #region Overrides

        /// <summary>
        /// Reads the tables from the table file.
        /// </summary>
        public override List<ITable> GetTables()
        {
            if (_tables == null)
            {
                _tables = new List<ITable>();
                StringBuilder resultBuilder = new StringBuilder();

                try
                {
                    using (TextReader reader = File.OpenText(TablesFileName))
                    {
                        string line = reader.ReadLine();
                        int dimY = int.MinValue;

                        while (line != "END")
                        {
                            int dimX = 0;
                            while (line.Contains("#"))
                            {
                                if (line.Length > dimY)
                                    dimY = line.Length;

                                ++dimX;
                                resultBuilder.AppendLine(line);
                                line = reader.ReadLine();
                            }
                            if (resultBuilder.Length > 0)
                            {
                                _tables.Add(new Table(resultBuilder.ToString(),dimX, dimY));
                                resultBuilder = new StringBuilder();
                            }
                            line = reader.ReadLine();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }

                return _tables;
            }
            return _tables;
        }

        /// <summary>
        /// Generates a byte array from a table representation.
        /// </summary>
        public override byte[,] ConvertTableToByteArray(ITable table)
        {
            byte[,] result = new byte[table.DimX, table.DimY];
            int j = 0;
            int k = 0;

            for (int i = 0; i < table.Value.Length; i++)
            {
                switch (table.Value[i])
                {
                    case ' ':
                        result[j, k] = 0;
                        break;
                    case '@':
                        result[j, k] = 1;
                        break;
                    case '$':
                        result[j, k] = 2;
                        break;
                    case '#':
                        result[j, k] = 3;
                        break;
                    case '.':
                        result[j, k] = 4;
                        break;
                    case '*':
                        result[j, k] = 6;
                        break;
                    case '\r':
                        break;
                    case '\n':
                        ++j;
                        k = -1;
                        break;
                    default:
                        result[j, k] = 5;
                        break;
                }
                ++k;
            }

            return result;
        }

        #endregion
    }
}
