using System;

namespace ZionVault.Util
{
    public class Downloader
    {
        private bool CheckURLValid(string source) => Uri.TryCreate(source, UriKind.Absolute, out Uri uriResult) && uriResult.Scheme == Uri.UriSchemeHttp;

        public void DownloadFile(string FileUri, string DestinationFolder, string FileName)
        {
            Console.WriteLine(String.Format("Baixando arquivo {0}", FileUri));

            Util.IO.CreateDirectory(DestinationFolder);

            try
            {
                if (!System.IO.File.Exists(DestinationFolder + FileName))
                {
                    if (CheckURLValid(FileUri))
                    {
                        System.Net.WebClient webClient = new System.Net.WebClient();
                        webClient.DownloadFile(FileUri, DestinationFolder + FileName);
                    }
                    else
                    {
                        System.IO.File.Copy(FileUri, DestinationFolder + FileName, true);
                    }
                }
            }
            catch (Exception ex)
            {
                (new ZionVault.Logger.LogWindows()).error(
                    String.Format(@"Vault Downloader - {0}", ex.Message));
                throw ex;
            }
        }
    }
}
