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

package uprrp.tania;

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 com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;

import org.json.JSONObject;

public class ResetPassword extends AppCompatActivity {

    String device_token;
    String forgotPasswordKey;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_password);

        //Let's get the device's token
        FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(new OnSuccessListener<InstanceIdResult>() {
            @Override
            public void onSuccess(InstanceIdResult instanceIdResult) {
                device_token = instanceIdResult.getToken();
            }
        });

        Intent intent = getIntent();
        if(Intent.ACTION_VIEW.equals(intent.getAction())){
            Uri uri = intent.getData();
            assert uri != null;
            forgotPasswordKey = uri.getQueryParameter("id");
        }


        final EditText newPassword = findViewById(R.id.editTextNewPassword);
        final EditText confirmPassword = findViewById(R.id.editTextConfirmPassword);

        final JSONObject resetPasswordJSON = new JSONObject();

        final Button resetPassword = findViewById(R.id.buttonResetPassword);
        resetPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    if (newPassword.getText().toString().equals(confirmPassword.getText().toString())){
                        resetPasswordJSON.put("token", device_token);
                        resetPasswordJSON.put("password", newPassword.getText().toString());
                        resetPasswordJSON.put("forgotPasswordKey", forgotPasswordKey);

                        Log.d("RESET PASSWORD JSON", resetPasswordJSON.toString());

                        SendResetPasswordToServer sendResetPasswordToServer = new SendResetPasswordToServer(getApplicationContext());
                        sendResetPasswordToServer.execute(resetPasswordJSON.toString());


                    }
                    else{
                        Toast.makeText(ResetPassword.this, "The password and confirmation must match!", Toast.LENGTH_LONG).show();
                    }


                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }
}
