using System;
using PCUT.ViewModels;
using Windows.UI.Popups;
using Windows.UI.Xaml.Controls;

// The Content Dialog item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238

namespace PCUT.Pages.CategoriesManagement
{
    public sealed partial class MetadataUpsertDialog : ContentDialog
    {
        public MetadataUpsertDialog(string categoryId, string type, string id = null)
        {
            this.InitializeComponent();
            ViewModel.CategoryId = categoryId;
            ViewModel.Type = type;
            ViewModel.Initialize(id);
        }

        private async void ContentDialog_Opened(ContentDialog sender, ContentDialogOpenedEventArgs args)
        {
            await ViewModel.LoadAsync();
        }

        private async void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
        {
            if (ViewModel.Name == null)
            {
                args.Cancel = true;
                var errorDialog = new MessageDialog("Name value cannot be null!");
                await errorDialog.ShowAsync();
            }
            else
            {
                await ViewModel.UpsertMetadataAsync();
            }
        }
    }
}
