using System;
using System.IO;
using System.Net;
using ChatController.LoginKlassen;
using ChatController.Utilities;
using Newtonsoft.Json;

namespace ChatController.Data
{
    public class OwnChatApi
    {
        public static string ServerUrl { get; set; }
        public static string ApiKey { get; set; }
        public static string Tenant { get; set; }

        public static long GetMaximumAllowedFileUploadSize(string pApiKey, string pCustomerId)
        {
            var url = ServerUrl + Constants.MaxUploadFileSizeUrl;

            var webRequest = WebRequest.Create(url);
            webRequest.Headers[Constants.Token] = pApiKey;
            webRequest.Headers[Constants.CustomerId] = pCustomerId;

            webRequest.Credentials = CredentialCache.DefaultCredentials;
            webRequest.Proxy = null;

            using (var response = webRequest.GetResponse())
            {
                var responseFromServer = ReadStream(response);

                var definiti = new {file_upload_max_size = ""};

                var jsonDatentyp = JsonConvert.DeserializeAnonymousType(responseFromServer, definiti);

                return Convert.ToInt64(jsonDatentyp.file_upload_max_size);
            }
        }

        public static UserMessages GetMessages(long pGroupId)
        {
            
            var url = ServerUrl + Constants.LoadMessagesForGroupUrl + pGroupId;

            var webRequest = WebRequest.Create(url);

            webRequest.Credentials = CredentialCache.DefaultCredentials;
            webRequest.Headers[Constants.Token] = ApiKey;
            webRequest.Headers[Constants.CustomerId] = Tenant;

            using (var response = webRequest.GetResponse())
            {
                var serverResponse = ReadStream(response);

                if (!string.IsNullOrEmpty(serverResponse))
                {
                    return JsonConvert.DeserializeObject<UserMessages>(serverResponse);
                }
            }
            
            return null;
        }

        public ChatGruppenDaten GetAllChatGroups()
        {
            var endurl = ServerUrl + Constants.GetChatGroupsUrl;

            var webRequest = WebRequest.Create(endurl);

            webRequest.Credentials = CredentialCache.DefaultCredentials;
            webRequest.Headers[Constants.Token] = ApiKey;
            webRequest.Headers[Constants.CustomerId] = Tenant;

            using (var response = webRequest.GetResponse())
            {
                var serverResponse = ReadStream(response);
                return JsonConvert.DeserializeObject<ChatGruppenDaten>(serverResponse);
            }
        }

        public static string ReadStream(WebResponse response)
        {
            try
            {
                var serverResponse = string.Empty;
                
                using (var dataStream = response.GetResponseStream())
                {
                    if(dataStream == null)
                    {
                        return serverResponse;
                    }
                    
                    using (var reader = new StreamReader(dataStream))
                    {
                        serverResponse = reader.ReadToEnd();
                        
                        return serverResponse;
                    }
                }
            }
            catch (Exception exception)
            {
                return null;
            }
        }
    }
}
