using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net.Http;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Net;
using System.Text.RegularExpressions;

namespace TrayCheckoutApi.Controllers
{
    public class TrayCheckout
    {
        private string message;
        private XmlDocument xmlDocument;
        private HttpStatusCode httpStatusCode;
        private string xmlResponse;
        private string generalError;
        private string validationError;


        public void setMessage(string msg)
        {
            message = msg;
        }

        public string getMessage()
        {
            return message;
        }

        public void setXml(XmlDocument x)
        {
            xmlDocument = x;
        }

        public XmlDocument getXml()
        {
            return xmlDocument;
        }

        private void setContent(string content)
        {
            xmlResponse = content;
        }

        public string getContent()
        {
            return xmlResponse;
        }

        private void setHttpCodeStatus(HttpStatusCode code)
        {
            httpStatusCode = code;
        }

        public HttpStatusCode getHttpStatusCode()
        {
            return httpStatusCode;
        }

        public bool sucessResponse()
        {
            XmlNodeList message_response = xmlDocument.GetElementsByTagName("message_response");
            XmlNode nodeMessage = message_response[0];
            setMessage(nodeMessage["message"].InnerText);
            if (getMessage().Equals("error"))
            {
                return false;
            }
            return true;
        }

        public string getGeneralError()
        {
            generalError = "";
            /* Generic Errors */
            XmlNodeList generalErrorList = xmlDocument.GetElementsByTagName("general_error");
            foreach (XmlNode pm in generalErrorList)
            {
                string code = pm["code"].InnerText;
                string msg = pm["message"].InnerText;
                generalError += "\n<general_error>";
                generalError += "\nCode:" + code;
                generalError += "\nMessage:" + msg;
            }

            XmlNodeList errorList = xmlDocument.GetElementsByTagName("error");
            foreach (XmlNode pm in errorList)
            {
                string code = pm["code"].InnerText;
                string msg = pm["message"].InnerText;
                generalError += "\n<error>";
                generalError += "\nCode:" + code;
                generalError += "\nMessage:" + msg;
            }
            return generalError;
        }

        public string getValidationError()
        {
            /* Especific Errors - Fields */
            XmlNodeList validationErrorList = xmlDocument.GetElementsByTagName("validation_error");
            foreach (XmlNode pm in validationErrorList)
            {
                string code = pm["code"].InnerText;
                string msg = pm["message"].InnerText;
                string field = pm["field"].InnerText;
                string messageComplete = pm["message_complete"].InnerText;
                validationError += "\n<validation_error>";
                validationError += "\nCode:" + code;
                validationError += "\nMessage:" + msg;
                validationError += "\nfield:" + field;
                validationError += "\nMessageComplete:" + messageComplete;
            }
            return validationError;
        }

        public void RequestTrayCheckout(FormUrlEncodedContent content, string url)
        {
            setContent("");
            setHttpCodeStatus(HttpStatusCode.NoContent);
            using (var client = new HttpClient())
            {
                try
                {
                    var result = client.PostAsync(url, content).Result;
                    setHttpCodeStatus(result.StatusCode);
                    if (!result.StatusCode.Equals(HttpStatusCode.OK))
                    {
                        generalError += "\nRequisição inválida. Http code: " + result.StatusCode;
                        return; 
                    }
                    string xmlResponse = result.Content.ReadAsStringAsync().Result;
                    setContent(xmlResponse);
                }
                catch (HttpRequestException e)
                {
                    setHttpCodeStatus(HttpStatusCode.NotFound);
                    generalError += "\nRequisição inválida. Erro:" + e;
                }
            }
        }
    }
}