using System;
using CurrencyExchangeLib;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CurrencyExchangeTestLib
{
    [TestClass]
    public class CurrencyExchangeTest
    {
        const double DELTA = 0.000001;

        [TestMethod]
        public void Canary()
        {
            Assert.IsTrue(true);
        }

        [TestMethod]
        public void MarkDown()
        {
            var currencyExchange = new CurrencyExchange();
            Assert.AreEqual(98.0, currencyExchange.MarkDown(100), DELTA);
        }

        [TestMethod]
        public void GetRateNoVendors()
        {
            var currencyExchange = new CurrencyExchange();
            var result = new Tuple<String, double>("", 0.0);
            Assert.AreEqual(result, currencyExchange.GetRate("GBP"));
        }

        [TestMethod]
        public void GetRateWithOneVendor()
        {
            var vendorRateService = new CurrencyExchangeLib.Fakes.StubIVendorRateWebService()
            {
                GetExchangeStringString = (vendor, currency) => 100.0
            };

            var currencyExchange = new CurrencyExchange(vendorRateService);
            var result = new Tuple<string, double>("vendor1", 98.0);
            currencyExchange.SetVendor("vendor1");
            Assert.AreEqual(result, currencyExchange.GetRate("GBP"));
        }

        [TestMethod]
        public void GetRateWithOneVendorButServiceError()
        {
            var vendorRateService = new CurrencyExchangeLib.Fakes.StubIVendorRateWebService()
            {
                GetExchangeStringString = (vendor, currency) => { throw new ApplicationException("Error processing");} 
            };

            var currencyExchange = new CurrencyExchange(vendorRateService);
            var result = new Tuple<string, double>("", 0.0);
            currencyExchange.SetVendor("vendor1");
            Assert.AreEqual(result, currencyExchange.GetRate("GBP"));
        }
    }
}
