// contact is a RevertableModel which is injected from the state's resolve data.
export class EditContactController{
  constructor($state, dialogService, Contacts, contact) {
    this.$state = $state;
    this.dialogService = dialogService;
    this.Contacts = Contacts;
    this.contact = contact.editableModel;
    this.clearDirty = () => contact.clearDirty();
  }

  /** Ask for confirmation, then delete the contact, then go to the grandparent state ('contacts') */
  remove(contact) {
    this.dialogService.confirm(`Delete contact: ${contact.name.first} ${contact.name.last}`)
        .then(() => this.Contacts.remove(contact))
        .then(this.clearDirty)
        .then(() => this.$state.go("^.^"));
  }

  /** Save the contact, then go to the grandparent state ('contacts') */
  save(contact) {
    this.Contacts.save(contact)
        .then(this.clearDirty)
        .then(() => this.$state.go("^", null, { reload: true }));
  }
}