/**
   * Parse an userIDS text into a list.
   *
   * @param userIDsText the userIDs text.
   * @param homeServerSuffix the home server suffix
   * @return the userIDs list.
   */
  public static ArrayList<String> parseUserIDsList(String userIDsText, String homeServerSuffix) {
    ArrayList<String> userIDsList = new ArrayList<String>();

    if (!TextUtils.isEmpty(userIDsText)) {
      userIDsText = userIDsText.trim();

      if (!TextUtils.isEmpty(userIDsText)) {
        // they are separated by a ;
        String[] splitItems = userIDsText.split(";");

        for (int i = 0; i < splitItems.length; i++) {
          String item = splitItems[i];

          // avoid null name
          if (item.length() > 0) {
            // add missing @ or home suffix
            String checkedItem = CommonActivityUtils.checkUserId(item, homeServerSuffix);

            // not yet added ? -> add it
            if (userIDsList.indexOf(checkedItem) < 0) {
              checkedItem.trim();
              userIDsList.add(checkedItem);
            }
          }
        }
      }
    }

    return userIDsList;
  }
  public static void goToOneToOneRoom(
      final MXSession aSession,
      final String otherUserId,
      final Activity fromActivity,
      final ApiCallback<Void> callback) {
    // sanity check
    if (null == otherUserId) {
      return;
    }

    // check first if the 1:1 room already exists
    MXSession session = (aSession == null) ? Matrix.getMXSession(fromActivity, null) : aSession;

    // no session is provided
    if (null == session) {
      // get the default one.
      session = Matrix.getInstance(fromActivity.getApplicationContext()).getDefaultSession();
    }

    // sanity check
    if ((null == session) || !session.isActive()) {
      return;
    }

    final MXSession fSession = session;

    // so, list the existing room, and search the 2 users room with this other users
    String roomId = null;
    Collection<Room> rooms = session.getDataHandler().getStore().getRooms();

    for (Room room : rooms) {
      Collection<RoomMember> members = room.getMembers();

      if (members.size() == 2) {
        for (RoomMember member : members) {
          if (member.getUserId().equals(otherUserId)) {
            roomId = room.getRoomId();
            break;
          }
        }
      }
    }

    // the room already exists -> switch to it
    if (null != roomId) {
      CommonActivityUtils.goToRoomPage(session, roomId, fromActivity, null);

      // everything is ok
      if (null != callback) {
        callback.onSuccess(null);
      }
    } else {
      session.createRoom(
          null,
          null,
          RoomState.VISIBILITY_PRIVATE,
          null,
          new SimpleApiCallback<String>(fromActivity) {

            @Override
            public void onSuccess(String roomId) {
              final Room room = fSession.getDataHandler().getRoom(roomId);

              room.invite(
                  otherUserId,
                  new SimpleApiCallback<Void>(this) {
                    @Override
                    public void onSuccess(Void info) {
                      CommonActivityUtils.goToRoomPage(
                          fSession, room.getRoomId(), fromActivity, null);

                      callback.onSuccess(null);
                    }

                    @Override
                    public void onMatrixError(MatrixError e) {
                      if (null != callback) {
                        callback.onMatrixError(e);
                      }
                    }

                    @Override
                    public void onNetworkError(Exception e) {
                      if (null != callback) {
                        callback.onNetworkError(e);
                      }
                    }

                    @Override
                    public void onUnexpectedError(Exception e) {
                      if (null != callback) {
                        callback.onUnexpectedError(e);
                      }
                    }
                  });
            }

            @Override
            public void onMatrixError(MatrixError e) {
              if (null != callback) {
                callback.onMatrixError(e);
              }
            }

            @Override
            public void onNetworkError(Exception e) {
              if (null != callback) {
                callback.onNetworkError(e);
              }
            }

            @Override
            public void onUnexpectedError(Exception e) {
              if (null != callback) {
                callback.onUnexpectedError(e);
              }
            }
          });
    }
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    if (CommonActivityUtils.shouldRestartApp()) {
      CommonActivityUtils.restartApp(this);
    }

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_member_details);

    Intent intent = getIntent();
    if (!intent.hasExtra(EXTRA_ROOM_ID)) {
      Log.e(LOG_TAG, "No room ID extra.");
      finish();
      return;
    }
    mRoomId = intent.getStringExtra(EXTRA_ROOM_ID);

    if (!intent.hasExtra(EXTRA_MEMBER_ID)) {
      Log.e(LOG_TAG, "No member ID extra.");
      finish();
      return;
    }

    mMemberId = intent.getStringExtra(EXTRA_MEMBER_ID);

    mSession = getSession(intent);

    if (null == mSession) {
      Log.e(LOG_TAG, "The no session");
      finish();
      return;
    }

    mRoom = mSession.getDataHandler().getRoom(mRoomId);

    if (null == mRoom) {
      Log.e(LOG_TAG, "The room is not found");
      finish();
      return;
    }

    // find out the room member
    Collection<RoomMember> members = mRoom.getMembers();
    for (RoomMember member : members) {
      if (member.getUserId().equals(mMemberId)) {
        mMember = member;
        break;
      }
    }

    // sanity checks
    if (null == mMember) {
      Log.e(LOG_TAG, "The user " + mMemberId + " is not in the room " + mRoomId);
      finish();
      return;
    }

    mButtonsList = new ArrayList<Button>();
    mButtonsList.add((Button) findViewById(R.id.contact_button_1));
    mButtonsList.add((Button) findViewById(R.id.contact_button_2));
    mButtonsList.add((Button) findViewById(R.id.contact_button_3));
    mButtonsList.add((Button) findViewById(R.id.contact_button_4));

    // set the click listener for each button
    for (Button button : mButtonsList) {
      button.setOnClickListener(
          new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              String text = (String) ((Button) v).getText();

              final View refreshingView = findViewById(R.id.profile_mask);
              final ApiCallback callback =
                  new SimpleApiCallback<Void>(MemberDetailsActivity.this) {
                    @Override
                    public void onMatrixError(MatrixError e) {
                      if (MatrixError.FORBIDDEN.equals(e.errcode)) {
                        Toast.makeText(MemberDetailsActivity.this, e.error, Toast.LENGTH_LONG)
                            .show();
                      }

                      MemberDetailsActivity.this.refresh();
                    }

                    @Override
                    public void onSuccess(Void info) {
                      MemberDetailsActivity.this.refresh();
                    }
                  };

              // disable the buttons
              for (Button button : mButtonsList) {
                button.setEnabled(false);
              }

              if (text.equals(getResources().getString(R.string.kick))) {
                refreshingView.setVisibility(View.VISIBLE);
                mRoom.kick(mMember.getUserId(), callback);
              } else if (text.equals(getResources().getString(R.string.ban))) {
                refreshingView.setVisibility(View.VISIBLE);
                mRoom.ban(mMember.getUserId(), null, callback);
              } else if (text.equals(getResources().getString(R.string.unban))) {
                refreshingView.setVisibility(View.VISIBLE);
                mRoom.unban(mMember.getUserId(), callback);
              } else if (text.equals(getResources().getString(R.string.invite))) {
                refreshingView.setVisibility(View.VISIBLE);
                mRoom.invite(mMember.getUserId(), callback);
              } else if (text.equals(getResources().getString(R.string.chat))) {
                refreshingView.setVisibility(View.VISIBLE);
                MemberDetailsActivity.this.runOnUiThread(
                    new Runnable() {
                      @Override
                      public void run() {
                        CommonActivityUtils.goToOneToOneRoom(
                            mSession,
                            mMemberId,
                            MemberDetailsActivity.this,
                            new SimpleApiCallback<Void>(MemberDetailsActivity.this) {
                              @Override
                              public void onMatrixError(MatrixError e) {
                                if (MatrixError.FORBIDDEN.equals(e.errcode)) {
                                  Toast.makeText(
                                          MemberDetailsActivity.this, e.error, Toast.LENGTH_LONG)
                                      .show();
                                }
                                MemberDetailsActivity.this.refresh();
                              }

                              @Override
                              public void onNetworkError(Exception e) {
                                Toast.makeText(
                                        MemberDetailsActivity.this,
                                        e.getLocalizedMessage(),
                                        Toast.LENGTH_LONG)
                                    .show();
                                MemberDetailsActivity.this.refresh();
                              }

                              @Override
                              public void onUnexpectedError(Exception e) {
                                Toast.makeText(
                                        MemberDetailsActivity.this,
                                        e.getLocalizedMessage(),
                                        Toast.LENGTH_LONG)
                                    .show();
                                MemberDetailsActivity.this.refresh();
                              }
                            });
                      }
                    });
              } else if (text.equals(getResources().getString(R.string.set_power_level))) {
                String title = getResources().getString(R.string.set_power_level);
                String initText =
                    mRoom.getLiveState().getPowerLevels().getUserPowerLevel(mMemberId) + "";

                final AlertDialog alert =
                    CommonActivityUtils.createEditTextAlert(
                        MemberDetailsActivity.this,
                        title,
                        null,
                        initText,
                        new CommonActivityUtils.OnSubmitListener() {
                          @Override
                          public void onSubmit(String text) {
                            if (text.length() == 0) {
                              return;
                            }

                            int newPowerLevel = -1;

                            try {
                              newPowerLevel = Integer.parseInt(text);
                            } catch (Exception e) {
                            }

                            if (newPowerLevel >= 0) {
                              refreshingView.setVisibility(View.VISIBLE);
                              mRoom.updateUserPowerLevels(
                                  mMember.getUserId(), newPowerLevel, callback);
                            } else {
                              MemberDetailsActivity.this.refresh();
                            }
                          }

                          @Override
                          public void onCancelled() {
                            MemberDetailsActivity.this.refresh();
                          }
                        });

                MemberDetailsActivity.this.runOnUiThread(
                    new Runnable() {
                      @Override
                      public void run() {
                        alert.show();
                      }
                    });
              }
            }
          });
    }

    // load the thumbnail
    mThumbnailImageView = (ImageView) findViewById(R.id.avatar_img);

    // set the title
    mMatrixIdTextView = (TextView) findViewById(R.id.textView_matrixid);

    // refresh the activity views
    refresh();
  }