using MySql.Data.MySqlClient;
using SSD_Main.log;
using SSD_Main.MsgBx;
using SSD_Main.Properties;
using System;
using System.Windows.Forms;

namespace SSD_Main
{
    public partial class login : Form
    {
        string connStr = "";
        public login()
        {
            InitializeComponent();
            connStr = "server=technofox-software.de;user=root;database=" + Settings.Default.db_name + ";port=3306;password=lasmichrein";

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                Logging log = new Logging();
                log.write("Starte Login", false, "login.cs");
                MySqlConnection conn = new MySqlConnection(connStr);
                conn.Open();
                string sql = "SELECT * FROM pc_user WHERE un='" + comboBox1.Text + "' AND pass= '" + textBox2.Text + "';";
                log.write("DEBUG: SELECT * FROM pc_user WHERE un='" + comboBox1.Text + "' AND pass= '" + textBox2.Text + "';", false, "login.cs");
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                MySqlDataReader rdr = cmd.ExecuteReader();
                if (rdr.HasRows)
                {
                    if (rdr.Read())
                    {
                        MySqlConnection conn2 = new MySqlConnection(connStr);
                        conn2.Open();
                        string sql2 = "SELECT * FROM pc_user WHERE un='" + comboBox1.Text + "' AND pass= '" + textBox2.Text + "';";
                        MySqlCommand cmd2 = new MySqlCommand(sql2, conn2);
                        MySqlDataReader rdr2 = cmd2.ExecuteReader();
                        if (rdr2.HasRows)
                        {
                            if (rdr2.Read()) {
                                if (checkBox1.Checked)
                                {
                                    Settings.Default.username = comboBox1.Text;
                                    Settings.Default.passwort = textBox2.Text;
                                    Settings.Default.stayLogged = true;
                                    Settings.Default.db_normal = "server=technofox-software.de;user=root;database=" + Settings.Default.db_name + ";port=3306;password=lasmichrein";
                                    Settings.Default.db_user = "server=technofox-software.de;user=root;database=Users;port=3306;password=lasmichrein";
                                    if (rdr2[11].ToString() == "1")
                                    {
                                        Settings.Default.ma_admin = true;
                                    }
                                    else
                                    {
                                        Settings.Default.ma_admin = false;
                                    }
                                    Settings.Default.Save();
                                    log.write("Speichere Login", false, "login.cs");
                                }

                                this.Hide();
                                Main main = new Main(Int32.Parse(rdr[0].ToString()));
                                main.Show();
                                log.write("Login erfolgreich", false, "login.cs");
                                MessageBox.Show("Willkommen " + rdr[1].ToString(), "SSD Login", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                conn.Close();
                                conn2.Close();
                            }
                        }
                        else
                        {
                            log.write("Benutzer inaktiv.", true, "login.cs");
                            MessageBox.Show("Benutzer in aktiv. Bitte an Administrator wenden", "SSD Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            conn.Close();
                            conn2.Close();
                        }
                    }
                    else
                    {
                        log.write("Benutzername/Passwort falsch. Ungültige eingabe", true, "login.cs");
                        MessageBox.Show("Benutzername und/oder Passwort falsch\n\nBitte erneut versuchen", "SSD Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        conn.Close();
                    }
                }
                else
                {
                    log.write("Benutzername/Passwort falsch. Keine Rows gefunden", true, "login.cs");
                    MessageBox.Show("Benutzername und/oder Passwort falsch\n\nBitte erneut versuchen", "SSD Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    conn.Close();
                }
                rdr.Close();
            }
            catch (Exception ex)
            {
                Logging log = new Logging();
                log.write("Login Fehler: " + ex.Message, true, "login.cs");
                MessageBox.Show("Fehler im Login.\n\nFehler:\n" + ex.Message, "SSD Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
        }

        private void login_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        private void auto_login()
        {
            if (Settings.Default.stayLogged)
            {
                try
                {
                    Logging log = new Logging();
                    log.write("Starte Auto-Login", false, "login.cs");
                    MySqlConnection conn = new MySqlConnection(connStr);
                    conn.Open();
                    string sql = "SELECT * FROM pc_user WHERE un='" + Settings.Default.username + "' AND pass = '" + Settings.Default.passwort + "';";
                    MySqlCommand cmd = new MySqlCommand(sql, conn);
                    MySqlDataReader rdr = cmd.ExecuteReader();
                    if (rdr.HasRows)
                    {
                        if (rdr.Read())
                        {
                            Settings.Default.db_normal = "server=technofox-software.de;user=root;database=" + Settings.Default.db_name + ";port=3306;password=lasmichrein";
                            Settings.Default.db_user = "server=technofox-software.de;user=root;database=Users;port=3306;password=lasmichrein";
                            if (rdr[11].ToString() == "1")
                            {
                                Settings.Default.ma_admin = true;
                            }
                            else
                            {
                                Settings.Default.ma_admin = false;
                            }
                            Settings.Default.Save();
                            this.Hide();
                            Main main = new Main(Int32.Parse(rdr[0].ToString()));
                            main.Show();
                            log.write("Auto-Login erfolgreich", false, "login.cs");
                            conn.Close();
                        }
                        else
                        {
                            Settings.Default.stayLogged = false;
                            Settings.Default.username = "";
                            Settings.Default.passwort = "";
                            Settings.Default.Save();
                            log.write("Auto-Login fehlgeschlagen", false, "login.cs");
                            MessageBox.Show("Benutzername und/oder Passwort falsch\n\nBitte erneut versuchen", "SSD Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            conn.Close();
                        }
                    }
                    else
                    {
                        Settings.Default.stayLogged = false;
                        Settings.Default.username = "";
                        Settings.Default.passwort = "";
                        Settings.Default.Save();
                        log.write("Benutzername/Passwort falsch. Keine Rows gefunden", true, "login.cs");
                        MessageBox.Show("Benutzername und/oder Passwort falsch\n\nBitte erneut versuchen", "SSD Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        conn.Close();
                    }
                    rdr.Close();
                }
                catch (Exception ex)
                {
                    Logging log = new Logging();
                    log.write("Auto-Login Fehler: " + ex.Message, true, "login.cs");
                    MessageBox.Show("Fehler im Auto-Login.\n\nFehler:\n" + ex.Message, "SSD Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void login_Shown(object sender, EventArgs e)
        {
            auto_login();
        }

        private void login_Load(object sender, EventArgs e)
        {
            try
            {
                MySqlConnection conn = new MySqlConnection(connStr);
                conn.Open();
                string sql = "SELECT * FROM pc_user WHERE active='1';";
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                MySqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    comboBox1.Items.Add(rdr[2].ToString());
                }
                conn.Close();
            }
            catch(Exception ex)
            {
                Logging log = new Logging();
                log.write("Fehler beim holen der Benutzernamen Fehler: " + ex.Message, true, "login.cs");
                MessageBox.Show("Fehler beim Holen der Benutzernamen.\n\nFehler:\n" + ex.Message, "SSD Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
