package uprrp.tania.activities;

import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
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.GlobalValues;
import uprrp.tania.R;
import uprrp.tania.networking.SendResetPassword;
import uprrp.tania.utils.URLEventListener;

public class ResetPasswordActivity extends AppCompatActivity {

    private static final String TAG = "ResetPasswordActivity";
    private String FORGOT_PASSWORD_KEY;

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

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

        // Fetch experience token from URL (only if we launched this Activity from Browser)
        Intent intent = getIntent();
        if(Intent.ACTION_VIEW.equals(intent.getAction())) {
            Uri uri = intent.getData();
            assert uri != null; // TODO: figure out if this causes crashes...
            FORGOT_PASSWORD_KEY = uri.getQueryParameter("forgotPasswordKey");
            Log.d(TAG, "ForgotPasswordKey is " + FORGOT_PASSWORD_KEY);
        } else {
            Log.wtf(TAG, "Activity started from somewhere other than the browser!");
        }

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

    }

    private void sendPasswordResetRequest() {

        final EditText newPasswordEditText = findViewById(R.id.editTextNewPassword);
        final EditText confirmPasswordEditText = findViewById(R.id.editTextConfirmPassword);

        String newPassword = newPasswordEditText.getText().toString();
        String confirmPassword = confirmPasswordEditText.getText().toString();

        if(newPassword.equals(confirmPassword) && !newPassword.equals("")) {

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

            // Define task
            SendResetPassword sendResetPasswordTask = new SendResetPassword(new URLEventListener() {
                @Override
                public void onSuccess() {
                    progressDialog.dismiss();
                    Toast.makeText(getApplicationContext(), "Success!\nYou can login with your new password now.", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(getApplicationContext(), GettingStartedActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }

                @Override
                public void onFailure(Exception e) {
                    progressDialog.dismiss();
                    Toast.makeText(getApplicationContext(), "An error occurred while trying to register. Please try again.", Toast.LENGTH_LONG).show();
                    Log.e(TAG, "Couldn't reset password!");
                    e.printStackTrace();
                }
            });

            // Start task
            String deviceToken = GlobalValues.getInstance().getDeviceToken();
            sendResetPasswordTask.execute(deviceToken, newPassword, FORGOT_PASSWORD_KEY);

        } else {
            Toast.makeText(ResetPasswordActivity.this, "Both fields must match and can't be blank!", Toast.LENGTH_LONG).show();
        }


    }
}
