using System.Drawing.Imaging;
using System.Text;

namespace Launcheroid
{
    public class LRD4
    {
        public class Item
        {
            public string Name { get; set; }
            public string Path { get; set; }
            public string Arguments { get; set; }
            public string InitialFolder { get; set; }
            public Image Image { get; set; }
        }

        public LRD4()
        {
            Reset();
        }

        public void Reset()
        {
            NavigationRoot = null;
            IconSize = 48;
            IconSpacing = 5;
            IconMargin = 7;
            Items.Clear();
        }

        #region Load/Save
        /// <summary>
        /// Load all information from file
        /// </summary>
        /// <param name="path"></param>
        public void Load(string path)
        {
            if (!File.Exists(path))
                return;
            Reset();
        }

        /// <summary>
        /// Save the current information to a file
        /// </summary>
        /// <param name="path"></param>
        public void Save(string path)
        {
            using FileStream fs = new(path, FileMode.OpenOrCreate, FileAccess.Write);
            using StreamWriter sw = new(fs, Encoding.UTF8);
            sw.WriteLine("// Launcheroid4 file (Text mode) Autogenerated by Lancheroid on " + DateTime.Now.ToString());
            sw.WriteLine("// Launcheroid4 by XWolfOverride in 2025");
            sw.WriteLine();
            // Create JSON version
            foreach (Item i in Items)
            {
                sw.WriteLine("[Item]");
                sw.Write("name:"); sw.WriteLine(i.Name);
                sw.Write("path:"); sw.WriteLine(i.Path);
                sw.Write("idir:"); sw.WriteLine(i.InitialFolder);
                sw.Write("args:"); sw.WriteLine(i.Arguments);
                sw.Write("icon:"); sw.WriteLine(Image64(i.Image));
                sw.WriteLine("");
            }
            sw.WriteLine("[Settings]");
            sw.Write("root:"); sw.WriteLine(NavigationRoot);
            sw.Write("size:"); sw.WriteLine(IconSize);
            sw.Write("spac:"); sw.WriteLine(IconSpacing);
            sw.Write("marg:"); sw.WriteLine(IconMargin);
        }

        private string Image64(Image i)
        {
            if (i == null)
                return "";
            MemoryStream ms = new MemoryStream();
            i.Save(ms, ImageFormat.Png);
            ms.Close();
            byte[] data = ms.GetBuffer();
            return Convert.ToBase64String(data);
        }
        #endregion

        /// <summary>
        /// Navigation root for navigation panel (if not null)<br/>
        /// use '/' for root
        /// </summary>
        public string NavigationRoot { get; set; }

        /// <summary>
        /// Size (in pixels) of icons in screen
        /// </summary>
        public int IconSize { get; set; }

        /// <summary>
        /// Size (in pixels) of spacing between icons
        /// </summary>
        public int IconSpacing { get; set; }

        /// <summary>
        /// Size (in pixels) of between icons and window frame
        /// </summary>
        public int IconMargin { get; set; }

        /// <summary>
        /// List of items in Launcher 
        /// </summary>
        public List<Item> Items { get; } = new List<Item>();
    }
}
