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.RatingBar;
import android.widget.Toast;
import com.muzima.domain.Credentials;
import com.muzima.R;
import com.muzima.utils.StringUtils;
import com.muzima.utils.ThemeUtils;


public class FeedbackActivity extends BaseActivity {
    private Button sendButton;
    private Button cancelButton;
    private EditText feedbackText;
    private RatingBar feedbackRatingBar;

    private static final String EMAIL_TO = "help@muzima.org";
    private static final String SUBJECT = "Feedback";
    private final ThemeUtils themeUtils = new ThemeUtils();

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

    @Override
    protected void onResume() {
        super.onResume();
        themeUtils.onResume(this);
    }

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

            private String composeMessage() {
                String sender = String.join(" ", "Sender:", getUserName());
                String rating = String.join(" ", "Rating:", String.valueOf(feedbackRatingBar.getRating()));
                String feedback = String.join(" ", "Message:", feedbackText.getText().toString());
                String message = String.join("\n", sender, rating, feedback);
                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, SUBJECT);
                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) {
                FeedbackActivity.this.onBackPressed();
            }
        });
    }

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

    private void initViews() {
        sendButton = findViewById(R.id.send);
        cancelButton = findViewById(R.id.cancel);
        feedbackText = findViewById(R.id.feedback_message);
        feedbackRatingBar = findViewById(R.id.question_rating);
    }

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


@@ -1908,33 +1908,16 @@
 sender =
- String.join(%22 %22,
  %22Sender
@@ -1917,18 +1917,20 @@
 %22Sender:
-%22,
+ %22 +
  getUser
@@ -1935,17 +1935,16 @@
 erName()
-)
 ;%0D%0A     
@@ -1969,33 +1969,16 @@
 rating =
- String.join(%22 %22,
  %22Rating
@@ -1978,18 +1978,21 @@
 %22Rating:
-%22,
+ %22 + 
  String.
@@ -2029,17 +2029,16 @@
 ating())
-)
 ;%0D%0A     
@@ -2069,25 +2069,8 @@
 ck =
- String.join(%22 %22,
  %22Me
@@ -2075,18 +2075,20 @@
 Message:
-%22,
+ %22 +
  feedbac
@@ -2109,25 +2109,24 @@
 ).toString()
-)
 ;%0D%0A         
@@ -2153,41 +2153,38 @@
 e = 
-String.join(%22%5Cn%22, sender, rating,
+sender + %22%5Cn%22 + rating+ %22%5Cn%22 +
  fee
@@ -2180,33 +2180,32 @@
  %22%5Cn%22 + feedback
-)
 ;%0D%0A             

change string concatenation for avoiding app crash