using System;
using System.IO;
using System.Net;
using System.Xml;
using System.Xml.Linq;

namespace VMT
{
    public class UserRole
    {
       

        public static string InvokeService(string data1, string data2)
        {
           // string url =  "http://localhost:58980/UserRoleService.asmx/GetRole";
            string url = "http://52.35.69.115/vms/UserRoleService.asmx?op=GetRole";
            string xmlData = "";
            string result = "";
            string strPost = "argStoredProcedureName=" + data1 + "&pageTitle=" + data2;
            StreamWriter myWriter = null;
            HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
            objRequest.UseDefaultCredentials = true;
            objRequest.Method = "POST";
            objRequest.Timeout = 200000; // this should be greater than IIS / web server's timeout
            objRequest.ContentLength = strPost.Length;
            objRequest.ContentType = "application/x-www-form-urlencoded";
            objRequest.KeepAlive = false;
            try
            {
                myWriter = new StreamWriter(objRequest.GetRequestStream());
                myWriter.Write(strPost);

            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
            finally
            {
                myWriter.Close();
            }

            HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();

            using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
            {
                xmlData = sr.ReadToEnd();
                sr.Close();
                XDocument doc = XDocument.Parse(xmlData);
                result = doc.Root.Value;
            }
            return result;
        }

        public static HttpWebRequest CreateSOAPWebRequest()
        {
            string m_URL_USERROLE = Properties.Settings.Default.URL_USERROLE;
            //Making Web Request    
           // HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(@"http://localhost:63658/UserRoleService.asmx");
            HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(m_URL_USERROLE);
            //SOAPAction    
            Req.Headers.Add(@"SOAPAction:http://tempuri.org/GetRole");
            //Content_type    
            Req.ContentType = "text/xml;charset=\"utf-8\"";
            Req.Accept = "text/xml";
            //HTTP method    
            Req.Method = "POST";
            //return HttpWebRequest    
            return Req;
        }

        public static HttpWebRequest CreateNewSOAPWebRequest()
        {
            string m_URL_USERROLE = Properties.Settings.Default.URL_USERROLE;
            //Making Web Request    
            // HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(@"http://localhost:63658/UserRoleService.asmx");
            HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(m_URL_USERROLE);
            //SOAPAction    
             Req.Headers.Add(@"SOAPAction:http://tempuri.org/GetUserRole");
            //Content_type    
            Req.ContentType = "text/xml;charset=\"utf-8\"";
            Req.Accept = "text/xml";
            //HTTP method    
            Req.Method = "POST";
            //return HttpWebRequest    
            return Req;
        }

        public static string CallUserRoleWebService(string argStoredProcedureName, string pageTitle)
        {
            string result = "";
            //Calling CreateSOAPWebRequest method  
            HttpWebRequest request = CreateSOAPWebRequest();
            //request.Method = "GET";
            request.UseDefaultCredentials = true;
            XmlDocument SOAPReqBody = new XmlDocument();
            //SOAP Body Request  
            SOAPReqBody.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?>  
            <soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">  
             <soap:Body>  
                <GetRole xmlns=""http://tempuri.org/"">  
                  <argStoredProcedureName>" + argStoredProcedureName + @"</argStoredProcedureName>  
                  <pageTitle>" + pageTitle + @"</pageTitle>  
                </GetRole>  
              </soap:Body>  
            </soap:Envelope>");


            using (Stream stream = request.GetRequestStream())
            {
                SOAPReqBody.Save(stream);
            }
            //Geting response from request  
            using (WebResponse Serviceres = request.GetResponse())
            {
                using (StreamReader rd = new StreamReader(Serviceres.GetResponseStream()))
                {
                    //reading stream  
                    var ServiceResult = rd.ReadToEnd();


                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.LoadXml(ServiceResult);



                    result = xmlDoc.InnerText.ToString();
                   
                }
            }

            return result;
        }

        public static string CallNewUserRoleWebService(string argStoredProcedureName, string pageTitle, string Userid)
        {
            string result = "";
            ////Calling CreateSOAPWebRequest method  
            //HttpWebRequest request = CreateNewSOAPWebRequest();
            ////request.Method = "GET";
            //request.UseDefaultCredentials = true;
            //XmlDocument SOAPReqBody = new XmlDocument();
            ////SOAP Body Request  
            //SOAPReqBody.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?>  
            //<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">  
            // <soap:Body>  
            //    <GetUserRole xmlns=""http://tempuri.org/"">  
            //      <argStoredProcedureName>" + argStoredProcedureName + @"</argStoredProcedureName>  
            //      <pageTitle>" + pageTitle + @"</pageTitle>  
            //      <Userid>" + Userid + @"</Userid> 
            //    </GetUserRole>  
            //  </soap:Body>  
            //</soap:Envelope>");


            //using (Stream stream = request.GetRequestStream())
            //{
            //    SOAPReqBody.Save(stream);
            //}
            ////Geting response from request  
            //using (WebResponse Serviceres = request.GetResponse())
            //{
            //    using (StreamReader rd = new StreamReader(Serviceres.GetResponseStream()))
            //    {
            //        //reading stream  
            //        var ServiceResult = rd.ReadToEnd();


            //        XmlDocument xmlDoc = new XmlDocument();
            //        xmlDoc.LoadXml(ServiceResult);



            //        result = xmlDoc.InnerText.ToString();

            //    }
            //}
            result = "1";
            return result;
        }  


    }
}