/**
   * Get the duration of current ending voice communication Verifica se foi este o telemovel que
   * iniciou a chamada e se foi, obtem a sua duração e preenche o DTO que lhe é passado.
   *
   * @param voice
   */
  public Voice endCallerVoice_VerifyAndGetDuration(Voice voice) throws IncompatibleModeException {
    // se está able, é porque não foi ele que iniciou a chamada
    final boolean ableToCommunicate = this.getMode().ableToSendVoice();
    if (ableToCommunicate) {
      throw new IncompatibleModeException(
          "Cannot End Call: No call being made because is able to comunicate",
          this.getOperator().getSeqNumber());
    }

    if (!(this.getIsCallInitiator())) {
      throw new IncompatibleModeException(
          "Cannot End Call: Not Caller, flag callinitiator not set",
          this.getOperator().getSeqNumber());
    }
    // se o iniciador foi este numero de telemovel e o receptor o "speakingTo"
    if (voice.getReceiverID().equals(this.getSpeakingTo())) {

      // TimeStamp do fim da chamada
      DateTime endTimeVoice = new DateTime();

      // Calcula a duracao em segundos da chamada
      int sec = Seconds.secondsBetween(super.getVoiceInitTime(), endTimeVoice).getSeconds();

      // System.out.println("A chamada teve duranção: " + sec);
      // Acrescenta a duracao a chamada
      voice.setDuration(sec);
      return voice;
    } else {
      throw new IncompatibleModeException(
          "Cannot End Call: Not same destination", this.getOperator().getSeqNumber());
    }
  }
  /**
   * Ends the receiver's voice communication
   *
   * @param voice The ending voice communication
   * @throws IncompatibleModeException the incompatible mode for communication exception
   */
  public void endReceiverVoiceCommunication(Voice voice) throws IncompatibleModeException {
    final boolean ableToCommunicate = this.getMode().ableToReceiveVoice();
    System.out.println(voice.getDuration());
    if (ableToCommunicate) {
      throw new IncompatibleModeException(
          "Cannot End Call: No call being received because its able to receive",
          this.getOperator().getSeqNumber());
    }

    // se o iniciador foi este numero de telemovel e o receptor o "speakingTo"
    if (!this.getIsCallInitiator() && voice.getCallerID().equals(this.getSpeakingTo())) {
      addReceivedVoice(voice);
      // (forca porque o busy nao tolera mudanca de estado)
      this.setMode(this.getModeBeforeCall());
      this.setSpeakingTo(null);

    } else {
      throw new IncompatibleModeException(
          "Cannot End Call: Not Receiver", this.getOperator().getSeqNumber());
    }
  }
  /**
   * 1º INVOCAR O endCallerVoice_VerifyAndGetDuration. Ends the caller's voice communication. Call
   * endCallerVoiceGetDuration before to get the duration of call.
   *
   * @param voice
   * @return voice uptaded voice with the duration of the call
   * @throws IncompatibleModeException the incompatible mode for communication exception
   */
  public Voice endCallerVoiceCommunication(Voice voice) throws IncompatibleModeException {
    this.setBalance(getBalance() - voice.getCost());

    // Guardar a chamada feita
    addMadeVoice(voice);
    // Guardar a ultima chamada
    super.setLastComunication(voice);

    // repõe estado antes da chamada (forca porque o busy nao tolera mudanca de
    // estado)
    this.setMode(this.getModeBeforeCall());

    this.setSpeakingTo(null);

    // retorna a comunicacao actualizada (com duracao)
    return voice;
  }