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

namespace Lab_Extensibility.InputReaders
{
    public class CsvInputReader : IInputReader
    {
        private readonly string _inputFileName;

        public CsvInputReader(string inputFileName)
        {
            ArgumentException.ThrowIfNullOrEmpty(inputFileName);
            _inputFileName = inputFileName;
        }

        public List<Person> Read()
        {
            List<Person> persons = new();

            using (StreamReader reader = new(_inputFileName))
            {
                Console.WriteLine($"File has been opened ({_inputFileName})");

                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    var columns = new System.Text.RegularExpressions.Regex(
                        "((?<=\")[^\"]*(?=\"(,|$)+)|(?<=,|^)[^,\"]*(?=,|$))"
                    ).Matches(line);

                    persons.Add(new Person(
                        firstName: columns[0].Value,
                        lastName: columns[1].Value,
                        companyName: columns[2].Value,
                        address: columns[3].Value,
                        city: columns[4].Value,
                        state: columns[6].Value,
                        age: columns[10].Value,
                        weight: columns[11].Value,
                        decease: columns[12].Value
                    ));
                }
            }

            return persons;
        }
    }


}
