/*************************************************************
 * By: Coralys Cubero Rivera
 * Date: 2019
 *************************************************************/

package uprrp.tania;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import org.json.JSONException;
import org.json.JSONObject;

public class ForgotPasswordActivity extends AppCompatActivity {

    private String recovery_email;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgot_password);

        //Let's get the email provided by the user so we can help them reset their password
        final EditText emailRecoveryEditText = findViewById(R.id.editTextEmailResetPassword);
        recovery_email = emailRecoveryEditText.getText().toString();

        final JSONObject recoveryEmailJSON = new JSONObject();

        //Let's send the provided email to the server
        Button sendEmailRecoveryButton = findViewById(R.id.buttonSendEmail);
        sendEmailRecoveryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    recovery_email = emailRecoveryEditText.getText().toString();
                    //If we have an email, then we send it. Otherwise, the user needs to provide an email.
                    if (!recovery_email.equals("")) {
                        recoveryEmailJSON.put("email", recovery_email);
                        SendRecoveryEmailToServer sendRecoveryEmailToServer = new SendRecoveryEmailToServer(getApplicationContext());
                        sendRecoveryEmailToServer.execute(recoveryEmailJSON.toString());
                    }
                    else{
                        Toast.makeText(getApplicationContext(), "Please enter your email to reset your password.", Toast.LENGTH_LONG);
                    }
                }
                catch(JSONException e){
                    e.getMessage();
                }

            }
        });



    }
}
