using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Net;
using System.Text;
using System.IO;

namespace fooWeb.Admin.Accounting
{
    public partial class invoice : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


        }

        protected void CreateInvoice(object sender, EventArgs e)
        {
            string URL = WebConfigurationManager.AppSettings["iCountAPI"].ToString();
            string qrystring = string.Empty;
            Dictionary<string, string> items = new Dictionary();
            items.Add("compID", "foo");
            items.Add("user", "foo");
            items.Add("pass", "foo");
            items.Add("dateissued", "20100515");//Should be in format YYYYMMDD
            items.Add("clientname", "ישראל ישראלי");
            items.Add("client_street_number", "10");
            items.Add("client_street", "אבן גבירול");
            items.Add("client_city", "תל אביב");
            items.Add("client_country", "ישראל");
            items.Add("client_zip", "12345");
            items.Add("docType", "invoice");
            items.Add("totalsum", "100");
            items.Add("totalvat", "16");
            items.Add("totalwithvat", "116");
            //items.Add("paidincvat", "foo");
            //items.Add("taxexempt", "foo");
            //must add 3 items as array items.
            //desc[]
            //quantity[]
            //unitprice[]
            items.Add("maampercent", "16");
            items.Add("originaldiscount", "");
            items.Add("currency", "5");//Better use enum
            items.Add("rate", "");
            items.Add("paydate", "20100601");
            items.Add("afterdiscount", "1000");
            items.Add("income_type_name", "foo");
            items.Add("vatid", "512616665");
            items.Add("show_response", "1");
            string[] desc = {"סיר 8 ליטר - סולתם"};
            int[] quantity = { 1 };
            int[] unitprice = { 100 };
            qrystring = "compID=CompID";
            foreach (var item in items)
            {
                qrystring += string.Format("&{0}={1}", item.Key, item.Value);
            }
            qrystring += "&desc[0]=" + desc[0] + "&quantity[0]=" + quantity[0]  + "&unitprice[0]=" + unitprice[0];
             // Create a request using a URL that can receive a post. 
            WebRequest request = WebRequest.Create(URL);
            // Set the Method property of the request to POST.
            request.Method = "POST";
            // Create POST data and convert it to a byte array.
            //string postData = "This is a test that posts this string to a Web server.";
            byte[] byteArray = Encoding.UTF8.GetBytes (qrystring);
            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";
            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;
            // Get the request stream.
            Stream dataStream = request.GetRequestStream ();
            // Write the data to the request stream.
            dataStream.Write (byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close ();
            // Get the response.
            WebResponse response = request.GetResponse ();
            // Display the status.
            Console.WriteLine (((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream ();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader (dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd ();
            // Display the content.
            Console.WriteLine (responseFromServer);
            // Clean up the streams.
            reader.Close ();
            dataStream.Close ();
            response.Close();
        
        }
    }
}