///|
/// What a server tells about itself.
#derive(Eq, Debug, ToJson, FromJson)
pub(all) struct AppInfo {
  instance : InstanceName
  version : @contracts.Version
}

///|
/// The request to add a folder to the project list.
#derive(Eq, Debug, ToJson, FromJson)
pub(all) struct AddProject {
  /// The text that the user gives. It has no rules here: the server parses it.
  path : String
}

///|
/// What `add_project` did with the folder.
#derive(Eq, Debug)
pub(all) enum Addition {
  /// The folder is now at the end of the list.
  New
  /// The folder was in the list, and the list did not change.
  AlreadyListed
}

///|
fn Addition::json_name(self : Addition) -> String {
  match self {
    New => "new"
    AlreadyListed => "already_listed"
  }
}

///|
pub impl ToJson for Addition with fn to_json(self) {
  Json::string(self.json_name())
}

///|
pub impl @json.FromJson for Addition with fn from_json(json, path) {
  case_of_name(json, path, [New, AlreadyListed], Addition::json_name)
}

///|
/// The reply to `add_project`.
#derive(Eq, Debug, ToJson, FromJson)
pub(all) struct AddedProject {
  project : Project
  addition : Addition
}

///|
/// A project, and the kind of view that shows it.
#derive(Eq, Debug, ToJson, FromJson)
pub(all) struct ShownProject {
  project : ProjectId
  surface : Surface
}

///|
/// The server stops.
#derive(Eq, Debug, ToJson, FromJson)
pub(all) struct Stopping {}

///|
/// The reply to `choose_folder`.
#derive(Eq, Debug, ToJson)
pub(all) struct ChosenFolder {
  /// The path of the folder, or `None` if the user chose no folder. It is free
  /// text for `add_project`.
  path : String?
}

///|
pub impl @json.FromJson for ChosenFolder with fn from_json(json, path) {
  { path: optional_field(json, path, "path"), }
}

///|
/// Gives the instance and the version of the server.
pub let app_info : @proton_contract.Command[
  @proton_contract.EmptyRequest,
  AppInfo,
] = @proton_contract.command("app_info")

///|
/// Gives the projects in the order of the list.
pub let list_projects : @proton_contract.Command[
  @proton_contract.EmptyRequest,
  Array[Project],
] = @proton_contract.command("list_projects")

///|
/// Adds a folder to the project list.
pub let add_project : @proton_contract.Command[AddProject, AddedProject] = @proton_contract.command(
  "add_project",
)

///|
/// Records that the user opens a project.
pub let open_project : @proton_contract.Command[ProjectId, Project] = @proton_contract.command(
  "open_project",
)

///|
/// Removes a project from the list. The folder and its files stay.
pub let remove_project : @proton_contract.Command[ProjectId, Project] = @proton_contract.command(
  "remove_project",
)

///|
/// Tells the views of one kind to show a project.
pub let show_project : @proton_contract.Command[ShownProject, Project] = @proton_contract.command(
  "show_project",
)

///|
/// Stops the server of the instance.
pub let stop_app : @proton_contract.Command[
  @proton_contract.EmptyRequest,
  Stopping,
] = @proton_contract.command("stop_app")

///|
/// The project list changed. The payload is the new list.
pub let projects_changed : @proton_contract.Event[Array[Project]] = @proton_contract.event(
  "projects_changed",
)

///|
/// A caller used `show_project`.
pub let project_shown : @proton_contract.Event[ShownProject] = @proton_contract.event(
  "project_shown",
)

///|
/// The server stops.
pub let app_stopping : @proton_contract.Event[Stopping] = @proton_contract.event(
  "app_stopping",
)

///|
/// Asks the user for a folder in a dialog of the system. Only the window of the
/// desktop app has this command. The server does not have it.
pub let choose_folder : @proton_contract.Command[
  @proton_contract.EmptyRequest,
  ChosenFolder,
] = @proton_contract.command("choose_folder")

///|
/// The names of the commands that the server has.
pub let server_commands : ReadOnlyArray[String] = [
  app_info.name(),
  list_projects.name(),
  add_project.name(),
  open_project.name(),
  remove_project.name(),
  show_project.name(),
  stop_app.name(),
]

///|
/// The name of a command or an event between the server and a view: `app:` and
/// the name. Proton uses the same form.
pub fn wire_name(name : String) -> String {
  "app:" + name
}

///|
pub extend AppInfo with Eq::{not_equal, equal}

///|
pub extend AppInfo with @debug.Debug::{to_repr}

///|
pub extend AppInfo with ToJson::{to_json}

///|
pub extend AppInfo with @json.FromJson::{from_json}

///|
pub extend AddProject with Eq::{not_equal, equal}

///|
pub extend AddProject with @debug.Debug::{to_repr}

///|
pub extend AddProject with ToJson::{to_json}

///|
pub extend AddProject with @json.FromJson::{from_json}

///|
pub extend Addition with Eq::{not_equal, equal}

///|
pub extend Addition with @debug.Debug::{to_repr}

///|
pub extend Addition with ToJson::{to_json}

///|
pub extend Addition with @json.FromJson::{from_json}

///|
pub extend AddedProject with Eq::{not_equal, equal}

///|
pub extend AddedProject with @debug.Debug::{to_repr}

///|
pub extend AddedProject with ToJson::{to_json}

///|
pub extend AddedProject with @json.FromJson::{from_json}

///|
pub extend ShownProject with Eq::{not_equal, equal}

///|
pub extend ShownProject with @debug.Debug::{to_repr}

///|
pub extend ShownProject with ToJson::{to_json}

///|
pub extend ShownProject with @json.FromJson::{from_json}

///|
pub extend Stopping with Eq::{not_equal, equal}

///|
pub extend Stopping with @debug.Debug::{to_repr}

///|
pub extend Stopping with ToJson::{to_json}

///|
pub extend Stopping with @json.FromJson::{from_json}

///|
pub extend ChosenFolder with Eq::{not_equal, equal}

///|
pub extend ChosenFolder with @debug.Debug::{to_repr}

///|
pub extend ChosenFolder with ToJson::{to_json}

///|
pub extend ChosenFolder with @json.FromJson::{from_json}