using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Katteker
{
    /// <summary>
    /// Configuration of a Katteker-Deployment
    /// </summary>
    [Serializable]
    public class KattekerConfig
    {
        /// <summary>
        /// Publish path
        /// </summary>
        public string Publish { get; set; }

        /// <summary>
        /// Name of the changelog file.
        /// </summary>
        public string Changelog { get; set; }

        /// <summary>
        /// Channel of the application. (Alpha, Beta and so on...)
        /// </summary>
        public string Channel { get; set; }

        /// <summary>
        /// Read file and deserialize content.
        /// </summary>
        /// <param name="path">path of the file.</param>
        /// <returns>this object</returns>
        /// <exception cref="FileNotFoundException"></exception>
        public static KattekerConfig ReadFromFile(string path)
        {
            if (!File.Exists(path)) throw new FileNotFoundException();
            var serializer = new BinaryFormatter();
            using (var fileStream = File.OpenRead(path))
            {
                var obj = serializer.Deserialize(fileStream);
                return (KattekerConfig)obj;
            }
        }

        /// <summary>
        /// Serialize object and write to file.
        /// </summary>
        /// <param name="path">path of the file.</param>
        public void WriteToFile(string path)
        {
            if (File.Exists(path)) File.Delete(path);
            using (var fileStream = File.OpenWrite(path))
            {
                var serializer = new BinaryFormatter();
                serializer.Serialize(fileStream, this);
            }
        }
    }
}