using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace hukpiWinApp
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
            
        }

        private void buttonDoLogin_Click(object sender, EventArgs e)
        {
            if (areLoginFieldsEmpty())
            {
                MessageBox.Show("Kérem töltse ki a felhasználó és jelszó mezőket!");
                textBoxUserName.Focus();
            }
            else
            {
                doLogin(textBoxUserName.Text, textBoxPassword.Text);
            }
        }

        private bool areLoginFieldsEmpty()
        {
            if ("".Equals(textBoxPassword.Text.Trim()) || "".Equals(textBoxUserName.Text.Trim()))
            {
                return true;
            }

            return false;
        }

        private void doLogin(string user, string password)
        {
            
            hukpiDatabaseDataSetTableAdapters.UsersTableAdapter hukpiDataTableAdapter = new hukpiWinApp.hukpiDatabaseDataSetTableAdapters.UsersTableAdapter();

            hukpiDatabaseDataSet.UsersDataTable loginUser = hukpiDataTableAdapter.GetDataByUserName(textBoxUserName.Text);

            if (loginUser.Count == 0)
            {
                MessageBox.Show("Nem létező felhasználó!");
            }
            else if (loginUser[0]["userPassword"].Equals(Utils.getMD5Hash(textBoxPassword.Text)))
            {
                MainForm mf = new MainForm(loginUser[0]["usersId"].ToString(), loginUser[0]["userName"].ToString(), loginUser[0]["groupId"].ToString(), loginUser[0]["userFullName"].ToString());
                mf.Show();
                this.Close();
            }
            else
            {
                MessageBox.Show("Helytelen jelszó!");
            }
            
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
