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

namespace CurrencyExchangeLib
{
    public class CurrencyExchange
    {
        private string[] _vendors = new string[] {};
        private IVendorRateWebService _vendorRateService;

        public CurrencyExchange(IVendorRateWebService vendorRateService = null)
        {
            _vendorRateService = vendorRateService;
        }

        internal double MarkDown(double amount)
        {
            return amount * 0.98;
        }

        public Tuple<String, double> GetRate(string currency)
        {
            var rate = new Tuple<string, double>("", 0.0);

            foreach (var vendor in _vendors)
            {
                try
                {
                    var adjustedRate = MarkDown(_vendorRateService.GetExchange(vendor, currency));
                    if (rate.Item2 < adjustedRate)
                    {
                        rate = new Tuple<string, double>(vendor, adjustedRate);
                    }
                }
                catch (ApplicationException ex)
                {
                    var message = String.Format("Vendor service reported error at {0}: for Vendor {1}: {2}", 
                        DateTime.Now, vendor, ex.Message);
                    System.IO.File.WriteAllText("errors.log", message);
                }
            }

            return rate;
        }

        internal void SetVendors(params string[] vendors)
        {
            _vendors = vendors;
        }
    }
}
