using System.Linq;
using Nop.Core.Domain.Common;
using Nop.Core.Domain.Customers;
using Nop.Tests;
using NUnit.Framework;

namespace Nop.Core.Tests.Domain.Customers
{
    [TestFixture]
    public class CustomerTests
    {
        [Test]
        public void New_customer_has_clear_password_type()
        {
            var customer = new Customer();
            customer.PasswordFormat.ShouldEqual(PasswordFormat.Clear);
        }

        [Test]
        public void Can_add_address()
        {
            var customer = new Customer();
            var address = new Address { Id = 1 };

            customer.AddAddress(address);

            customer.Addresses.Count.ShouldEqual(1);
            customer.Addresses.First().Id.ShouldEqual(1);
        }

        [Test]
        public void Can_not_add_duplicate_addresses()
        {
            var customer = new Customer();
            var address = new Address { Id = 1 };
            var address2 = new Address { Id = 2 };

            customer.AddAddress(address);
            customer.AddAddress(address); // should not add
            customer.AddAddress(address2);


            customer.Addresses.Count.ShouldEqual(2);
            var addresses = customer.Addresses.ToList();
            addresses[0].Id.ShouldEqual(1);
            addresses[1].Id.ShouldEqual(2);
        }

        [Test]
        public void Can_remove_address_assigned_as_billing_address()
        {
            var customer = new Customer();
            var address = new Address { Id = 1 };

            customer.AddAddress(address);
            customer.SetBillingAddress(address);

            customer.BillingAddress.ShouldBeTheSameAs(customer.Addresses.First());

            customer.RemoveAddress(address);
            customer.Addresses.Count.ShouldEqual(0);
            customer.BillingAddress.ShouldBeNull();
        }

        [Test]
        public void Can_add_rewardPointsHistoryEntry()
        {
            var customer = new Customer();
            customer.AddRewardPointsHistoryEntry(1, "Points for registration");

            customer.RewardPointsHistory.Count.ShouldEqual(1);
            customer.RewardPointsHistory.First().Points.ShouldEqual(1);
        }

        [Test]
        public void Can_get_rewardPointsHistoryBalance()
        {
            var customer = new Customer();
            customer.AddRewardPointsHistoryEntry(1, "Points for registration");
            //customer.AddRewardPointsHistoryEntry(3, "Points for registration");

            customer.GetRewardPointsBalance().ShouldEqual(1);
        }

        [Test]
        public void Can_get_add_remove_giftCardCouponCodes()
        {
            var customer = new Customer();
            customer.ApplyGiftCardCouponCode("code1");
            customer.ApplyGiftCardCouponCode("code2");
            customer.RemoveGiftCardCouponCode("code2");
            customer.ApplyGiftCardCouponCode("code3");

            var codes = customer.ParseAppliedGiftCardCouponCodes();
            codes.Length.ShouldEqual(2);
            codes[0].ShouldEqual("code1");
            codes[1].ShouldEqual("code3");
        }
        [Test]
        public void Can_not_add_duplicate_giftCardCouponCodes()
        {
            var customer = new Customer();
            customer.ApplyGiftCardCouponCode("code1");
            customer.ApplyGiftCardCouponCode("code2");
            customer.ApplyGiftCardCouponCode("code1");

            var codes = customer.ParseAppliedGiftCardCouponCodes();
            codes.Length.ShouldEqual(2);
            codes[0].ShouldEqual("code1");
            codes[1].ShouldEqual("code2");
        }
    }
}
