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

namespace PCUT.ViewModels
{
    public class UpsertCategoryViewModel : NotificationBase
    {
        private string _name;
        public string Name
        {
            get { return _name; }

            set { SetProperty(ref _name, value); }
        }

        private string _description;
        public string Description
        {
            get { return _description; }

            set { SetProperty(ref _description, value); }
        }

        private string _categoryId;
        public ICommand Command { get; }

        public UpsertCategoryViewModel()
        {
            Command = new RelayCommand(async () => await UpsertCategoryAsync());
        }

        public void Initialize(string categoryId)
        {
            _categoryId = categoryId;
        }

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

            using (var client = HttpClientFactory.CreateClient(ClientNames.ApiClient))
            {
                try
                {
                    var response = await client.GetAsync(Api.CategoryById.FormatRoute(_categoryId));
                    if (response.IsSuccessStatusCode)
                    {
                        var categoryData = await response.DeserializeObjectAsync<DataResponse<Category>>();
                        Name = categoryData.Data.Name;
                        Description = categoryData.Data.Description;
                    }
                }
                catch (Exception ex) when (!(ex is AppOutdatedException)) { }
            }
        }

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

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

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

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