package com.example.tampopo_client.views;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
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 androidx.lifecycle.ViewModelProvider;
import com.example.tampopo_client.R;
import com.example.tampopo_client.Tampopo;
import com.example.tampopo_client.viewmodels.UserViewModel;

import androidx.lifecycle.Observer;

public class LoginActivity extends AppCompatActivity {

  UserViewModel userViewModel;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    EdgeToEdge.enable(this);
    setContentView(R.layout.activity_login);
    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;
    });
    userViewModel = new ViewModelProvider(this).get(UserViewModel.class);

    userViewModel.getError().observe(this,
        msg -> {
          if (msg != null) {
            Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
          }
        });

    userViewModel.getToken().observe(this, new Observer<String>() {
      @Override
      public void onChanged(String token) {
        if (token != null){
          Tampopo app = (Tampopo) LoginActivity.this.getApplication();
          app.setToken(token);

          EditText passwordInput = findViewById(R.id.PasswordText);
          String enteredPassword = passwordInput.getText().toString();
          app.setPassword(enteredPassword);


          Intent intent = new Intent(LoginActivity.this, MainActivity.class);
          startActivity(intent);
        }
      }
    });

    Button button_login = (Button) findViewById(R.id.dicisionbutton);
    button_login.setOnClickListener(new OnClickListener() {

      private Object getApplication;

      public void onClick(View v) {
        EditText useridInput = (EditText) findViewById(R.id.idbutton);
        String userid = useridInput.getText().toString();
        ((Tampopo) LoginActivity.this.getApplication()).setUserId(userid);
        EditText passwordInput = (EditText) findViewById(R.id.PasswordText);
        String password = passwordInput.getText().toString();
        userViewModel.login(userid, password);




      }
    });

    Button button_createaccount = (Button) findViewById(R.id.createaccountbutton);
    button_createaccount.setOnClickListener(new OnClickListener() {

      public void onClick(View v) {
        Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
        startActivity(intent);
      }
    });

  }
}