using System;
using System.IO;
using Esendex.Rest.App.settings;
using RestSharp;

namespace Esendex.Rest.App.builders
{
    public class RequestBuilder
    {
        private Method _method;
        private string _uri { get; set; }
        private string _recipient { get; set; }
        private string _messageBody { get; set; }

        public static RequestBuilder Create()
        {
            return new RequestBuilder();
        }

        public RequestBuilder WithRecipient(string recipient)
        {
            _recipient = recipient;
            return this;
        }

        public RequestBuilder WithMessageBody(string messageBody)
        {
            _messageBody = messageBody;
            return this;
        }

        public RequestBuilder WithUri(string uri)
        {
            _uri = uri;
            return this;
        }

        public RequestBuilder WithMethod(Method method)
        {
            _method = method;
            return this;
        }

        public IRestRequest BuildSendRequest()
        {
            var messageXml = File.ReadAllText("Message.XML");
            var xmlToSend = String.Format(messageXml, AccountSettings.ACCOUNT_REFERENCE, _messageBody, _recipient);

            var request = new RestRequest(_uri, _method);
            request.AddParameter("application/xml", xmlToSend, ParameterType.RequestBody);

            return request;
        }

        public IRestRequest BuildGetRequest()
        {
            return new RestRequest(_uri, _method);
        }
    }
}