using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace pos2txt
{
    class Program
    {
        static string filePath, newFile;

        static void Main(string[] args)
        {

            Console.WriteLine("Název vstupního adresáře:");
            string directoryName = Console.ReadLine();
            string inputDirectory = Directory.GetCurrentDirectory() + @"\" + directoryName;

            //get files in input firectory
            string[] dirs = Directory.GetFiles(inputDirectory, "*.pos");

            Console.WriteLine("Output file name:");
            Console.WriteLine(directoryName + ".txt");
            newFile = Directory.GetCurrentDirectory() + @"\" + directoryName + ".txt";


            if (File.Exists(newFile))
            {
                File.Delete(newFile);
            }


            foreach (string dir in dirs)
            {

                // read file
                List<string> lines = ReadLines(dir);
                filePath = newFile;
                //remove comment lines
                lines = lines.Where(x => x.Contains("%") != true && x.Contains("/") != false).ToList();

                WriteLines(lines, newFile);
            }

            Console.WriteLine("Done..!");
            Console.ReadKey();
        }



        /// <summary>
        /// Read lines from input file
        /// </summary>
        /// <param name="pathfile">input</param>
        /// <returns></returns>
        static List<string> ReadLines(string pathfile)
        {
            List<string> lines = new List<string>();

            using (StreamReader sr = new StreamReader(new FileStream(pathfile, FileMode.Open)))
            {
                while (!sr.EndOfStream)
                {
                    string buffer = sr.ReadLine();
                    lines.Add(buffer);
                }
            }

            return lines;
        }

        /// <summary>
        /// Write lines to output file
        /// </summary>
        /// <param name="inputLines">lines from input file</param>
        /// <param name="outputFile">output file name</param>
        static void WriteLines(List<string> inputLines, string outputFile)
        {
            using (StreamWriter sw = new StreamWriter(new FileStream(newFile, FileMode.Append)))
            {
                foreach (var line in inputLines)
                {
                    sw.WriteLine(line);
                }
            }
        }

    }
}

