package com.project.ilsaenterprise.account;

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

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

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.project.ilsaenterprise.R;

import de.hdodenhof.circleimageview.CircleImageView;

public class ChangePhone extends AppCompatActivity {

    private EditText edtHp;
    private RelativeLayout btnSave;
    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_phone);

        edtHp = findViewById(R.id.tvNoHp);
        btnSave = findViewById(R.id.btnSaveName);

        Bundle bundle = getIntent().getExtras();
        if(bundle.getString("phone") != null){
            edtHp.setText(bundle.getString("phone"));
        }

        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 = edtHp.getText().toString();

        if(TextUtils.isEmpty(setUserName))
            Toast.makeText(this, "Mohon lengkapi data anda", Toast.LENGTH_SHORT).show();
        else
        {
            try {
                userRef.child(userId).child("noHp").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(ChangePhone.this, UserProfile.class));
                        finish();
                    }
                }).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();
            }

        }
    }
}