// The CMLeonOS Project (https://github.com/Leonmmcoset/CMLeonOS)
// Copyright (C) 2025-present LeonOS 2 Developer Team
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

using System;
using System.Drawing;
using System.IO;
using System.Text;

// Modified from https://github.com/nifanfa/Cosmos-DrawString

namespace CMLeonOS.Gui
{
    internal static class Asc16
    {
        internal static string Asc16Base64 = "[encoded data, 5464 characters]";
        internal static MemoryStream Asc16FontStream = new MemoryStream(Convert.FromBase64String(Asc16Base64));

        internal static void DrawAsciiString(string str, Color color, int x, int y, int[] buffer, int bufferWidth, int bufferHeight)
        {
            for (int c = 0; c < str.Length; c++)
            {
                int offset = (Encoding.ASCII.GetBytes(str[c].ToString())[0] & 0xFF) * 16;
                Asc16FontStream.Seek(offset, SeekOrigin.Begin);
                byte[] fontbuf = new byte[16];
                Asc16FontStream.Read(fontbuf, 0, fontbuf.Length);

                for (int i = 0; i < 16; i++)
                {
                    for (int j = 0; j < 8; j++)
                    {
                        if ((fontbuf[i] & (0x80 >> j)) != 0)
                        {
                            int index = x + ((i + y) * bufferWidth) + j + (c * 8);
                            if (index < 0 || index > buffer.Length) continue;
                            buffer[index] = color.ToArgb();
                        }
                    }
                }
            }
        }
    }
}