package uprrp.tania.activities;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
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 uprrp.tania.R;
import uprrp.tania.networking.SendRecoveryEmail;
import uprrp.tania.utils.URLEventListener;

public class ForgotPasswordActivity extends AppCompatActivity {

    private static final String TAG = "ForgotPasswordActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        // Constructor stuff
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgot_password);

        // Change all caps text to normal capitalization and add onClick listeners
        final Button sendEmailRecoveryButton = findViewById(R.id.buttonSendEmail);
        sendEmailRecoveryButton.setTransformationMethod(null); // TODO: this is a workaround I found, any other acceptable solution is welcome
        sendEmailRecoveryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendRecoveryEmail();
            }
        });

    }

    private void sendRecoveryEmail() {

        final EditText emailRecoveryEditText = findViewById(R.id.editTextEmailRecovery);
        String email = emailRecoveryEditText.getText().toString();

        // Email and password are needed (make sure user knows); else, proceed
        if(email.equals("")) {
            Toast.makeText(getApplicationContext(), "Please enter your email to reset your password.", Toast.LENGTH_LONG).show();
        } else {

            // Initiate progress dialog
            // TODO: find substitute for this deprecated dialog box
            final ProgressDialog progressDialog = ProgressDialog.show(ForgotPasswordActivity.this,
                    "Verifying Email",
                    getString(R.string.progressDialogDescriptionText));

            // Define task
            SendRecoveryEmail sendRecoveryEmailTask = new SendRecoveryEmail(new URLEventListener() {
                @Override
                public void onSuccess() {
                    progressDialog.dismiss();
                    Toast.makeText(getApplicationContext(), "Thanks!\nWe'll send you an email to reset your password.", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(ForgotPasswordActivity.this, GettingStartedActivity.class);
                    startActivity(intent);
                }

                @Override
                public void onFailure(Exception e) {
                    progressDialog.dismiss();
                    Toast.makeText(getApplicationContext(), "Error!\nPlease try again (make sure to write your email correctly)", Toast.LENGTH_SHORT).show();
                    Log.e(TAG, "Couldn't send recovery email to server!");
                    e.printStackTrace();
                }
            });

            // Start task
            sendRecoveryEmailTask.execute(email);

        }

    }

}
