package com.dowerx.quack.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.dowerx.quack.R;
import com.dowerx.quack.service.AuthService;

public class RegisterActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_register);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

    }

    public void register(View view) {
        String email = String.valueOf(((EditText)findViewById(R.id.email)).getText());
        String username = String.valueOf(((EditText)findViewById(R.id.username)).getText());
        String password = String.valueOf(((EditText)findViewById(R.id.password)).getText());
        String rePassword = String.valueOf(((EditText)findViewById(R.id.repeat_password)).getText());

        if (email.isEmpty() || password.isEmpty() || rePassword.isEmpty() || username.isEmpty())
            return;

        if (!password.equals(rePassword)) {
            Toast toast = new Toast(this);
            toast.setText("passwords don't match");
            toast.setDuration(Toast.LENGTH_LONG);
            toast.show();
            return;
        }

        AuthService.Register task = new AuthService.Register(this);
        task.execute(email, password, username);
    }

    public void redirect_login(View view) {
        startActivity(new Intent(this, MainActivity.class));
    }
}