//
// FileSystemDestination.cs
//
// Author:
//       Carsten Sonne Larsen <cs@innolan.dk>
//
// Copyright (c) 2016 Carsten Sonne Larsen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.IO;
using Ntp.Analyzer.Localize;
using Ntp.Interop;
using Ntp.Log;

namespace Ntp.Analyzer.Export
{
    public abstract class FileSystemDestination : StreamDestination
    {
        public static uint? FileUserId = null;
        public static uint? FileGroupId = null;
        public static uint FileMask = 644;

        protected void TestFile(string file, string content)
        {
            if (File.Exists(file))
                File.Delete(file);

            File.WriteAllText(file, content);
            File.Delete(file);
        }

        protected void PrepareFile(string file, LogBase log)
        {
            string created = null;
            try
            {
                created = DirectoryCommand.CreateRecursiveFromFile(file);
            }
            catch (Exception e)
            {
                log.WriteLine(String.Format(
                    LogMessage.DestinationPathError, file),
                    Severity.Error);
                log.WriteLine(e, Severity.Trace);
            }

            if (created != null)
            {
                log.WriteLine(String.Format(
                    LogMessage.DestinationNewDirectory, created),
                    Severity.Notice);
            }

            if (File.Exists(file))
                File.Delete(file);
        }

        protected void WriteFile(Stream stream, string file, LogBase log)
        {
            try
            {
                using (var fileStream = new FileStream(file, FileMode.CreateNew))
                    stream.CopyTo(fileStream);
            }
            catch (Exception e)
            {
                log.WriteLine(String.Format(
                    LogMessage.DestinationFileError, file),
                    Severity.Error);
                log.WriteLine(e, Severity.Trace);
            }
        }

        protected void ApplyPermissions(string file, LogBase log)
        {
            if (FileUserId != null)
            {
                try
                {
                    if (!Permission.ChangeFileOwner(file, FileUserId.Value, FileGroupId))
                    {
                        log.WriteLine(String.Format(
                            LogMessage.DestinationOwnerError, file),
                            Severity.Warn);
                    }
                }
                catch (Exception e)
                {
                    log.WriteLine(String.Format(
                        LogMessage.DestinationOwnerError, file),
                        Severity.Error);
                    log.WriteLine(e, Severity.Trace);
                }
            }

            try
            {
                if (!Permission.ChangeFileMode(file, FileMask))
                {
                    log.WriteLine(String.Format(
                        LogMessage.DestinationPermissonError, file),
                        Severity.Warn);
                }
            }
            catch (Exception e)
            {
                log.WriteLine(String.Format(
                    LogMessage.DestinationPermissonError, file),
                    Severity.Error);
                log.WriteLine(e, Severity.Trace);
            }
        }
    }
}