package com.example.ugotthis;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    // Variables
    private final AppCompatActivity activity = MainActivity.this;
    private DatabaseHelper databaseHelper;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        CardView cvLogin = (CardView)findViewById(R.id.cardView);
        TextView txRegister = (TextView) findViewById(R.id.textView_Register);

        // User input fields.
        final EditText userName = (EditText) findViewById(R.id.editText_userName);
        final EditText etpassword = (EditText) findViewById(R.id.ediText_Password);

        databaseHelper = new DatabaseHelper(activity);

        cvLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String user = userName.getText().toString();
                String password = etpassword.getText().toString();

                SharedPreferences preferences = getSharedPreferences("MYPREFS", MODE_PRIVATE);

                if (userName.length() == 0) {
                    userName.setError("Please enter Username !");
                }
                else
                {
                    if (etpassword.length()==0) {
                        etpassword.setError("Please enter Password !");
                    }
                    else {

                        Log.d("User", user);
                        Log.d("Password", password);

                        int iResult = databaseHelper.checkUser(user, password);

                        // Get user id, error if -1
                        if ( iResult > -1) {

                            //Toast.makeText(MainActivity.this, String.valueOf(databaseHelper.checkUser(user, password)), Toast.LENGTH_LONG).show();

                            //  Save user id to SP
                            SharedPreferences.Editor editor = preferences.edit();
                            editor.putInt("user_id", iResult); // Storing integer
                            editor.commit(); // commit changes

                            // Redirect to activity list
                            Intent tasksScreen = new Intent(MainActivity.this, TasksActivity.class);
                            startActivity(tasksScreen);
                        }
                        else {
                            Toast.makeText(MainActivity.this, "Wrong UserName or Password!", Toast.LENGTH_LONG).show();
                        }
                    }
                }
            }
        });

        txRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent registerScreen = new Intent(MainActivity.this, Register.class);
                startActivity(registerScreen);
            }
        });
    }

    @Override
    public void onClick(View v) {

    }
}
