using RioJoy.Core.Profiles;
using Xunit;

namespace RioJoy.Core.Tests.Profiles;

public class RioIniImporterTests
{
    private const string Sample = """
        [Plasma]
        Greeting = RIOvJoy2 v.03

        [Desktop]
        File = C:\games\RIO\VWE2.bmp

        [JoyStick]
        invertX = false
        invertY = true
        enableZR = false

        ; comment line
        [Buttons]
        RIO00 = 0x8049
        RIO01 = 0x805A
        RIO16 = 0x0
        RIO50 = 33353
        """;

    [Fact]
    public void Imports_ButtonsAsAddresses()
    {
        RioProfile p = RioIniImporter.Import(Sample, "VWE2");

        Assert.Equal("VWE2", p.Name);
        Assert.Equal(0x8049, p.Buttons[0x00]);
        Assert.Equal(0x805A, p.Buttons[0x01]);
        Assert.Equal(33353, p.Buttons[0x50]); // keypad-0 address, decimal value
        Assert.False(p.Buttons.ContainsKey(0x16)); // 0x0 entries are skipped
    }

    [Fact]
    public void Imports_PlasmaAndCalibration()
    {
        RioProfile p = RioIniImporter.Import(Sample);

        Assert.Equal("RIOvJoy2 v.03", p.PlasmaGreeting);
        Assert.Equal(@"C:\games\RIO\VWE2.bmp", p.WallpaperPath);
        Assert.False(p.Calibration.InvertX);
        Assert.True(p.Calibration.InvertY);
        Assert.False(p.Calibration.EnableZR);
    }

    [Fact]
    public void EnableZR_DefaultsTrue_WhenAbsent()
    {
        RioProfile p = RioIniImporter.Import("[JoyStick]\ninvertX = true");
        Assert.True(p.Calibration.EnableZR);
    }

    // The real gsheet "INI_V2" output: modifier-stacked words (0x8F26 =
    // lit+shift+ctrl+alt+ext + VK 0x26), keypad VK words (0x30), and RIO
    // command words (0x7000) must all survive import verbatim.
    private const string SheetSample = """
        ;RIO INI V2 built useing gsheet configurator's final value
        [Desktop]
        File=C:\games\RIO\defalt.bmp
        [Plasma]
        Greeting="Hello,"
        [JoyStick]
        invertX=0
        enableZR=1

        [Buttons]
        RIO04=0x8F26
        RIO50=0x30
        RIO60=0x7000
        RIO6F=0x700F
        """;

    [Fact]
    public void Imports_RealSheetOutput_PreservesRawWords()
    {
        RioProfile p = RioIniImporter.Import(SheetSample, "Sheet");

        Assert.Equal(0x8F26, p.Buttons[0x04]); // lit + all modifiers + extended
        Assert.Equal(0x30, p.Buttons[0x50]);   // keypad '0'
        Assert.Equal(0x7000, p.Buttons[0x60]); // RIO command word
        Assert.Equal(0x700F, p.Buttons[0x6F]); // last address in the table
        Assert.Equal(@"C:\games\RIO\defalt.bmp", p.WallpaperPath);
        Assert.Equal("Hello,", p.PlasmaGreeting); // surrounding quotes stripped
    }
}
