/*************************************************************
 * 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.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 ExperienceRegistrationActivity  extends AppCompatActivity {

    String device_token;
    String id_experience;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_experience_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();
            }
        });

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

        final JSONObject experienceCodeJSON = new JSONObject();

        final Button enterExperience = findViewById(R.id.buttonEnterExperience);
        enterExperience.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    if (!id_experience.equals("")){
                        experienceCodeJSON.put("token", device_token);
                        experienceCodeJSON.put("id_experiencia", id_experience);


                        Log.d("EXPERIENCE CODE JSON", experienceCodeJSON.toString());

                        SendActivateExperienceToServer sendActivateExperienceToServer = new SendActivateExperienceToServer(getApplicationContext());
                        sendActivateExperienceToServer.execute(experienceCodeJSON.toString());

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


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