using System;
using System.Collections.Generic;
using System.Linq;

namespace ZionRobot.Models.Logs
{
    public class Search
    {
        private string path;
        private List<string> searchResults;

        public List<string> SearchResults { get => searchResults; set => SearchResults = value; }

        public Search(string path)
        {
            this.path = path;
            this.searchResults = new List<string>();
            if (System.IO.File.Exists(this.path))
            {
                searchResults = System.IO.File.ReadAllLines(this.path).ToList();
            }
        }

        public void Save()
        {
            try
            {
                using (System.IO.StreamWriter stream = new System.IO.StreamWriter(this.path))
                {

                    searchResults.ForEach(r =>
                    {
                        stream.WriteLine(r);
                    });

                    stream.Close();
                    stream.Dispose();
                }
            }
            catch (System.IO.IOException ex)
            {
                System.Threading.Thread.Sleep(200);
                Save();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }
    }
}
