/*
 * @license Private Source License v1.0.0
 * @copyright 2023 Reinhardt Weyers
 *
 * @author Reinhardt Weyers
 *
 * @version 0.1.0
 * 
 * Created On: 04/12/25
 * Last edited: 04/12/25
 */

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace RetroDev.Networking
{
    /// <summary>
    /// Provides methods for encoding content into various formats.
    /// </summary>
    public class Encoder
    {
        /// <summary>
        /// Encodes a dictionary of form fields into a string based on the specified content type.
        /// </summary>
        /// <param name="formFields">A dictionary containing the fields to be encoded.</param>
        /// <param name="contentType">The content type for encoding (e.g., FormUrlEncoded, Json, PlainText).</param>
        /// <returns>A string representation of the encoded content.</returns>
        /// <exception cref="ArgumentException">Thrown when the content type is unsupported.</exception>
        public static string EncodeContent(Dictionary<string, object> formFields, ContentType contentType)
        {
            switch (contentType)
            {
                case ContentType.FormUrlEncoded:
                    return EncodeFormUrlEncoded(formFields);
                case ContentType.Json:
                    return EncodeJson(formFields);
                case ContentType.PlainText:
                    return EncodePlainText(formFields);
                default:
                    throw new ArgumentException("Unsupported content type");
            }
        }

        /// <summary>
        /// Encodes form fields into a URL-encoded string.
        /// </summary>
        /// <param name="formFields">A dictionary containing the fields to be encoded.</param>
        /// <returns>A URL-encoded string representation of the form fields.</returns>
        private static string EncodeFormUrlEncoded(Dictionary<string, object> formFields)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var field in formFields)
            {
                if (sb.Length > 0)
                    sb.Append("&");

                sb.Append(Uri.EscapeDataString(field.Key) + "=" + Uri.EscapeDataString(field.Value.ToString()));
            }
            return sb.ToString();
        }

        /// <summary>
        /// Encodes form fields into a JSON string.
        /// </summary>
        /// <param name="formFields">A dictionary containing the fields to be encoded.</param>
        /// <returns>A JSON string representation of the form fields.</returns>
        private static string EncodeJson(Dictionary<string, object> formFields)
        {
            // Serialize the dictionary to a JSON string using Newtonsoft.Json
            return JsonConvert.SerializeObject(formFields);
        }

        /// <summary>
        /// Encodes form fields into a plain text string.
        /// </summary>
        /// <param name="formFields">A dictionary containing the fields to be encoded.</param>
        /// <returns>A plain text representation of the form fields.</returns>
        private static string EncodePlainText(Dictionary<string, object> formFields)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var field in formFields)
            {
                sb.Append(field.Key + ": " + field.Value.ToString() + "\n");
            }
            return sb.ToString().Trim(); // Trim to remove trailing newline
        }
    }
}
