/* (non-Javadoc)
   * @see android.app.Activity#onCreate(android.os.Bundle)
   */
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.text_dialog);

    TextView title = (TextView) findViewById(R.id.dialogPromptTitle);
    title.setText(R.string.enterIpAddress);

    InetAddress currentIP = Util.getLocalIpAddress();

    // Send back to main menu if not connected
    if (currentIP == null) {
      setResult(RESULT_CANCELED);
      finish();
    }

    // If not IPv4, set as IPv6
    if ((currentIP != null) && !InetAddressUtils.isIPv4Address(currentIP.getHostAddress())) {
      isIPv4 = false;
    }

    final EditText textView = (EditText) findViewById(R.id.dialogPromptTextbox);

    if (isIPv4) {
      textView.setHint(R.string.ipv4Address);
    } else {
      textView.setHint(R.string.ipv6Address);
      textView.setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
    }

    textView.setOnEditorActionListener(this);

    // create button for the view
    Button ok = (Button) findViewById(R.id.ok);
    ok.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View v) {
            addressEntered();
          }
        });
  }
  /* (non-Javadoc)
   * @see com.worthwhilegames.cardgames.shared.PlayerController#handleBroadcastReceive(android.content.Context, android.content.Intent)
   */
  @Override
  public void handleBroadcastReceive(Context context, Intent intent) {
    String action = intent.getAction();

    if (ConnectionConstants.MESSAGE_RX_INTENT.equals(action)) {
      String object = intent.getStringExtra(ConnectionConstants.KEY_MESSAGE_RX);
      int messageType = intent.getIntExtra(ConnectionConstants.KEY_MESSAGE_TYPE, -1);

      if (Util.isDebugBuild()) {
        Log.d(TAG, "message: " + object);
      }

      switch (messageType) {
        case MSG_SETUP:
          // Parse the Message if it was to start the game over
          cardHand.removeAll(cardHand);
          playerContext.removeAllCards();
          try {
            JSONArray arr = new JSONArray(object);
            for (int i = 0; i < arr.length(); i++) {
              JSONObject obj = arr.getJSONObject(i);
              int suit = obj.getInt(KEY_SUIT);
              int value = obj.getInt(KEY_VALUE);
              int id = obj.getInt(KEY_CARD_ID);
              playerContext.addCard(new Card(suit, value, id));
            }
          } catch (JSONException ex) {
            ex.printStackTrace();
          }
          setButtonsEnabled(false);
          isTurn = false;
          cardSuggestedId = -1;
          break;
        case FIRST_ROUND_BETTING: /* purposely have both here */
        case SECOND_ROUND_BETTING:
          mySM.sayBet(playerName);
          isTurn = true;
          this.setCardLead(object);
          currentState = messageType;
          if (currentState == SECOND_ROUND_BETTING) {
            trumpSuit = -1;
          } else {
            trumpSuit = cardLead.getSuit();
          }

          // start select bet activity for round 1
          // start select bet activity to let the player bet
          isBettingNow = true;
          bettingView();
          isTurn = true;
          setButtonsEnabled(true);
          break;
        case PLAY_LEAD_CARD:
          playingView();
          mySM.sayTurn(playerName);
          currentState = PLAY_LEAD_CARD;
          setButtonsEnabled(true);
          isTurn = true;
          break;
        case PICK_IT_UP:
          currentState = PICK_IT_UP;
          mySM.sayPickItUp(playerName);
          try {
            JSONObject obj = new JSONObject(object);
            int suit = obj.getInt(KEY_SUIT);
            int value = obj.getInt(KEY_VALUE);
            int id = obj.getInt(KEY_CARD_ID);
            playerContext.addCard(new Card(suit, value, id));
          } catch (JSONException ex) {
            ex.printStackTrace();
          }
          isTurn = true;
          dealerDiscardView();
          break;

        case MSG_PLAY_CARD:
          playingView();
          mySM.sayTurn(playerName);
          this.setCardLead(object);
          currentState = MSG_PLAY_CARD;

          setButtonsEnabled(true);
          isTurn = true;
          break;
        case Constants.MSG_SUGGESTED_CARD:
          if (isTurn && object != null && isPlayAssistMode) {
            try {
              JSONObject obj = new JSONObject(object);
              int id = obj.getInt(Constants.KEY_CARD_ID);
              cardSuggestedId = id;

              // Let the UI know which card was suggested
              int selectedId = -1;
              if (cardSelected != null) {
                selectedId = cardSelected.getIdNum();
              }
              playerContext.setSelected(selectedId, cardSuggestedId);
            } catch (JSONException ex) {
              ex.printStackTrace();
            }
          }
        case MSG_REFRESH:
          // Parse the refresh Message
          try {
            JSONArray arr = new JSONArray(object);
            JSONObject refreshInfo = arr.getJSONObject(0);
            isTurn = refreshInfo.getBoolean(Constants.KEY_TURN);
            currentState = refreshInfo.getInt(KEY_CURRENT_STATE);
            playerName = refreshInfo.getString(Constants.KEY_PLAYER_NAME);
            trumpSuit = refreshInfo.getInt(TRUMP);
            // add more refresh info here

            playerContext.removeAllCards();

            JSONObject obj = arr.getJSONObject(1);
            int suit = obj.getInt(KEY_SUIT);
            int value = obj.getInt(KEY_VALUE);
            int id = obj.getInt(KEY_CARD_ID);
            cardLead = new Card(suit, value, id);

            // the 2nd through however many are the cards of the player
            for (int i = 2; i < arr.length(); i++) {
              obj = arr.getJSONObject(i);
              suit = obj.getInt(KEY_SUIT);
              value = obj.getInt(KEY_VALUE);
              id = obj.getInt(KEY_CARD_ID);
              playerContext.addCard(new Card(suit, value, id));
            }
          } catch (JSONException ex) {
            ex.printStackTrace();
          }

          if ((currentState == FIRST_ROUND_BETTING || currentState == SECOND_ROUND_BETTING)) {
            if (isTurn && isBettingNow) {
              // don't want to open 2 betting windows
              break;
            } else if (isTurn && !isBettingNow) {
              bettingView();
              setButtonsEnabled(true);
            } else {
              playingView();
              isTurn = false;
              cardSuggestedId = -1;
              setButtonsEnabled(isTurn);
              isBettingNow = true;
            }
            break;
          } else if (isTurn && currentState == PICK_IT_UP) {
            dealerDiscardView();
          } else {
            playingView();
          }
          setButtonsEnabled(isTurn);
          cardSelected = null;
          break;
        case ROUND_OVER:
          currentState = ROUND_OVER;
          break;
        case MSG_WINNER:
          playerContext.unregisterReceiver();
          Intent winner = new Intent(playerContext, GameResultsActivity.class);
          winner.putExtra(GameResultsActivity.IS_WINNER, true);
          playerContext.startActivityForResult(winner, QUIT_GAME);
          break;
        case MSG_LOSER:
          playerContext.unregisterReceiver();
          Intent loser = new Intent(playerContext, GameResultsActivity.class);
          loser.putExtra(GameResultsActivity.IS_WINNER, false);
          playerContext.startActivityForResult(loser, QUIT_GAME);
          break;
      }
    }
  }