// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.IO;
using System.Text.Json;
using Microsoft.PowerToys.Run.Plugin.WindowsTerminal;
using Microsoft.PowerToys.Run.Plugin.WindowsTerminal.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Microsoft.Plugin.WindowsTerminal.UnitTests
{
    [TestClass]
    public class TerminalHelperTests
    {
        [DataTestMethod]
        [DataRow("Windows PowerShell", true, "--window 0 nt --profile \"Windows PowerShell\"")]
        [DataRow("Windows PowerShell", false, "--profile \"Windows PowerShell\"")]
        public void ArgumentsTest(string profile, bool openNewTab, string expectedArguments)
        {
            var arguments = TerminalHelper.GetArguments(profile, openNewTab);
            Assert.AreEqual(arguments, expectedArguments);
        }

        [TestMethod]
        public void ParseSettingsTest()
        {
            var terminal = new TerminalPackage(string.Empty, new Version(), string.Empty, string.Empty, string.Empty);

            var settingsPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "settings.json");
            var settings = File.ReadAllText(settingsPath);
            var profiles = TerminalHelper.ParseSettings(terminal, settings);

            Assert.AreEqual(profiles.Count, 4);
        }

        [DataTestMethod]
        [DataRow(
            @"{""guid"":""{61c54bbd-c2c6-5271-96e7-009a87ff44bf}"",""name"":""Windows PowerShell"",""commandline"":""powershell.exe"",""hidden"":true}",
            "61c54bbd-c2c6-5271-96e7-009a87ff44bf",
            "Windows PowerShell",
            true)]
        [DataRow(
            @"{""name"":""Windows PowerShell"",""commandline"":""powershell.exe"",""hidden"":false}",
            null,
            "Windows PowerShell",
            false)]
        public void ParseProfilesTest(string json, string identifier, string name, bool hidden)
        {
            var profileElement = JsonDocument.Parse(json).RootElement;
            var terminal = new TerminalPackage(string.Empty, new Version(), string.Empty, string.Empty, string.Empty);
            var profile = TerminalHelper.ParseProfile(terminal, profileElement);

            var expectedIdentifier = identifier != null ? new Guid(identifier) : null as Guid?;
            Assert.AreEqual(profile.Terminal, terminal);
            Assert.AreEqual(profile.Identifier, expectedIdentifier);
            Assert.AreEqual(profile.Name, name);
            Assert.AreEqual(profile.Hidden, hidden);
        }
    }
}
