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

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

        private readonly string _getLauncherVersionUrl =
            Configuration.Default.ServerIP + Configuration.Default.GetLauncherVersion;

        private readonly string _updateLauncherVersionUrl =
            Configuration.Default.ServerIP + Configuration.Default.UpdateLauncherVersion;

        private readonly string _getLauncherFilesUrl =
            Configuration.Default.ServerIP + Configuration.Default.ListFileUrl;

        private readonly string _checkClientFiles =
            Configuration.Default.ServerIP + Configuration.Default.CheckClientFiles;

        private readonly string _getTranslate = Configuration.Default.ServerIP + Configuration.Default.GetTranslate;

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

            return str;
        }

        public async Task<string> GetLauncherVersionAsync(string token, string username)
        {
            string launcherVersionAsync;
            using (var content = new FormUrlEncodedContent(new Dictionary<string, string>
                   {
                       {
                           nameof(token),
                           token
                       },
                       {
                           nameof(username),
                           username
                       }
                   }))
            {
                using (var client = new HttpClient())
                {
                    launcherVersionAsync = await (await client.PostAsync(_getLauncherVersionUrl, content))
                        .Content
                        .ReadAsStringAsync();
                }
            }

            return launcherVersionAsync;
        }

        public async Task<string> UpdateLauncherVersionAsync(string token, string updatedVersion, string username)
        {
            string str;
            using (var content = new FormUrlEncodedContent(new Dictionary<string, string>
                   {
                       {
                           nameof(token),
                           token
                       },
                       {
                           nameof(username),
                           username
                       },
                       {
                           "UpdaterVersion",
                           updatedVersion
                       }
                   }))
            {
                using (var client = new HttpClient())
                {
                    str = await (await client.PostAsync(_updateLauncherVersionUrl, content))
                        .Content
                        .ReadAsStringAsync();
                }
            }

            return str;
        }

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

            return launcherListFilesAsync;
        }

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

            return filesCheckListAsync;
        }

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

            return launcherTranslateAsync;
        }
    }
}