/**
   * Initialize the buttons declared in the xml. In this case just the MAIN button. Main button: add
   * a touch listener, when clicked should take you back to the main conversation
   */
  public void initializeButtons() {
    // set listener to main button
    // Button mainButton = (Button) findViewById(R.id.main_button);
    /*
     * mainButton.setOnTouchListener(new View.OnTouchListener() {
     *
     * @Override public boolean onTouch(View view, MotionEvent evt) { switch
     * (evt.getAction()) { case MotionEvent.ACTION_DOWN: break; case
     * MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_UP:
     * if(!(screen.getSpace()==Space.getMainSpace())){
     * MainApplication.screen
     * .getSpaceViewController().changeSpace(Space.getMainSpace()); } break;
     * } return false; } });
     */
    try {
      // sidechat 1
      // Log.v("C", "Problem on adding space");
      Space newSpace = SpaceController.addSpace(Space.getMainSpace().getContext());

      side1 = new PrivateSpaceIconView(Space.getMainSpace().getContext(), newSpace);
      newSpace.getSpaceController().setPSIV(side1);
      // main chat
      PrivateSpaceIconView main =
          new PrivateSpaceIconView(Space.getMainSpace().getContext(), Space.getMainSpace());
      Space.getMainSpace().getSpaceController().setPSIV(main);
      // sidechat 2
      Space newSpace2 = SpaceController.addSpace(Space.getMainSpace().getContext());
      side2 = new PrivateSpaceIconView(Space.getMainSpace().getContext(), newSpace2);
      newSpace2.getSpaceController().setPSIV(side2);
    } catch (XMPPException e) {
      // TODO Auto-generated catch block

      e.printStackTrace();
    }

    TextView logo = (TextView) findViewById(R.id.logo);
    logo.setClickable(true);
    logo.setOnClickListener(this.logoOnClickListener());

    // ImageView soundButton = (ImageView) findViewById(R.id.sound_button);
    // soundButton.setClickable(true);
    // soundButton.setOnClickListener(this.getSoundButtonOnClickListener());
    /*
     * Log.v(TAG, "Clicked on MENU key"); LayoutInflater inflater =
     * (LayoutInflater) MainApplication.this
     * .getSystemService(Context.LAYOUT_INFLATER_SERVICE); MenuView menuView
     * = new MenuView(inflater); menuView.launch();
     */

    ImageView spatializationButton = (ImageView) findViewById(R.id.auto_spatialization);
    spatializationButton.setClickable(true);
    spatializationButton.setOnClickListener(this.getSpatializationListener());
  }
  /**
   * Called when application is first created. <b>Set up:</b>
   *
   * <ol>
   *   <li>Create and open the main space and put the primary user (the user that started this
   *       application) into the space
   *   <li>Initialize button functionality: main button
   * </ol>
   *
   * <b>Assumptions:</b>
   *
   * <ul>
   *   <li>The main space is created by the primary user
   *   <li>XMPPConnection is established and authorized before this Activity is called
   * </ul>
   */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    if (D) Log.d(TAG, "onCreate - Started the MainApplication activity");
    super.onCreate(savedInstanceState);
    ContextTracker.setContext(this);

    // Open up the layout specified by the main XML
    setContentView(R.layout.main);
    // Change the parameters of the appearance according to screen size
    // adjustLayoutParams();
    // This spaceview already created for you in the XML file, so retrieve
    // it
    screen = (SpaceView) findViewById(R.id.space_view);
    // Check if the mainspace was already created
    if (Space.getMainSpace() == null) {
      // Obtain username used to log into the application
      Intent start_intent = getIntent();
      username = start_intent.getStringExtra(Network.KEY_USERNAME);
      // Create instance of primary user
      if (userPrimary == null) {
        userPrimary = new User(username, username.split("@")[0], R.drawable.question);
      }
      try {
        // create the mainspace
        SpaceController.createMainSpace(this);

        // create an empty private space
        // SpaceController.addSpace(this);

        // TODO add private space preview
      } catch (XMPPException e) {
        // Log.e(TAG, "onCreate - Error (" + e.getXMPPError().getCode()
        // + ") " + e.getXMPPError().getMessage());
        e.printStackTrace();
      }
      screen.setSpace(Space.getMainSpace());
      // screen.getSpaceViewController().changeSpace(Space.getMainSpace());
      Space.getMainSpace().setScreenOn(true);
      // Space.getMainSpace().setEntered(true);

      font = Typeface.createFromAsset(getAssets(), Values.font);
      // Button mainButtonText = (Button) findViewById(R.id.main_button);
      // mainButtonText.setTypeface(font);

    }
    // this.plusButtonSetUp(0);
    initializeButtons();

    // Initializes the onKeyListener to record keypad events
    screen.setOnKeyListener(onKeyListener);

    // DEBUG: create User object to test invitations and kickouts
    debug = new User("opencommsec@" + Network.DEFAULT_HOSTNAME, "opencommsec", 0);
    // for (Space s : Space.allSpaces) Log.v(TAG, s.getRoomID());
    debug1 = new User("mucopencomm@" + Network.DEFAULT_HOSTNAME, "mucopencomm", 0);

    // Change screen dimensions to 480x800
    Values.setValues(480, 800);
  }
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
          if (!D) return true;
          if (event.getAction() != KeyEvent.ACTION_DOWN) {
            return true;
          }
          switch (keyCode) {
            case KeyEvent.KEYCODE_1:
              {
                Log.v(TAG, "pressed 1 key - confirmation screen");
                LayoutInflater inflater =
                    (LayoutInflater)
                        MainApplication.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                ConfirmationView confirmationView = new ConfirmationView(inflater);
                confirmationView.launch();
                break;
              }
            case KeyEvent.KEYCODE_2:
              {
                Log.v(TAG, "pressed 2 key - invitation screen");
                LayoutInflater inflater =
                    (LayoutInflater)
                        MainApplication.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                InvitationView invitationView = new InvitationView(inflater);
                invitationView.launch();
                break;
              }
            case KeyEvent.KEYCODE_3:
              {
                Log.v(TAG, "pressed 3 key - login screen");
                LayoutInflater inflater =
                    (LayoutInflater)
                        MainApplication.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                // LoginView loginView = new LoginView(inflater);
                // loginView.launch();
                break;
              }
            case KeyEvent.KEYCODE_4:
              {
                Log.v(TAG, "pressed 4 key - tip screen");
                LayoutInflater inflater =
                    (LayoutInflater)
                        MainApplication.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                TipView tipView = new TipView(inflater);
                tipView.launch();
                break;
              }
            case KeyEvent.KEYCODE_5:
              {
                Log.v(TAG, "pressed 5 key - admin tip screen");
                LayoutInflater inflater =
                    (LayoutInflater)
                        MainApplication.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                AdminTipView adminTipView = new AdminTipView(inflater);
                adminTipView.launch();
                break;
              }
            case KeyEvent.KEYCODE_6:
              {
                Log.v(TAG, "pressed 6 key - dashboard screen");
                LayoutInflater inflater =
                    (LayoutInflater)
                        MainApplication.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                // DashboardView dashboardView = new DashboardView(inflater);
                // dashboardView.launch();
                break;
              }
            case KeyEvent.KEYCODE_7:
              {
                Log.v(TAG, "pressed 7 key - sound settings screen");
                LayoutInflater inflater =
                    (LayoutInflater)
                        MainApplication.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                SoundSettingsView soundSettingsView = new SoundSettingsView(inflater);
                soundSettingsView.launch();
                break;
              }
            case KeyEvent.KEYCODE_8:
              {
                Log.v(TAG, "Pressed 8 key - Signup");
                SignupView view = new SignupView(MainApplication.this);
                view.launch();
                break;
              }
            case KeyEvent.KEYCODE_M:
              {
                // invite a user to the mainspace. Assume inviter is owner
                int i = 0;
                Log.v(TAG, "pressed M key - invitation" + i++);
                (Space.getMainSpace().getInvitationController()).inviteUser(debug, "You're fun!");
                break;
              }
            case KeyEvent.KEYCODE_N:
              {
                Log.v(TAG, "pressed N key - kickout");
                Space.getMainSpace().getKickoutController().kickoutUser(debug, "You suck!");
                break;
              }
            case KeyEvent.KEYCODE_V:
              {
                Log.v(TAG, "pressed V key - change owner");
                Space.getMainSpace()
                    .getParticipantController()
                    .grantOwnership("opencommsec@" + Network.DEFAULT_HOSTNAME, false);
                break;
              }
            case KeyEvent.KEYCODE_Z:
              {
                Iterator<String> affiliates = Space.getMainSpace().getMUC().getOccupants();
                while (affiliates.hasNext()) {
                  String a = affiliates.next();
                  Log.v(
                      TAG,
                      a
                          + "'s affiliaton is "
                          + Space.getMainSpace().getMUC().getOccupant(a).getAffiliation());
                }
                break;
              }
            case KeyEvent.KEYCODE_J:
              {
                Log.v(TAG, "Pressed J key - join");

                Space.getMainSpace()
                    .getParticipantStatusController()
                    .joined("roomname@" + Network.DEFAULT_CONFERENCE + "/" + debug1.getNickname());
                break;
              }
            case KeyEvent.KEYCODE_S:
              {
                Log.v(TAG, "Pressed S key - create a new private space");
                try {
                  SpaceController.addSpace((Context) MainApplication.screen.getContext());
                } catch (XMPPException e) {
                  Log.v(TAG, "Can't create another private space :(");
                }
                break;
              }

            case KeyEvent.KEYCODE_MENU:
              {
                Log.v(TAG, "Clicked on MENU key");
                LayoutInflater inflater =
                    (LayoutInflater)
                        MainApplication.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                MenuView menuView = new MenuView(inflater);
                menuView.launch();
                break;
              }
            case KeyEvent.KEYCODE_B:
              {
                Log.v(TAG, "pressed B key - leave space");
                Space.getMainSpace().getParticipantController().leaveSpace(false);
                break;
              }
            case KeyEvent.KEYCODE_Q:
              {
                Log.v(TAG, "pressed Q key - you (a moderator) received an invite request");
                InvitationController ic =
                    MainApplication.screen.getSpace().getInvitationController();
                // fake invite request
                String inviteRequest =
                    ""
                        + Network.REQUEST_INVITE
                        + "@inviter"
                        + debug.getUsername()
                        + "@invitee"
                        + debug1.getUsername()
                        + "@reason"
                        + "Because you're a cool cat.";
                ic.receiveInvitationRequest(inviteRequest);
                break;
              }
            case KeyEvent.KEYCODE_W:
              {
                Log.v(TAG, "pressed W key - you received an invite to a chat");
                InvitationController ic =
                    MainApplication.screen.getSpace().getInvitationController();
                // fake invite request
                String inviteRequest =
                    ""
                        + Network.REQUEST_INVITE
                        + "@requester"
                        + debug.getUsername()
                        + "@invitee"
                        + MainApplication.userPrimary.getUsername()
                        + "@reason"
                        + "Because you're a cool cat.";
                ic.receiveInvitationRequest(inviteRequest);
                /*
                 * LoginController.xmppService.getInvitiationListener().
                 * invitationReceived(LoginController.xmppConnection, "",
                 * debug.getUsername(), "reason", "password", new
                 * Message("blah message"));
                 */
                break;
              }
            case KeyEvent.KEYCODE_E:
              {
                Log.v(TAG, "pressed E key - you (a moderator) received a kickout request");
                KickoutController kc = MainApplication.screen.getSpace().getKickoutController();
                // fake kickout request
                String kickoutRequest =
                    ""
                        + Network.REQUEST_KICKOUT
                        + "@requester"
                        + debug.getUsername()
                        + "@kickee"
                        + debug1.getUsername()
                        + "@reason"
                        + "Because you didn't give me food.";
                kc.receiveKickoutRequest(kickoutRequest);
                break;
              }
            case KeyEvent.KEYCODE_R:
              {
                Log.v(TAG, "pressed R key - you received a kickout confirmation");
                KickoutController kc = MainApplication.screen.getSpace().getKickoutController();
                // fake kickout request
                String kickoutRequest =
                    ""
                        + Network.REQUEST_KICKOUT
                        + "@requester"
                        + debug.getUsername()
                        + "@kickee"
                        + MainApplication.userPrimary.getUsername()
                        + "@reason"
                        + "Because you didn't give me food.";
                kc.receiveKickoutRequest(kickoutRequest);
                break;
              }
            case KeyEvent.KEYCODE_A:
              {
                Log.v(TAG, "pressed A key - logout");
                MainApplication.this.disconnect();
                break;
              }
            case KeyEvent.KEYCODE_H:
              {
                Log.v(TAG, "pressed H key - invite request");
                Space.getMainSpace()
                    .getInvitationController()
                    .inviteUser(debug1, Network.DEFAULT_INVITE);
                break;
              }

            case KeyEvent.KEYCODE_Y:
              {
                Log.d(TAG, "contacts button clicked");
                Intent i = new Intent();
                // LayoutInflater ifl=dashboardView.getInflater();

                LayoutInflater ifl =
                    (LayoutInflater)
                        MainApplication.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                ResetPasswordView cpv = new ResetPasswordView(ifl, MainApplication.this);
                // cpv.setContext(Space.getMainSpace().getContext());
                cpv.launch();
              }
          }
          ;
          return true;
        }