using Microsoft.AspNetCore.Components;
using Server.Data;
using Server.Model;

namespace Server.Components.Pages
{
    public partial class CustomerPage : ComponentBase
    {
        #region Private Properties

        private bool EditFormIsNew { get; set; }

        private Address? EditFormObject { get; set; }

        private bool EditFormShow { get; set; }

        #endregion

        #region Override SetParametersAsync

        //// <inheritdoc />
        public override async Task SetParametersAsync(ParameterView parameters)
        {
            parameters.SetParameterProperties(this);

            await CustomerData.LoadAsync();

            await base.SetParametersAsync(ParameterView.Empty);
        }

        #endregion

        #region Private Method DeleteCustomerAsync

        /// <summary>
        ///     Deletes the specified customer asynchronously.
        /// </summary>
        /// <param name="customer">The customer to be deleted.</param>
        /// <return>A Task representing the asynchronous operation.</return>
        private async Task DeleteCustomerAsync(Address customer)
        {
            customer.State = RecordState.Deleted;
            await CustomerData.SaveAsync();
        }

        #endregion

        #region Private Method EditCustomerAsync

        /// <summary>
        ///     Initiates the edit process for a given customer asynchronously.
        /// </summary>
        /// <param name="customer">The customer to be edited.</param>
        /// <returns>A Task representing the asynchronous operation.</returns>
        private Task EditCustomerAsync(Address customer)
        {
            EditFormIsNew = false;
            EditFormObject = customer;
            EditFormShow = true;
            return Task.CompletedTask;
        }

        #endregion

        #region Private Method HideEditFormAsync

        /// <summary>
        ///     Hides the edit form asynchronously.
        /// </summary>
        /// <return>A Task representing the asynchronous operation.</return>
        private Task HideEditFormAsync()
        {
            EditFormObject = new();
            EditFormShow = false;

            return Task.CompletedTask;
        }

        #endregion

        #region Private Method SaveCustomerAsync

        /// <summary>
        ///     Saves the current customer data asynchronously.
        /// </summary>
        /// <returns>A Task representing the asynchronous operation.</returns>
        private async Task SaveCustomerAsync()
        {
            if (EditFormObject == null) return;

            EditFormShow = false;
            if (EditFormIsNew) CustomerData.Instance.Customers.Add(EditFormObject);
            await CustomerData.SaveAsync();
            EditFormObject = new();
        }

        #endregion

        #region Private Method ShowCreateAsync

        /// <summary>
        ///     Displays the form for creating a new customer.
        /// </summary>
        /// <returns>A Task representing the asynchronous operation.</returns>
        private Task ShowCreateAsync()
        {
            EditFormIsNew = true;
            EditFormObject = new();
            EditFormShow = true;

            return Task.CompletedTask;
        }

        #endregion
    }
}