using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Beam.Abstractions;
using Beam.Models;

namespace Beam.Exports {
    public class PlainTextExporter : IExporter, IAsyncExporter {
        public PlainTextExporter(IDocument document) {
            Document = document;
        }

        public IDocument Document { get; }

        protected virtual string Convert() {
            return Document.ToString();
        }

        protected virtual Task<string> ConvertAsync() {
            return Task.FromResult(Document.ToString());
        }

        public virtual void Write(string path) {
            var text = Convert();
            if (!Directory.Exists(Path.GetDirectoryName(path)))
                throw new ArgumentException(S.M.FileDirectoryDoesNotExist, nameof(path));
            System.IO.File.WriteAllText(path, text, Encoding.Unicode);
        }

        public virtual async Task WriteAsync(string path) {
            var text = await ConvertAsync();
            if (!Directory.Exists(path))
                throw new ArgumentException(S.M.FileDirectoryDoesNotExist, nameof(path));
            await System.IO.File.WriteAllTextAsync(path, text);
        }
    }
}
