﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

namespace VioletGrass.ProjectModel.ChangeOrders
{
    public class AddOrganizationMemberTest
    {
        [Fact]
        public void ApplySimple()
        {
            var owner = Sample.ApplyOrganization(new AddOrganizationMember(Sample.FreePersonName, OrganizationPermission.Read, false, Sample.PersonNameOfAnOrganizationOwner));

            Assert.NotNull(owner);
            Assert.Equal(Sample.NrOfOrgProjects, owner.Projects.Length);
            Assert.Equal(Sample.NrOfOrgMembers + 1, owner.Members.Length);
            var member = owner.Members[Sample.NrOfOrgMembers];
            Assert.Equal(Sample.FreePersonName, member.PersonName);
            Assert.False(member.MembershipIsPublic);
            Assert.Equal(OrganizationPermission.Read, member.Permission);
        }
        [Fact]
        public void ApplyModifyExisting()
        {
            var owner = Sample.ApplyOrganization(new AddOrganizationMember(Sample.PersonNameOfAnOrganizationOwner, OrganizationPermission.Write, false, Sample.PersonNameOfAnOrganizationOwner));

            Assert.NotNull(owner);
            Assert.Equal(Sample.NrOfOrgProjects, owner.Projects.Length);
            Assert.Equal(Sample.NrOfOrgMembers, owner.Members.Length);
            var member = owner.Members.First(x => x.PersonName == Sample.PersonNameOfAnOrganizationOwner);
            Assert.Equal(Sample.PersonNameOfAnOrganizationOwner, member.PersonName);
            Assert.False(member.MembershipIsPublic);
            Assert.Equal(OrganizationPermission.Write, member.Permission);
        }
        [Fact]
        public void ApplyThrowsNotOwner()
        {
            Assert.Throws<InvalidOperationException>(() =>
                Sample.ApplyOrganization(new AddOrganizationMember(Sample.FreePersonName, OrganizationPermission.Read, false, Sample.PersonNameOfAnNormalMember))
            );
        }
    }
}
