using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using ChatController.Klassen;
using Newtonsoft.Json;

namespace ChatController.Data
{
    public class OwnChatApi
    {
        private static String serverUrl;
        private static String apiKey;
        private static String tenant;

        public static long GetMaximumAllowedFileUploadSize(string apikey, string customerid)
        {
            var endurl = serverUrl + "/api/ownchat/uploadmaxsize";// + apikey + "/" + customerid;

            WebRequest request = WebRequest.Create(endurl);
            request.Headers["Token"] = apikey;
            request.Headers["CustomerID"] = customerid;

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

            using (WebResponse response = request.GetResponse())
            {
                string responseFromServer = ReadStream(response);

                var definiti = new {file_upload_max_size = ""};

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

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

        private UserMessages GetMessages(long groupId)
        {
            
            var endurl = serverUrl + "/api/chat/messages?groupid=" + groupId;

            WebRequest request = WebRequest.Create(endurl);

            request.Credentials = CredentialCache.DefaultCredentials;
            request.Headers["Token"] = apiKey;
            request.Headers["CustomerID"] = tenant;

            using (WebResponse response = request.GetResponse())
            {
                
                string responseFromServer = ReadStream(response);

                if (!String.IsNullOrEmpty(responseFromServer))
                {
                    var jsonDaten = JsonConvert.DeserializeObject<UserMessages>(responseFromServer);
                    return jsonDaten;
                        
                }
                
            }
            
            return null;
        }

        private ChatGruppenDaten GetAllChatGroups()
        {
            var endurl = serverUrl + "/api/chat/groups";

            WebRequest request = WebRequest.Create(endurl);

            request.Credentials = CredentialCache.DefaultCredentials;
            request.Headers["Token"] = apiKey;
            request.Headers["CustomerID"] = tenant;

            using (WebResponse response = request.GetResponse())
            {
                string responseFromServer = ReadStream(response);
                return JsonConvert.DeserializeObject<ChatGruppenDaten>(responseFromServer);
            }
        }

        private static string ReadStream(WebResponse response)
        {
            try
            {
                using (Stream dataStream = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(dataStream))
                    {
                        string responseFromServer = reader.ReadToEnd();
                        
                        return responseFromServer;
                    }
                }
            }
            catch (Exception e)
            {
                //  MessageBox.Show(e.Message);
                return null;
            }
        }
    }
}
