// Copyright 2018 RED Software, LLC. All Rights Reserved.

using System.Runtime.InteropServices;
using System.Text;

namespace IgniteEngine.IO
{
    /// <summary>
    /// Class to read and manipulate .ini files.
    /// </summary>
    public class INIFile
    {
        /// <summary>
        /// The file's path.
        /// </summary>
        private readonly string path;

        /// <summary>
        /// The section to read from.
        /// </summary>
        private readonly string section;

        /// <summary>
        /// Creates a new instance of the <see cref="INIFile"/> class.
        /// </summary>
        /// <param name="path">The path of the file.</param>
        /// <param name="section">The section to read from.</param>
        public INIFile(string path, string section = null)
        {
            this.path = path;
            this.section = section;
        }

        /// <summary>
        /// Gets information from the file.
        /// </summary>
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        /// <summary>
        /// Writes information to the file.
        /// </summary>
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        /// <summary>
        /// Gets a byte from the file.
        /// </summary>
        public byte GetByte(string key)
        {
            return GetByte(section, key);
        }

        /// <summary>
        /// Gets a byte from the file.
        /// </summary>
        public byte GetByte(string section, string key)
        {
            return byte.Parse(GetValue(section, key));
        }

        /// <summary>
        /// Gets a 16-bit integer from the file.
        /// </summary>
        public short GetInt16(string key)
        {
            return GetInt16(section, key);
        }

        /// <summary>
        /// Gets a 16-bit integer from the file.
        /// </summary>
        public short GetInt16(string section, string key)
        {
            return short.Parse(GetValue(section, key));
        }

        /// <summary>
        /// Gets a 32-bit integer from the file.
        /// </summary>
        public int GetInt32(string key)
        {
            return GetInt32(section, key);
        }

        /// <summary>
        /// Gets a 32-bit integer from the file.
        /// </summary>
        public int GetInt32(string section, string key)
        {
            return int.Parse(GetValue(section, key));
        }

        /// <summary>
        /// Gets a 64-bit integer from the file.
        /// </summary>
        public long GetInt64(string key)
        {
            return GetInt64(section, key);
        }

        /// <summary>
        /// Gets a 64-bit integer from the file.
        /// </summary>
        public long GetInt64(string section, string key)
        {
            return long.Parse(GetValue(section, key));
        }

        /// <summary>
        /// Gets a string from the file.
        /// </summary>
        public string GetString(string key)
        {
            return GetString(section, key);
        }

        /// <summary>
        /// Gets a string from the file.
        /// </summary>
        public string GetString(string section, string key)
        {
            return GetValue(section, key);
        }

        /// <summary>
        /// Sets a value in the file.
        /// </summary>
        /// <param name="section">The value's section.</param>
        /// <param name="key">The value's key.</param>
        /// <param name="value">The value.</param>
        public void SetValue(string section, string key, string value)
        {
            WritePrivateProfileString(section, key, value, path);
        }

        /// <summary>
        /// Gets a value from the file.
        /// </summary>
        /// <param name="section">The value's section.</param>
        /// <param name="key">The value's key.</param>
        private string GetValue(string section, string key)
        {
            var builder = new StringBuilder(256);
            
            GetPrivateProfileString(section, key, "" , builder, 256, path);

            return builder.ToString();
        }
    }
}
