using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ImageBlockz.Core
{
    /// <summary>
    /// Reader for CSV colormap files.
    /// </summary>
    public class CsvReader
    {
        /// <summary>
        /// Static method for reading colormaps from csv file from requested path.
        /// </summary>
        /// <param name="path">File name with path.</param>
        /// <returns>Array of values. Each row contains value for R, G and B channel.</returns> 
        public static int[,] ReadCsv(string path)
        {
            System.Globalization.CultureInfo cult = new System.Globalization.CultureInfo("");

            int[,] csvTable = new int[256,3];

            try
            {
                string appPath = Application.StartupPath + path;
                string[] lines = System.IO.File.ReadAllLines(appPath);

                char[] delimiter = { ';' };
                int count = 3;

                for (int i = 0; i < 256; i++)
                {
                    string line = lines[i];
                    string[] words = line.Split(delimiter, count);

                    csvTable[i, 0] = Int32.Parse(words[0], cult);
                    csvTable[i, 1] = Int32.Parse(words[1], cult);
                    csvTable[i, 2] = Int32.Parse(words[2], cult);
                }
            }
            catch
            {
                System.Windows.Forms.MessageBox.Show("ERROR>> File " + path + ".csv not found.\r\n");
            }
           
            return csvTable;
        }
    }
}
