using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.ServiceModel.Configuration;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WPF_Sample.Common;

namespace WPF_Sample.Views.Examples
{
    /// <summary>
    /// Interaction logic for RestSample.xaml
    /// </summary>
    public partial class RestSample : Page
    {

        public RestSample()
        {
            InitializeComponent();
            URL.Text = "https://stratustime.centralservers.com/Service/ws-json/2.0/CreateToken";
        }

        private void PostButton_Click(object sender, RoutedEventArgs e)
        {
            PostResult.Text = "Loading Please Wait...";
            AppUtil.DoEvents();

            Func<HttpWebResponse, string> getResponse = (response) =>
            {
                var responseEncoding = Encoding.GetEncoding(response.CharacterSet);
                using (var sr = new StreamReader(response.GetResponseStream(), responseEncoding))
                {
                    return sr.ReadToEnd();
                }
            };

            string jsonContent = PostData.Text;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL.Text);
            request.Method = "POST";

            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
            Byte[] byteArray = encoding.GetBytes(jsonContent);

            request.ContentLength = byteArray.Length;
            request.ContentType = @"application/json";

            using (Stream dataStream = request.GetRequestStream())
            {
                dataStream.Write(byteArray, 0, byteArray.Length);
            }

            long length = 0;

            try
            {
                string body;
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    length = response.ContentLength;
                    body = getResponse(response);
                }
                PostResult.Text = body;
            }
            catch (WebException ex)
            {
                PostResult.Text = "Be Sure to check the full URL:"  + ex.ToString();
            }

        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            PostData.Text = @"{
	""CustomerAlias"":""YOUR-ALIAS"",
	""SharedKey"":""234f710d-894f-44f2-9012-8598a09f4777"",
	""UserName"":""wsuser"",
	""UserPass"":""YOUR-PASS""
}";

        }

        private void V1_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process.Start("https://stratustime.centralservers.com/Service/ws-json/1.0/help");
        }

        private void V2_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process.Start("https://stratustime.centralservers.com/Service/ws-json/2.0/help");
        }

    }
}
