using System;
using System.Collections.ObjectModel;
using Avalonia.Controls;
using CommunityToolkit.Mvvm.Input;

namespace PortfolioManager.ViewModels
{
  public delegate void InstantiateWorkspace(SaveParameters saveParameters);

  public abstract class WorkspaceViewModel : ViewModelBase
  {
    // Relay Command
    private RelayCommand closeCommand;

    // Events
   public event EventHandler RequestClose;

    private InstantiateWorkspace workspaceInstantiator;
    private bool canClose = true;
    private bool isClosed = false;
    private String title = "WorkspaceViewModel";

    public WorkspaceViewModel Referer { get; set; }

    protected WorkspaceViewModel()
    {
    }

    public InstantiateWorkspace WorkspaceInstantiator
    {
      get
      {
        return workspaceInstantiator;
      }
      set
      {
        workspaceInstantiator = value;
      }
    }

    public IRelayCommand CloseCommand
    {
      get
      {
        if (null == closeCommand)
        {
          Action closeAction = delegate ()
          {
            this.OnRequestClose();
          };
          closeCommand = new RelayCommand(delegate () { this.OnRequestClose(); });
        }
        return closeCommand;
      }
    }

    public bool IsClosed
    {
      get { return isClosed; }
      set
      {
        if (isClosed != value)
        {
          isClosed = value;
          base.OnPropertyChanged("IsClosed");
        }
      }
    }

    public bool CanClose
    {
      get { return canClose; }
      set
      {
        if (canClose != value)
        {
          canClose = value;
          base.OnPropertyChanged("CanClose");
        }
      }
    }

    public virtual String Title
    {
      get
      {
        return title;
      }
      set
      {
        title = value;
        base.OnPropertyChanged("Title");
      }
    }

    public virtual String Header
    {
      get
      {
        return title;
      }
    }

    private void OnRequestClose()
    {
      EventHandler handler = this.RequestClose;
      if (null != handler) handler(this, EventArgs.Empty);
    }
  }
}
