using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Configuration;
using System.Net;
using System.IO;
using Newtonsoft.Json;
using RestApi.Models;

namespace RestApiTest
{
    [TestClass]
    public class UserTest
    {
        const string REQUEST_URI = "/users";
        
        [TestMethod]
        public void AuthorizedUserInfo()
        {
            string requestUri = ConfigurationManager.AppSettings["RestApiUri"] + REQUEST_URI + "/" + CredentialsManager.RegisteredUser.User.Id;
            var request = (HttpWebRequest)WebRequest.Create(requestUri);
            request.Method = "GET";
            request.ContentType = "application/json";
            request.Headers.Add("X-Auth-Token", CredentialsManager.AdminUser.Token.Id);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);

            StreamReader reader = new StreamReader(response.GetResponseStream());
            string s = reader.ReadToEnd();
            reader.Close();

            UserInfo info = JsonConvert.DeserializeObject<UserInfo>(s);

            Assert.IsNotNull(info);
            Assert.IsNotNull(info.UserId);
        }

        [TestMethod]
        public void UnauthorizedUserInfo()
        {
            string requestUri = ConfigurationManager.AppSettings["RestApiUri"] + REQUEST_URI + "/" + CredentialsManager.RegisteredUser.User.Id;
            var request = (HttpWebRequest)WebRequest.Create(requestUri);
            request.Method = "GET";
            request.ContentType = "application/json";
            request.Headers.Add("X-Auth-Token", "_invalid token_");

            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Assert.Fail();
            }
            catch (WebException ex)
            {
                using (HttpWebResponse response = (HttpWebResponse)ex.Response)
                {
                    Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode);
                }
            }
        }


        [TestMethod]
        public void UserLifeCycle()
        {
            CreateUser();
            DeleteUser();
        }

        private void CreateUser()
        {
            UserInfo user = new UserInfo();
            user.UserId = CredentialsManager.NonRegisteredUser.User.Name;
            user.FullName = user.UserId;
            user.Password = "jcLKe6f8P";
            user.Email = CredentialsManager.NonRegisteredUser.User.Name + "@pha.jhu.edu";

            string requestUri = ConfigurationManager.AppSettings["RestApiUri"] + REQUEST_URI+"/"+CredentialsManager.NonRegisteredUser.User.Id;
            var request = (HttpWebRequest)WebRequest.Create(requestUri);
            request.Method = "PUT";
            request.ContentType = "application/json";
            request.Headers.Add("X-Auth-Token", CredentialsManager.AdminUser.Token.Id);
            StreamWriter writer = new StreamWriter(request.GetRequestStream());
            writer.Write(JsonConvert.SerializeObject(user));
            writer.Close();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
        }

        private void DeleteUser()
        {
            string requestUri = ConfigurationManager.AppSettings["RestApiUri"] + REQUEST_URI + "/" + CredentialsManager.NonRegisteredUser.User.Id;
            var request = (HttpWebRequest)WebRequest.Create(requestUri);
            request.Method = "DELETE";
            request.ContentType = "application/json";
            request.Headers.Add("X-Auth-Token", CredentialsManager.AdminUser.Token.Id);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
        }

        [TestMethod]
        public void UnauthorizedCreateUser()
        {
            string requestUri = ConfigurationManager.AppSettings["RestApiUri"] + REQUEST_URI + "/" + CredentialsManager.NonRegisteredUser.User.Id;
            var request = (HttpWebRequest)WebRequest.Create(requestUri);
            request.Method = "PUT";
            request.ContentType = "application/json";
            request.Headers.Add("X-Auth-Token", "_invalid token_");
            StreamWriter writer = new StreamWriter(request.GetRequestStream());
            writer.Write("");
            writer.Close();
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Assert.Fail();
            }
            catch (WebException ex)
            {
                using (HttpWebResponse response = (HttpWebResponse)ex.Response)
                {
                    Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode);
                }
            }
        }

        [TestMethod]
        public void UnauthorizedDeleteUser()
        {
            string requestUri = ConfigurationManager.AppSettings["RestApiUri"] + REQUEST_URI + "/" + CredentialsManager.NonRegisteredUser.User.Id;
            var request = (HttpWebRequest)WebRequest.Create(requestUri);
            request.Method = "DELETe";
            request.ContentType = "application/json";
            request.Headers.Add("X-Auth-Token", "_invalid token_");
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Assert.Fail();
            }
            catch (WebException ex)
            {
                using (HttpWebResponse response = (HttpWebResponse)ex.Response)
                {
                    Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode);
                }
            }
        }
    }
}
