package com.muzima.view;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import com.muzima.R;
import com.muzima.domain.Credentials;
import com.muzima.utils.StringUtils;

public class InteractiveHelpActivity extends BaseActivity {
    private static final String EMAIL_TO = "help@muzima.org";
    private static final String SUBJECT = "Asking help - ";

    private Button sendButton;
    private Button cancelButton;
    private EditText helpText;
    private Spinner options;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_interactive_help);
        initViews();
        setupListeners();
    }

    private void setupListeners() {
        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String message = helpText.getText().toString();
                if (!StringUtils.isEmpty(message)) {
                    sendEmail();
                } else {
                    helpText.setHint(getString(R.string.hint_help_prompt));
                    helpText.setHintTextColor(getResources().getColor(R.color.error_text_color));
                }
            }

            private String composeMessage() {
                String sender = String.join(" ", "Sender:", getUserName());
                String help_msg = String.join(" ", "Message:", helpText.getText().toString());
                String message = String.join("\n", sender, help_msg);
                return message;
            }

            private void sendEmail() {
                Intent emailIntent = new Intent(Intent.ACTION_SEND);
                emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{EMAIL_TO});
                emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, String.join("", SUBJECT, options.getSelectedItem().toString()));
                emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, composeMessage());
                emailIntent.setData(Uri.parse("mailto:"));
                emailIntent.setType("message/rfc822");
                if (emailIntent.resolveActivity(getPackageManager()) != null) {
                    startActivityForResult(emailIntent, 1);
                } else {
                    Toast.makeText(getApplicationContext(), R.string.no_email_client, Toast.LENGTH_SHORT).show();
                }
            }
        });

        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InteractiveHelpActivity.this.onBackPressed();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1) {
            AlertDialog.Builder builder = new AlertDialog.Builder(InteractiveHelpActivity.this);
            builder
                    .setCancelable(true)
                    .setMessage(R.string.message_help_sent)
                    .setPositiveButton(getResources().getText(R.string.general_ok), new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            InteractiveHelpActivity.this.onBackPressed();
                        }
                    }).create().show();
        }
    }

    private void initViews() {
        sendButton = findViewById(R.id.send);
        cancelButton = findViewById(R.id.cancel);
        helpText = findViewById(R.id.help_message);
        options = findViewById(R.id.spinner);
    }

    private String getUserName() {
        Credentials credentials;
        credentials = new Credentials(this);
        return credentials.getUserName();
    }
}

@@ -1627,33 +1627,16 @@
 sender =
- String.join(%22 %22,
  %22Sender
@@ -1636,18 +1636,20 @@
 %22Sender:
-%22,
+ %22 +
  getUser
@@ -1654,17 +1654,16 @@
 erName()
-)
 ;%0D%0A     
@@ -1694,25 +1694,8 @@
 sg =
- String.join(%22 %22,
  %22Me
@@ -1700,18 +1700,20 @@
 Message:
-%22,
+ %22 +
  helpTex
@@ -1734,17 +1734,16 @@
 String()
-)
 ;%0D%0A     
@@ -1774,33 +1774,23 @@
 e = 
-String.join(%22%5Cn%22, sender,
+sender + %22%5Cn%22 +
  hel
@@ -1794,17 +1794,16 @@
 help_msg
-)
 ;%0D%0A     
@@ -1839,32 +1839,168 @@
            %7D%0D%0A%0D%0A
+            private String composeSubject() %7B%0D%0A                return SUBJECT + options.getSelectedItem().toString();%0D%0A            %7D%0D%0A%0D%0A
             priv
@@ -2271,69 +2271,23 @@
 CT, 
-String.join(%22%22, SUBJECT, options.getSelectedItem().toString()
+composeSubject(
 ));%0D

change string concatenation for avoiding app crash