using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

namespace LauncherIcarus.Daemon.HTTP
{
    public class HTTPREQ
    {
        private string LoginUrl = Configuration.Default.ServerIP + Configuration.Default.LoginUrl;

        private string GetLauncherVersionUrl =
            Configuration.Default.ServerIP + Configuration.Default.GetLauncherVersion;

        private string UpdateLauncherVersionUrl =
            Configuration.Default.ServerIP + Configuration.Default.UpdateLauncherVersion;

        private string GetLauncherFilesUrl = Configuration.Default.ServerIP + Configuration.Default.ListFileUrl;
        private string CheckClientFiles = Configuration.Default.ServerIP + Configuration.Default.CheckClientFiles;
        private string GetTranslate = Configuration.Default.ServerIP + Configuration.Default.GetTranslate;

        public async Task<string> LoginAsync(string accountName, string password)
        {
            string str;
            using (FormUrlEncodedContent content = new FormUrlEncodedContent(
                       (IEnumerable<KeyValuePair<string, string>>) new Dictionary<string, string>()
                       {
                           {
                               "Username",
                               accountName
                           },
                           {
                               "Password",
                               password
                           }
                       }))
            {
                using (HttpClient client = new HttpClient())
                    str = await (await client.PostAsync(this.LoginUrl, (HttpContent) content)).Content
                        .ReadAsStringAsync();
            }

            return str;
        }

        public async Task<string> GetLauncherVersionAsync(string token, string Username)
        {
            string launcherVersionAsync;
            using (FormUrlEncodedContent content = new FormUrlEncodedContent(
                       (IEnumerable<KeyValuePair<string, string>>) new Dictionary<string, string>()
                       {
                           {
                               nameof(token),
                               token
                           },
                           {
                               nameof(Username),
                               Username
                           }
                       }))
            {
                using (HttpClient client = new HttpClient())
                    launcherVersionAsync =
                        await (await client.PostAsync(this.GetLauncherVersionUrl, (HttpContent) content)).Content
                            .ReadAsStringAsync();
            }

            return launcherVersionAsync;
        }

        public async Task<string> UpdateLauncherVersionAsync(
            string token,
            string UpdatedVersion,
            string Username)
        {
            string str;
            using (FormUrlEncodedContent content = new FormUrlEncodedContent(
                       (IEnumerable<KeyValuePair<string, string>>) new Dictionary<string, string>()
                       {
                           {
                               nameof(token),
                               token
                           },
                           {
                               nameof(Username),
                               Username
                           },
                           {
                               "UpdaterVersion",
                               UpdatedVersion
                           }
                       }))
            {
                using (HttpClient client = new HttpClient())
                    str = await (await client.PostAsync(this.UpdateLauncherVersionUrl, (HttpContent) content)).Content
                        .ReadAsStringAsync();
            }

            return str;
        }

        public async Task<string> GetLauncherListFilesAsync(string token, string clientVersion)
        {
            string launcherListFilesAsync;
            using (FormUrlEncodedContent content = new FormUrlEncodedContent(
                       (IEnumerable<KeyValuePair<string, string>>) new Dictionary<string, string>()
                       {
                           {
                               nameof(token),
                               token
                           },
                           {
                               nameof(clientVersion),
                               clientVersion
                           }
                       }))
            {
                using (HttpClient client = new HttpClient())
                    launcherListFilesAsync =
                        await (await client.PostAsync(this.GetLauncherFilesUrl, (HttpContent) content)).Content
                            .ReadAsStringAsync();
            }

            return launcherListFilesAsync;
        }

        public async Task<string> GetClientFilesCheckListAsync(string token)
        {
            string filesCheckListAsync;
            using (FormUrlEncodedContent content = new FormUrlEncodedContent(
                       (IEnumerable<KeyValuePair<string, string>>) new Dictionary<string, string>()
                       {
                           {
                               nameof(token),
                               token
                           }
                       }))
            {
                using (HttpClient client = new HttpClient())
                    filesCheckListAsync = await (await client.PostAsync(this.CheckClientFiles, (HttpContent) content))
                        .Content.ReadAsStringAsync();
            }

            return filesCheckListAsync;
        }

        public async Task<string> GetLauncherTranslateAsync()
        {
            string launcherTranslateAsync;
            using (HttpClient client = new HttpClient())
                launcherTranslateAsync = await (await client.GetAsync(this.GetTranslate)).Content.ReadAsStringAsync();
            return launcherTranslateAsync;
        }
    }
}