using Knoks.Core.Logic.Interfaces;
using Knoks.Framework.Document;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace Knoks.Core.Logic.Managers
{
    public class ImageManager : IImageManager, IUserImageManager
    {
        private IDocumentProcess _documentDao;

        public ImageManager(IDocumentProcess documentDao)
        {
            _documentDao = documentDao;
        }
        public async Task<string> SaveKnokImage(long userId, long knokId, string fileName, Stream srcFileStream)
        {
            if (string.IsNullOrWhiteSpace(fileName))
                throw new ArgumentException("File name is null or empty.", nameof(fileName));

            if (srcFileStream == null)
                throw new ArgumentException("Source file name is null.", nameof(srcFileStream));

            var relativePath = GetFileName(userId, knokId, fileName);
            string result = await _documentDao.SaveIndexed(srcFileStream, relativePath);
            return ReturnFileName(knokId, fileName, result);
        }

        private string GetFileName(long userId, long knokId, string fileName)
        {
            var str = System.IO.Path.GetFileName(fileName);
            return Path.Combine(userId.ToString(), "images", $"knok{knokId}_{str}");
        }
        private string ReturnFileName(long knokId, string fileName, string result)
        {
            var str = System.IO.Path.GetFileName(result).Substring($"knok{knokId}_".Length);
            return Path.Combine(Path.GetDirectoryName(fileName), str).Replace("\\", "/");
        }

        public FileStream LoadKnokImage(long userId, long knokId, string fileName, out DateTimeOffset modified)
        {
            string path = GetFileName(userId, knokId, fileName);
            var result = _documentDao.LoadFileStream(path);
            if (result != null) {
                modified = _documentDao.GetFileModified(path);
                return result;
            }
            return null;
        }

        public byte[] LoadKnokImageBytes(long userId, long knokId, string fileName)
        {
            var relativePath = GetFileName(userId, knokId, fileName);
            return _documentDao.LoadFileBytes(relativePath);
        }

        public async Task SaveAvatar(long userId, Stream srcFileStream)
        {
            if (srcFileStream == null)
                throw new ArgumentException("Source file name is null.", nameof(srcFileStream));

            await _documentDao.Save(srcFileStream, $"{userId}/avatar.png", true);
        }
        public FileStream LoadAvatar(long userId, out DateTimeOffset modified)
        {
            string path = $"{userId}/avatar.png";
            FileStream result;
            //try
            //{
            result = _documentDao.LoadFileStream(path); //FileNotFoundException
            //}
            //catch (FileNotFoundException) {
            //    path = "default_avatar.png";
            //    result = _documentDao.LoadFileStream(path);
            //}
            if (result != null)
            {
                modified = _documentDao.GetFileModified(path);
                return result;
            }
            return null;
        }
    }
}