using ZuluTradingSampleApp.Configuration;
using ZuluTradingSampleApp.Model;
using ZuluTradingSampleApp.Sender;

namespace ZuluTradingSampleApp.Trading
{
    public class ZuluTradingGatewayImpl : IZuluTradingGateway
    {
        #region Properties
        private readonly ZuluTradeAuthenticationInfo _authenticationInfo;
        private readonly ZuluTradingServerConfig _config;
        private readonly IZuluTradingRequestGenerator _generator = new ZuluTradingRequestGeneratorImpl();
        #endregion

        #region Constructor
        public ZuluTradingGatewayImpl(ZuluTradeAuthenticationInfo authenticationInfo, ZuluTradingServerConfig config)
        {
            _authenticationInfo = authenticationInfo;
            _config = config;
        }
        #endregion

        #region IZuluTradingGateway
        public NewMarketTradeResponseMessage OpenMarket(string currencyName, double lots, bool buy, double requestedPrice,
            string uniqueId)
        {
            return TradingResquestsSender.SendRequest<NewMarketTradeResponseMessage>(_config, _authenticationInfo,
                _generator.GetOpenMarketRequest(currencyName, lots, buy, requestedPrice, uniqueId));
        }

        public NewOrderResponseMessage OpenOrder(string currencyName, double lots, bool buy, double requestedPrice, string uniqueId)
        {
            return TradingResquestsSender.SendRequest<NewOrderResponseMessage>(_config, _authenticationInfo,
                _generator.GetOpenOrderRequest(currencyName, lots, buy, requestedPrice, uniqueId));
        }

        public OrderCanceledResponseMessage CancelOrder(string currencyName, double lots, bool buy, string uniqueId)
        {
            return TradingResquestsSender.SendRequest<OrderCanceledResponseMessage>(_config, _authenticationInfo,
                _generator.GetCancelOrderRequest(currencyName, lots, buy, uniqueId));
        }

        public TradeClosedResponseMessage CloseMarket(string currencyName, double lots, bool buy, string uniqueId,
            double requestedPrice)
        {
            return TradingResquestsSender.SendRequest<TradeClosedResponseMessage>(_config, _authenticationInfo,
                _generator.GetCloseMarketRequest(currencyName, lots, buy, requestedPrice, uniqueId));
        }

        public UpdateValueResponseMessage UpdateStop(string currencyName, bool buy, double lots, string uniqueId, double stopValue)
        {
            return TradingResquestsSender.SendRequest<UpdateValueResponseMessage>(_config, _authenticationInfo,
                _generator.GetUpdateStopRequest(currencyName, lots, buy, stopValue, uniqueId));
        }

        public UpdateTrailingStopValueResponseMessage UpdateTrailingStop(string currencyName, bool buy, double lots, string uniqueId, int trailingStopValue)
        {
            return TradingResquestsSender.SendRequest<UpdateTrailingStopValueResponseMessage>(_config, _authenticationInfo,
                _generator.GetUpdateTrailingStopRequest(currencyName, buy, lots, uniqueId, trailingStopValue));
        }

        public UpdateTrailingStopValueResponseMessage UpdateTrailingStopCondition(string currencyName, bool buy, double lots, string uniqueId, int trailingStopValue, int trailingStopConditionPips)
        {
            return TradingResquestsSender.SendRequest<UpdateTrailingStopValueResponseMessage>(_config, _authenticationInfo,
                _generator.GetUpdateTrailingStopConditionRequest(currencyName, buy, lots, uniqueId, trailingStopValue, trailingStopConditionPips));
        }

        public UpdateValueResponseMessage UpdateLimit(string currencyName, bool buy, double lots, string uniqueId, double limitValue)
        {
            return TradingResquestsSender.SendRequest<UpdateValueResponseMessage>(_config, _authenticationInfo,
                _generator.GetUpdateLimitRequest(currencyName, lots, buy, limitValue, uniqueId));
        }

        public UpdateValueResponseMessage UpdateEntryRate(string currencyName, bool buy, double lots, string uniqueId,
            double entryValue)
        {
            return TradingResquestsSender.SendRequest<UpdateValueResponseMessage>(_config, _authenticationInfo,
                _generator.GetUpdateEntryRateRequest(currencyName, lots, buy, entryValue, uniqueId));
        }

        public OpenTradesResponseMessage GetOpenTrades()
        {
            return TradingResquestsSender.SendRequest<OpenTradesResponseMessage>(_config, _authenticationInfo,
                _generator.GetOpenTradesRequest());
        }

        public TradingConfigurationResponseMessage GetTradingConfiguration()
        {
            return TradingResquestsSender.SendRequest<TradingConfigurationResponseMessage>(_config, _authenticationInfo,
                _generator.GetTradingConfigurationRequest());
        }

        public ProviderStatisticsResponseMessage GetProviderStatistics()
        {
            return TradingResquestsSender.SendRequest<ProviderStatisticsResponseMessage>(_config, _authenticationInfo,
                _generator.GetProviderStatisticsRequest());
        }

        public ClosedTradesResponseMessage GetHistory(string startDate, string endDate)
        {
            return TradingResquestsSender.SendRequest<ClosedTradesResponseMessage>(_config, _authenticationInfo,
                _generator.GetHistoryRequest(startDate, endDate));
        }
        #endregion
    }
}
