using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using FirstSlice.Models;

namespace FirstSlice;

public partial class RoleWindow : Window
{
    private Role role_ = null;
    
    public RoleWindow()
    {
        InitializeComponent();
    }
    
    public RoleWindow(Role role)
    {
        InitializeComponent();

        role_ = role;
        TextRole.Text = role_.Name;
    }

    private void OnSaveRoleClick(object? sender, RoutedEventArgs e)
    {
        if (String.IsNullOrWhiteSpace(TextRole.Text))
        {
            return;
        }
        
        if (role_ != null)
        {
            role_.Name = TextRole.Text;

            Helper.GetContext().Update(role_);
            Helper.GetContext().SaveChanges();
            this.Close();
        }
        else
        {
            var newRole = new Role();
            newRole.Name = TextRole.Text;

            Helper.GetContext().Add(newRole);
            Helper.GetContext().SaveChanges();
            this.Close();
        }
    }
}