using System;
using System.Configuration;
using System.Web;
using System.Web.Services;
using System.Xml;
using MOH_CRA_XCRMS_Service.Common;
using MOH_CRA_XCRMS_Service.Entities;

namespace MOH_CRA_XCRMS_Service
{
    /// <summary>
    /// Summary description for MOH_CRA_XCRMS_Service
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class MOH_CRA_XCRMS_Service : WebService
    {
        // todo LOG აქვს ჩასამატებელი
        [WebMethod]
        public XmlDocument Get_MOHPersonInfo(string privateNumber, string birthDate, string requestReason,
            string organizationUnit, string unitName, string userName)
        {
            
            if (string.IsNullOrEmpty(userName)) throw new Exception("Username is Empty!");
            organizationUnit = organizationUnit.ToUpper();
            unitName = unitName.ToUpper();

         //   const string subContractId = "MOHPersonInfo1Test";
            const string subContractId = "MOHPersonInfo1";
            var _signingCertificate = Helpers.GetSigningCertificate_FileVersion(subContractId, organizationUnit, unitName);

            var requestXml = MOHCPersoninfoRepository.GetRequestXML(privateNumber, birthDate, subContractId, organizationUnit, _signingCertificate);

            long keyId;
            var keyExchangeSubcontractId = ConfigurationSettings.AppSettings[subContractId + "_KeyExchangeSubcontractId"];
            byte[] key = SymetricEncrypting.GetSymetricEncryptionKey_SubcontractId(subContractId, keyExchangeSubcontractId, organizationUnit, unitName, out keyId);

            ((XmlElement)requestXml.GetSubnode("Parameters")).EncryptXml(key, keyId);

            string response = CallApi.GetResponse(requestXml.OuterXml);

            var responseDoc = new XmlDocument();
            responseDoc.LoadXml(response);

            const string msgNamespace = "urn:g3.ge:cra:call:CRA_Xcrms_ProcessRequest:v1";

            string metodStr;
            string correlationID;
            string SMSSenderID;

            Helpers.ResponseXML_HEADER_Parse(responseDoc, msgNamespace, /*out xnsManager, */ out metodStr, out correlationID, out SMSSenderID);

            var result = new MOHCPersoninfoRepository().GetResultEntity(responseDoc, key);

            var req = HttpContext.Current.Request;
            var sreq = new Request_XCRMS_LOG
            {
                MetodName = $"Get_MOHPersonInfo: PID:{privateNumber}, BirthDate:{birthDate}",
                UserAddress = req.UserHostAddress,
                UserAgent = req.UserAgent,
                UserHost = req.UserHostName,
                Organization = organizationUnit,
                Unit = unitName,
                Useri = userName,
                Parametri = privateNumber,
                RequestReason = requestReason
            };
            RequestsEntity.AddRequest(sreq, result);

            return result;
        }

        [WebMethod]
        public MOHPersonInfoResultEntity Get_MOHPersonInfo_NET(string privateNumber, string birthDate, string requestReason,
                string organizationUnit, string unitName, string userName)
        {
            if (string.IsNullOrEmpty(userName)) throw new Exception("Username is Empty!");

            organizationUnit = organizationUnit.ToUpper();
            unitName = unitName.ToUpper();

            const string subContractId = "MOHPersonInfo1";
            var _signingCertificate = Helpers.GetSigningCertificate_FileVersion(subContractId, organizationUnit, unitName);

            var requestXml = MOHCPersoninfoRepository.GetRequestXML(privateNumber, birthDate, subContractId, organizationUnit, _signingCertificate);

            long keyId;
            var keyExchangeSubcontractId = ConfigurationSettings.AppSettings[subContractId + "_KeyExchangeSubcontractId"];
            byte[] key = SymetricEncrypting.GetSymetricEncryptionKey_SubcontractId(subContractId, keyExchangeSubcontractId, organizationUnit, unitName, out keyId);

            ((XmlElement)requestXml.GetSubnode("Parameters")).EncryptXml(key, keyId);

            string response = CallApi.GetResponse(requestXml.OuterXml);

            var responseDoc = new XmlDocument();
            responseDoc.LoadXml(response);

            const string msgNamespace = "urn:g3.ge:cra:call:CRA_Xcrms_ProcessRequest:v1";

            string metodStr;
            string correlationID;
            string SMSSenderID;

            Helpers.ResponseXML_HEADER_Parse(responseDoc, msgNamespace, /*out xnsManager, */ out metodStr, out correlationID, out SMSSenderID);

            var result = new MOHCPersoninfoRepository().GetResultEntity_NET(responseDoc, key);

            var req = HttpContext.Current.Request;
            var sreq = new Request_XCRMS_LOG
            {
                MetodName = $"Get_MOHPersonInfo_NET: PID:{privateNumber}, BirthDate:{birthDate}",
                UserAddress = req.UserHostAddress,
                UserAgent = req.UserAgent,
                UserHost = req.UserHostName,
                Organization = organizationUnit,
                Unit = unitName,
                Useri = userName,
                Parametri = privateNumber,
                RequestReason = requestReason
            };
            RequestsEntity.AddRequest(sreq, result);

            return result;
        }

        // todo LOG აქვს ჩასამატებელი
        [WebMethod]
        public XmlDocument Get_MOHPersonInfo_WithPhoto(string privateNumber, string birthDate, string requestReason,
            string organizationUnit, string unitName, string userName)
        {

            if (string.IsNullOrEmpty(userName)) throw new Exception("Username is Empty!");
            organizationUnit = organizationUnit.ToUpper();
            unitName = unitName.ToUpper();

            const string subContractId = "MOHPersonInfo2";
            var _signingCertificate = Helpers.GetSigningCertificate_FileVersion(subContractId, organizationUnit, unitName);

            var requestXml = MOHCPersoninfoRepository.GetRequestXML(privateNumber, birthDate, subContractId, organizationUnit, _signingCertificate);

            long keyId;
            var keyExchangeSubcontractId = ConfigurationSettings.AppSettings[subContractId + "_KeyExchangeSubcontractId"];
            byte[] key = SymetricEncrypting.GetSymetricEncryptionKey_SubcontractId(subContractId, keyExchangeSubcontractId, organizationUnit, unitName, out keyId);

            ((XmlElement)requestXml.GetSubnode("Parameters")).EncryptXml(key, keyId);

            string response = CallApi.GetResponse(requestXml.OuterXml);

            var responseDoc = new XmlDocument();
            responseDoc.LoadXml(response);

            const string msgNamespace = "urn:g3.ge:cra:call:CRA_Xcrms_ProcessRequest:v1";

            string metodStr;
            string correlationID;
            string SMSSenderID;

            Helpers.ResponseXML_HEADER_Parse(responseDoc, msgNamespace, /*out xnsManager, */ out metodStr, out correlationID, out SMSSenderID);

            var result = new MOHCPersoninfoRepository().GetResultEntity(responseDoc, key);

            var req = HttpContext.Current.Request;
            var sreq = new Request_XCRMS_LOG
            {
                MetodName = $"Get_MOHPersonInfo_WithPhoto: PID:{privateNumber}, BirthDate:{birthDate}",
                UserAddress = req.UserHostAddress,
                UserAgent = req.UserAgent,
                UserHost = req.UserHostName,
                Organization = organizationUnit,
                Unit = unitName,
                Useri = userName,
                Parametri = privateNumber,
                RequestReason = requestReason
            };
            RequestsEntity.AddRequest(sreq, result);

            return result;
        }
    }
}