﻿using System;
using System.Diagnostics;
using System.IO;
using Debug = UnityEngine.Debug;

namespace Toast.SmartDownloader.Example
{
    public static class FileUtil
    {
        public static void DeleteDirectory(string path)
        {
            if (!Directory.Exists(path))
            {
                Debug.LogErrorFormat("Not found directory : {0}", path);
                return;
            }

            try
            {
                var info = new DirectoryInfo(path);
                if (info != null)
                    info.Delete(true);
                Debug.LogFormat("Success to delete directory : {0}", path);
            }
            catch (Exception ex)
            {
                Debug.LogException(ex);
            }
        }

        public static bool DeleteFilesInDownloadPath()
        {
            try
            {
                var directories = Directory.GetDirectories(SmartDlExampleConfiguration.DownloadPath);
                foreach (var dir in directories)
                {
                    Directory.Delete(dir, true);
                }
                var files = Directory.GetFiles(SmartDlExampleConfiguration.DownloadPath, "*");
                foreach (var file in files)
                {
                    File.Delete(file);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

    }
}