using System;
using System.Configuration;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Web;
using System.Xml;
using MOH_CRA_XCRMS_Service.Common;
using System.Xml.Serialization;

namespace MOH_CRA_XCRMS_Service.Entities
{
    public class MOHCPersoninfoRepository
    {
        public static XmlDocument GetRequestXML(string privateNumber, string birthDate, string subContractId, string organizationUnit, X509Certificate2 signingCertificate)
        {
            var requestXmlName = ConfigurationSettings.AppSettings[subContractId + "_RequestXMLFileName"];
            var requestXml = HttpContext.Current.Server.MapPath(requestXmlName);

            var requestDoc = new XmlDocument();
            requestDoc.Load(requestXml);

            XmlNodeList nodeListPid = requestDoc.GetElementsByTagName("PrivateNumber");
            nodeListPid[0].FirstChild.Value = privateNumber;

            XmlNodeList nodeListSerial = requestDoc.GetElementsByTagName("BirthYear");
            nodeListSerial[0].FirstChild.Value = birthDate;

            ((XmlElement)requestDoc.GetSubnode("Request")).SignXml(signingCertificate, Guid.NewGuid());

            return requestDoc;
        }

        public XmlDocument GetResultEntity(XmlDocument responseDoc, byte[] key)
        {
            var encryptedNode = (XmlElement)responseDoc.GetSubnode("Person");
            if (encryptedNode != null)
                encryptedNode.DecryptXml(key);

            var statusNode = responseDoc.GetSubnode("Response"); //XmlNode

            var reader = XmlReader.Create(new StringReader(statusNode.OuterXml));
            var result = new XmlDocument();
            result.Load(reader);
            return result;
        }

        public MOHPersonInfoResultEntity GetResultEntity_NET(XmlDocument responseDoc, byte[] key)
        {
            var encryptedNode = (XmlElement)responseDoc.GetSubnode("Person");
            if (encryptedNode != null)
                encryptedNode.DecryptXml(key);

            var result = new MOHPersonInfoResultEntity { Person = new MOHPerson() };

            var statusNode = responseDoc.GetSubnode("ResultStatus");
            var serializerResultStatus = new XmlSerializer(typeof(ResultStatus), statusNode.NamespaceURI);

            var readerResultStatus = XmlReader.Create(new StringReader(statusNode.OuterXml));
            result.ResultStatus = (ResultStatus)serializerResultStatus.Deserialize(readerResultStatus);

            if (result.ResultStatus.Code == "14")
            {
                var personNode = responseDoc.GetSubnode("Person");
                var serializerPerson = new XmlSerializer(typeof(MOHPerson), personNode.NamespaceURI);

                var readerPerson = XmlReader.Create(new StringReader(personNode.OuterXml));
                result.Person = (MOHPerson)serializerPerson.Deserialize(readerPerson);
            }

            //var photoImage = result.Person.Photo.PhotoImageStr;
            //var encoding = new UTF8Encoding();  
            //var byteArray = encoding.GetBytes(photoImage);  
            //        PhotoImage = byteArray;  

            return result;
        }
    }
}