using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace EseguiMotorePrometeia
{
    //EseguiMotorePrometeia.exe serverPrometeia portaServerPrometeia pathInput                          pathOnput                       tipoTracciato   hp  alfa
    //EseguiMotorePrometeia.exe vmcolpra        9875                 D:\BatchSEI\C6\input\input.txt     D:\BatchSEI\C6\output\output    13              63  99
    class EseguiMotorePrometeia
    {
        static int Main(string[] args)
        {
            try
            {
                string serverPrometeia = args[0];
                string portaServerPrometeia = args[1];
                string pathInput = args[2];
                string pathOnput = args[3];
                string tipoTracciato = args[4];
                string hp = args[5];
                string alfa = args[6];
                StringBuilder xml = new StringBuilder();
                xml.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
                xml.Append("<methodCall>");
                xml.Append("<methodName>riskengine.perform</methodName>");
                xml.Append("<params>");
                xml.Append("<param>");
                xml.Append("<value>");
                xml.Append("<string>");
                xml.Append("<![CDATA[");
                xml.Append("<messaggio>");
                xml.Append("<testata>C6</testata>");
                xml.Append("<richiesta03>");
                xml.Append("<fileIn>" + pathInput + "</fileIn>");
                xml.Append("<fileOut>" + pathOnput + "</fileOut>");
                xml.Append("<tipoTracciato>" + tipoTracciato + "</tipoTracciato>");
                xml.Append("<hp>" + hp + "</hp>");
                xml.Append("<alfa>" + alfa + "</alfa>");
                xml.Append("</richiesta03>");
                xml.Append("</messaggio>");
                xml.Append("]]>");
                xml.Append("</string>");
                xml.Append("</value>");
                xml.Append("</param>");
                xml.Append("</params>");
                xml.Append("</methodCall>");
                HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create("http://" + serverPrometeia + ":" + portaServerPrometeia);
                webrequest.KeepAlive = false;
                webrequest.Method = "POST";
                webrequest.ContentType = "text/xml";
                webrequest.AllowAutoRedirect = false;
                webrequest.Timeout = int.MaxValue;
                BuildReqStream(ref webrequest, xml.ToString());
                HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
                Encoding enc = System.Text.Encoding.GetEncoding(1252);
                StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc);
                string Response = loResponseStream.ReadToEnd();
                loResponseStream.Close();
                webresponse.Close();
                if (Response.ToLower().Contains("exception"))
                {
                    throw new Exception("Esegui Motore Prometeia ha generato un errore: " + Response);
                }
                return 0;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

                return 1;
            }
        }

        private static void BuildReqStream(ref HttpWebRequest webrequest, string xml)
        {
            byte[] bytes = Encoding.ASCII.GetBytes(xml);
            webrequest.ContentLength = bytes.Length;

            Stream oStreamOut = webrequest.GetRequestStream();
            oStreamOut.Write(bytes, 0, bytes.Length);
            oStreamOut.Close();
        }

    }
}