using RestSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InfoAPITestingTool
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            (sender as Button).Enabled = false;
            try
            {
                OdgovorTextBox.Text = "";

                string adress = urlTextBox.Text;

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(adress);

                request.ContentType = "application/json";
                request.Accept = "application/json";
                request.Method = "POST";


                string json = "{\"app_option\": \"INFO\",\"data\": \"\",\"req_id\": \"DOTEST_4S60QRPEI\",\"instance_id\": \"\",\"password\": \"demo\",\"session_id\": \"\",\"username\": \"info\",\"client_id\": \"DEMO\"}";
                byte[] bytes;
                bytes = Encoding.UTF8.GetBytes(json);

                request.ContentLength = bytes.Length;
                if (!string.IsNullOrEmpty(DebugTextBox.Text))
                    request.Headers.Add("debug", DebugTextBox.Text);

                if (!string.IsNullOrEmpty(ApiVersionTextBox.Text))
                    request.Headers.Add("api_version", ApiVersionTextBox.Text);

                Stream requestStream = request.GetRequestStream();
                requestStream.Write(bytes, 0, bytes.Length);
                requestStream.Close();

                var response = (HttpWebResponse)request.GetResponseAsync().GetAwaiter().GetResult();
                using (Stream stream = response.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(stream, Encoding.UTF8);
                    String responseString = reader.ReadToEnd();
                    OdgovorTextBox.Text = responseString;
                }
            }
            catch(Exception exc)
            {
                OdgovorTextBox.Text = exc.Message;
            }
            finally
            {
                (sender as Button).Enabled = true;
            }
        }
    }
}
