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

package uprrp.tania;

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.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.JSONException;
import org.json.JSONObject;


public class LoginAccount extends AppCompatActivity {

    private JSONObject recoverAccount = new JSONObject();
    private String device_token;

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

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

        final EditText emailText = findViewById(R.id.emailTextRecover);
        final EditText passwordText = findViewById(R.id.passwordTextRecover);

        final Button recoverButton = findViewById(R.id.recoverButton);
        recoverButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {

                    //As long as the email and password have been provided, then we can log in
                    if (!emailText.getText().toString().equals("")&& !passwordText.getText().toString().equals("")) {
                        recoverAccount.put("email", emailText.getText());
                        recoverAccount.put("password", passwordText.getText());
                        recoverAccount.put("token", device_token);

                        //Log.d("LOGIN ACCOUNT JSON", recoverAccount.toString());
                        new SendLoginToServer(getApplicationContext()).execute(recoverAccount.toString());

                    } else {
                        Toast.makeText(LoginAccount.this, "All fields are needed to complete the login.", Toast.LENGTH_LONG).show();
                    }


                } catch (JSONException e) {
                    Log.e("LOGIN ACCOUNT ERROR", e.getMessage());

                }
            }

        });

        //If by some chance they don't remember their password, then lets take them to the recovery activity
        final Button forgotPasswordButton = findViewById(R.id.forgotPasswordButton);
        forgotPasswordButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent needsRegisterIntent = new Intent(LoginAccount.this, ForgotPasswordActivity.class);
                startActivity(needsRegisterIntent);

            }
        });
    }
}
