package fr.svpro.radiomercure;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MailActivity extends AppCompatActivity {

    private EditText etSujet, etMessage;
    private Button btnEnvoyer;
    private static final String EMAIL_TO = "regie@radiomercure.fr";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.form_mail);

        etSujet = findViewById(R.id.etSujet);
        etMessage = findViewById(R.id.etMessage);
        btnEnvoyer = findViewById(R.id.btnEnvoyer);

        btnEnvoyer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Champ sujet non vide
                if (TextUtils.isEmpty(etSujet.getText().toString().trim())){
                    etSujet.setError(getString(R.string.et_err));
                } else {
                    envoiMail();
                }

            }
        });

    }

    protected void envoiMail() {
        String[] TO = {EMAIL_TO};
        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent.setData(Uri.parse("mailto:"));
        emailIntent.setType("text/plain");
        emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, etSujet.getText().toString());
        emailIntent.putExtra(Intent.EXTRA_TEXT, etMessage.getText().toString());

        try {
            startActivity(Intent.createChooser(emailIntent, "Envoi de votre message..."));
            finish();
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(this, R.string.mailclient_err, Toast.LENGTH_SHORT).show();
        }
    }

}