package com.project.ilsaenterprise.account;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.project.ilsaenterprise.R;
import com.project.ilsaenterprise.SharedPref;

import java.util.HashMap;

import de.hdodenhof.circleimageview.CircleImageView;

public class ChangeName extends AppCompatActivity {

    private EditText edtName;
    private RelativeLayout btnSave;
    private CircleImageView userProfileImage;
    private DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("users")
            .child(FirebaseAuth.getInstance().getCurrentUser().getUid());

    private DatabaseReference userRef;
    private FirebaseAuth mAuth;
    private FirebaseUser userFirebase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
        setContentView(R.layout.activity_change_name);


        edtName = findViewById(R.id.nameProfile);
        btnSave = findViewById(R.id.btnSaveName);

        Bundle bundle = getIntent().getExtras();

        if(bundle.getString("name") != null){
            edtName.setText(bundle.getString("name"));
        }

        mAuth = FirebaseAuth.getInstance();
        userFirebase = mAuth.getCurrentUser();
        userRef = FirebaseDatabase.getInstance().getReference().child("users");

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateUser();
            }
        });
    }

    private void updateUser(){
        String userId = userFirebase.getUid();
        String setUserName = edtName.getText().toString();

        if(TextUtils.isEmpty(setUserName))
            Toast.makeText(this, "Mohon lengkapi data anda", Toast.LENGTH_SHORT).show();
        else
        {
            try {
                userRef.child(userId).child("namaLengkap").setValue(setUserName).addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void unused) {
                        Toast.makeText(getBaseContext(), "Data telah disimpan.", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(ChangeName.this, UserProfile.class));
                        edtName.setFocusable(false);
                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(getBaseContext(), "Gagal menyimpan data "+ e, Toast.LENGTH_SHORT).show();
                    }
                });
            }catch (Exception e){
                Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_SHORT).show();
            }

        }
    }
}