using Http.Core.Exceptions;
using Http.Core;
using Http.Core.Extensions;
using PCUT.Entities.ApiResponse;
using PCUT.Entities;
using PCUT.Helpers;
using PCUT.Models;
using System;
using System.Threading.Tasks;
using System.Windows.Input;
using static Http.Core.Constants.HttpConstants;
using System.Net.Http;
using Windows.UI.Popups;

namespace PCUT.ViewModels
{
    public class BannerUpsertViewModel : NotificationBase
    {
        private string _name;
        public string Name
        {
            get => _name;
            set { SetProperty(ref _name, value); }
        }

        private string _content;
        public string Content
        {
            get => _content;
            set { SetProperty(ref _content, value);}
        }

        private string _bannerId;

        public ICommand Command { get; }

        public BannerUpsertViewModel()
        {
            Command = new RelayCommand(async () => await UpsertBannerAsync());
        }

        public void Initialize(string bannerId)
        {
            _bannerId = bannerId;
        }

        public async Task LoadAsync()
        {
            if (_bannerId is null)
                return;

            using (var client = HttpClientFactory.CreateClient(ClientNames.ApiClient))
            {
                try
                {
                    var response = await client.GetAsync(Api.BannerById.FormatRoute(_bannerId));
                    if (response.IsSuccessStatusCode)
                    {
                        var bannerData = await response.DeserializeObjectAsync<DataResponse<Banner>>();
                        Name = bannerData.Data.Name;
                        Content = bannerData.Data.Content;
                    }
                }
                catch (Exception ex) when (!(ex is AppOutdatedException)) { }
            }
        }

        public async Task UpsertBannerAsync()
        {
            using (var client = HttpClientFactory.CreateClient(ClientNames.ApiClient))
            {
                string message;
                if (_bannerId is null)
                    message = await AddAsync(client);
                else
                    message = await EditAsync(client);

                await new MessageDialog(message).ShowAsync();
            }
        }

        private async Task<string> AddAsync(HttpClient client)
        {
            var banner = new UpsertBanner
            {
                Name = this.Name,
                Content = this.Content
            };
            try
            {
                var response = await client.PostAsJsonAsync(Api.Banner, banner);
                if (response.IsSuccessStatusCode)
                    return "Banner added successfully!";
                else
                    return "Failed to add the banner!";
            }
            catch (Exception ex) when (!(ex is AppOutdatedException))
            {
                return $"error: {ex.Message}";
            }
        }

        private async Task<string> EditAsync(HttpClient client)
        {
            var banner = new UpsertBanner
            {
                Name = this.Name,
                Content = this.Content
            };
            try
            {
                var response = await client.PutAsJsonAsync(Api.BannerById.FormatRoute(_bannerId), banner);
                if (response.IsSuccessStatusCode)
                    return "Banner edited successfully!";
                else
                    return "Failed to edit the banner!";
            }
            catch (Exception ex) when (!(ex is AppOutdatedException))
            {
                return $"error: {ex.Message}";
            }
        }
    }
}
