using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Beam.Data;

namespace Beam.Tests {
    public class SouceLinkBuilder {
        [Fact]
        public void ShouldConstruct_NoErrors() {
            _ = new LinkBuilder("example.com");
        }
        [Fact]
        public void ShouldBuild_NoErrors() {
            _ = new LinkBuilder("example.com").Build();
        }
        [Fact]
        public void ShouldBuild_Correctly() {
            var k = new LinkBuilder("example.com");
            Assert.NotNull(k);
            var link = k.Build();
            Assert.Equal("https://example.com/", link); // trailing slash for RFC standardization stuff
        }
        [Fact]
        public void ShouldBuild_SegmentAddsCorrectly() {
            var k = new LinkBuilder("example.com");
            Assert.NotNull(k);
            k.AddSegment("folder1");
            var link = k.Build();
            Assert.Equal("https://example.com/folder1", link);
        }
        [Fact]
        public void ShouldThrow_EmptySegmentsDisallowed() {
            var k = new LinkBuilder("example.com");
            Assert.NotNull(k);
            Assert.Throws<ArgumentException>(() => {
                k.AddSegment("");
            });
        }
        [Theory]
        [InlineData("folder1", "folder2", "folder3")]
        [InlineData("f1", "f5", "f6")]
        public void ShouldBuild_MultipleSegmentsCorrect(params string[] segments) {
            var k = new LinkBuilder("example.com");
            Assert.NotNull(k);
            foreach (var segment in segments) {
                k.AddSegment(segment);
            }

            StringBuilder builder = new();
            builder.Append("https://example.com/");
            foreach(var segment in segments) {
                builder.Append(segment + "/");
            }

            // Remove trailing slash
            builder.Remove(builder.Length - 1, 1);

            var link = k.Build();

            Assert.Equal(builder.ToString(), link);
        }
        [Fact]
        public void ShouldBuild_SingleParameterCorrect() {
            var k = new LinkBuilder("example.com");
            Assert.NotNull(k);
            k.AddSegment("f1");
            k.AddParameters(0, "?q=");
            var link = k.Build("foo");
            Assert.Equal("https://example.com/f1?q=foo", link);
        }
        [Fact]
        public void ShouldBuild_MultiParameterCorrect() {
            var k = new LinkBuilder("example.com");
            Assert.NotNull(k);
            k.AddSegment("f1");
            k.AddParameters(0, "?q=", "?m=");
            var link = k.Build("foo", "bar");
            Assert.Equal("https://example.com/f1?q=foo?m=bar", link);
        }
        [Fact]
        public void ShouldBuild_MultiParameterCorrectWithSuffix() {
            var k = new LinkBuilder("example.com");
            Assert.NotNull(k);
            k.AddSegment("f1", "&");
            k.AddParameters(0, "?q=", "?m=");
            var link = k.Build("foo", "bar");
            Assert.Equal("https://example.com/f1?q=foo&?m=bar", link);
        }

    }
}
