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) {
            if (resultCode == RESULT_OK) {
                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();
            } else {
                AlertDialog.Builder builder = new AlertDialog.Builder(InteractiveHelpActivity.this);
                builder
                        .setCancelable(true)
                        .setMessage(R.string.message_help_halt)
                        .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();
    }
}

@@ -1592,16 +1592,17 @@
 essage()
+ 
 %7B%0D%0A     
@@ -2846,16 +2846,17 @@
 stener()
+ 
 %7B%0D%0A     
@@ -3153,56 +3153,8 @@
  %7B%0D%0A
-            if (resultCode == RESULT_OK) %7B%0D%0A    
     
@@ -3251,36 +3251,32 @@
 );%0D%0A            
-    
 builder%0D%0A       
@@ -3260,36 +3260,32 @@
        builder%0D%0A
-    
                 
@@ -3322,36 +3322,32 @@
                 
-  
-  
 .setMessage(R.st
@@ -3367,28 +3367,24 @@
 help_sent)%0D%0A
-    
             
@@ -3512,36 +3512,32 @@
                 
-    
 public void onCl
@@ -3598,36 +3598,32 @@
                 
- 
-   
 InteractiveHelpA
@@ -3673,35 +3673,27 @@
             
-    %7D%0D%0A    
+%7D%0D%0A
             
@@ -3725,653 +3725,8 @@
 );%0D%0A
-            %7D else %7B%0D%0A                AlertDialog.Builder builder = new AlertDialog.Builder(InteractiveHelpActivity.this);%0D%0A                builder%0D%0A                        .setCancelable(true)%0D%0A                        .setMessage(R.string.message_help_halt)%0D%0A                        .setPositiveButton(getResources().getText(R.string.general_ok), new DialogInterface.OnClickListener() %7B%0D%0A                            public void onClick(DialogInterface dialog, int which) %7B%0D%0A                                InteractiveHelpActivity.this.onBackPressed();%0D%0A                            %7D%0D%0A                        %7D).create().show();%0D%0A            %7D%0D%0A
     
@@ -3769,16 +3769,17 @@
 tViews()
+ 
 %7B%0D%0A     

change response of sending message