package com.mkmakerblog.makerchat;

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

import android.content.Intent;
import android.database.DatabaseUtils;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class RegisterActivity extends AppCompatActivity {
private Toolbar actionbarRegister;
private EditText txtEmail,txtPassword;
private Button btnRegister;
private FirebaseAuth auth;
private DatabaseReference myRef;


public void init(){
    myRef = FirebaseDatabase.getInstance().getReference();
    auth = FirebaseAuth.getInstance();
    actionbarRegister = (Toolbar) findViewById(R.id.actionbarRegister);
    setSupportActionBar(actionbarRegister);
    getSupportActionBar().setTitle("Hesap Oluştur");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    txtEmail = (EditText) findViewById(R.id.txtEmailRegister);
    txtPassword = (EditText) findViewById(R.id.txtPasswordRegister);
    btnRegister = (Button) findViewById(R.id.btnRegister);


}

    private void createNewAccount() {
        String email = txtEmail.getText().toString();
        String password = txtPassword.getText().toString();

       if (TextUtils.isEmpty(email)) {
            Toast.makeText(this, "Email alanı boş olamaz !", Toast.LENGTH_LONG).show();
        }
       else if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "Parola alanı boş olamaz !", Toast.LENGTH_LONG).show();
        } else {
auth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
    @Override
    public void onComplete(@NonNull Task<AuthResult> task) {

        if (task.isSuccessful()) {
            String currentUserId = auth.getCurrentUser().getUid();
            myRef.child("Kullanicilar").child(currentUserId).setValue("");
            Toast.makeText(RegisterActivity.this, "Hesabınız başarılı bir şekilde oluşturuldu !", Toast.LENGTH_LONG).show();
            Intent loginIntent = new Intent(RegisterActivity.this, LoginActivity.class);
            startActivity(loginIntent);

        }
        else {
            Toast.makeText(RegisterActivity.this, "Bir hata oluştu. Lütfen tekrar deneyiniz.", Toast.LENGTH_LONG).show();
            Intent aIntent = new Intent(RegisterActivity.this, RegisterActivity.class);
            startActivity(aIntent);
        }
    }
});
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        init();
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createNewAccount();
            }
        });
    }
}
