using System;
using StubCore.Plugins.Logging;
using System.Collections.Generic;

namespace StubCore.Plugins.FileOperations
{
    public class FileManager
    {
        private readonly string _dataFolder = "Data";
        private readonly string _filesFolder = "Files";

        private readonly FileDataOperations _file;
        private readonly ContextLogger _logger = new ContextLogger();

        public FileManager()
        {
            _file = new FileDataOperations();
        }

        public bool ValidateDatabaseFiles(Dictionary<string, string> _folderPaths)
        {
            var isValid = ValidateRootFolders();

            if (isValid)
            {
                foreach(KeyValuePair<string, string> _key in _folderPaths)
                {

                    var folderPath = _dataFolder + "/" + _key.Key;

                    if (_file.CreateDirectory(folderPath))
                    {
                        var filePath = folderPath + "/" + _key.Value + ".json";

                        if (!_file.FileExists(filePath))
                        {
                            var saved = _file.SaveFile(filePath, String.Empty);

                            if (!saved)
                            {
                                isValid = false;
                                _logger.FileWasntSaved(filePath);
                            }
                        }
                        
                    }
                }
            }

            return isValid;
        }

        public bool ValidateRootFolders()
        {
            var isValid = false;

            if (_file.CreateDirectory(_dataFolder) && _file.CreateDirectory(_filesFolder))
            {
                isValid = true;
            }

            return isValid;
        }
    }
}
