using CityInfo.WEB.Models;
using CityInfo.WEB.Services;
using Microsoft.AspNetCore.Components;

namespace CityInfo.WEB.Pages
{
    public partial class PointOfInterestEdit
    {
        [Inject]
        public IPointOfInterestDataService PointOfInterestDataService { get; set; } = default!;
        [Inject]
        public ICityDataService CityDataService { get; set; } = default!;
        [Inject]
        public NavigationManager NavigationManager { get; set; } = default!;
        [Parameter]
        public int CityId { get; set; }
        [Parameter]
        public int PointOfInterestId { get; set; }
        public PointOfInterest? PointOfInterest { get; set; }

        public bool ReturnData { get; set; } = false;
        public string StatusClass { get; set; } = string.Empty;
        public string ReturnMessage { get; set; } = string.Empty;
        protected async override Task OnInitializedAsync()
        {
            PointOfInterest = await PointOfInterestDataService.GetPointOfInterest(CityId, PointOfInterestId);
            if (PointOfInterest == null)
            {
                NavigationManager.NavigateTo("not-found");
                return;
            }
        }
        private async Task HandleValidSubmit()
        {
            if(PointOfInterest != null)
            {
                var isAdded = await PointOfInterestDataService.UpdatePointOfInterest(CityId, PointOfInterest);
                if (isAdded)
                {
                    ReturnData = true;
                    ReturnMessage = "Le point d'intérer à été mis à jour.";
                    StatusClass = "alert-success";
                } else
                {
                    ReturnData = true;
                    ReturnMessage = "Une erreur est survenue lors de la mise à jour du point d'intéret.";
                    StatusClass = "alert-danger";

                }
            } else
            {
                ReturnData = true;
                ReturnMessage = "Une erreur est arrivée avec les données entrées";
                StatusClass = "alert-danger";
            }
        }
        private async Task HandleInvalidSubmit()
        {
            ReturnData = true;
            ReturnMessage = "Les données entrées sont incorrectes.";
            StatusClass = "alert-danger";

        }
    }
}
