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

package uprrp.tania;

import android.app.DatePickerDialog;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
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;
import java.util.Calendar;
import java.util.Objects;

public class RegisterActivity extends AppCompatActivity {

    private static final String TAG = "Cognito";
    private String device_token;
    private JSONObject registrationInfo = new JSONObject();
    private DatePickerDialog.OnDateSetListener mDateSetListener;
    private String name;

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

        //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 nameEditText = findViewById(R.id.nameEditText);

        final TextView birthDateText = findViewById(R.id.birthDateText);

        final RadioButton maleGender = findViewById(R.id.maleRadioButton);
        final RadioButton femaleGender = findViewById(R.id.femaleRadioButton);
        final RadioButton notSpecifyGender = findViewById(R.id.notSayRadioButton);
        final RadioButton otherGender = findViewById(R.id.otherRadioButton);

        final RadioButton partTime = findViewById(R.id.partTimeRadioButton);
        final RadioButton fullTime = findViewById(R.id.fullTimeRadioButton);
        final RadioButton noJob = findViewById(R.id.noJobRadioButton);

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

        birthDateText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Calendar cal = Calendar.getInstance();
                int year = cal.get(Calendar.YEAR);
                int month = cal.get(Calendar.MONTH);
                int day = cal.get(Calendar.DAY_OF_MONTH);

                DatePickerDialog dialog = new DatePickerDialog(
                        RegisterActivity.this,
                        android.R.style.Theme_Holo_Light_Dialog_MinWidth,
                        mDateSetListener,
                        year, month, day);
                Objects.requireNonNull(dialog.getWindow()).setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                dialog.show();
            }
        });

        mDateSetListener = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                month = month + 1;
                Log.d(TAG, "onDateSet: mm/dd/yyy: " + month + "/" + day + "/" + year);

                String date = month + "/" + day + "/" + year;
                birthDateText.setText(date);
            }
        };


        Button buttonRegister = findViewById(R.id.registerButton);

        buttonRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {

                    if (nameEditText != null) {
                        name = nameEditText.getText().toString();
                    }
                    if (birthDateText.getText() != null && emailText.getText() != null && passwordText.getText() != null && (maleGender.isChecked() || femaleGender.isChecked() || notSpecifyGender.isChecked() || otherGender.isChecked()) && (partTime.isChecked() || fullTime.isChecked() || noJob.isChecked())) {
                        registrationInfo.put("birthDate", birthDateText.getText().toString());
                        registrationInfo.put("email", emailText.getText().toString());
                        registrationInfo.put("password", passwordText.getText().toString());

                        if (maleGender.isChecked()) {
                            registrationInfo.put("gender", "Male");
                        } else if (femaleGender.isChecked()) {
                            registrationInfo.put("gender", "Female");
                        } else if (notSpecifyGender.isChecked()) {
                            registrationInfo.put("gender", "Prefer not to say");
                        } else if (otherGender.isChecked()){
                            registrationInfo.put("gender", "Other");
                        }

                        if (partTime.isChecked()) {
                            registrationInfo.put("hasJob", "Part time");
                        } else if (fullTime.isChecked()) {
                            registrationInfo.put("hasJob", "Full time");
                        } else if (noJob.isChecked()) {
                            registrationInfo.put("hasJob", "No job");
                        }
                        registrationInfo.put("token", device_token);
                    } else {
                        Toast.makeText(RegisterActivity.this, "All the fields are needed to complete registration.", Toast.LENGTH_LONG).show();
                    }

                    SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
                    SharedPreferences.Editor editor = prefs.edit();
                    editor.putString("consentName", name);
                    editor.apply();

                    Log.d("REGISTER ACTIVITY", registrationInfo.toString());
                    new SendRegistrationToServer(getApplicationContext()).execute(registrationInfo.toString());

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

}



