﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MySql.Data.MySqlClient;
using HamburgerMenu;
using System.Net.NetworkInformation;
using System.Net;

namespace HamburgerMenuDemo
{
    /// <summary>
    /// Interaction logic for LoginWindow.xaml
    /// </summary>
    public partial class LoginWindow : Window
    {
        MySqlConnection dbCon;
        public LoginWindow()
        {
            InitializeComponent();
            dbCon = new MySqlConnection(@"Server = 154.0.170.167; Port = 3306; Database = coatsxpk_coatsandmore; Uid = coatsxpk_root;
                                        Pwd = Jaco123; Allow Zero Datetime = True");
            tbxUsername.Focus();
        }



        private bool CheckConnection(String URL)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
                request.Timeout = 2000;
                request.Credentials = CredentialCache.DefaultNetworkCredentials;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                if (response.StatusCode == HttpStatusCode.OK) return true;
                else return false;
            }
            catch
            {
                return false;
            }
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            if(CheckConnection("http://www.google.com"))
            {
                Authenticate auth = new Authenticate(tbxUsername.Text, Convert.ToString(passwordBox.Password), dbCon);
                if (auth.logIn())
                {
                    MainWindow main = new MainWindow(dbCon, tbxUsername.Text);
                    App.Current.MainWindow = main;
                    this.Close();
                    main.Show();
                }
                else
                {
                    MessageBox.Show("Incorrect username/password combination", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            else
            {
               

                MessageBox.Show("No Internet Access");
            }

           

        }

        private void OnKeyDownHandler(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            {
                btnLogin_Click(null, null);
            }
        }

        private void lblforgotPassword_MouseDown(object sender, MouseButtonEventArgs e)
        {

            MessageBox.Show("Contact Network Administrator");

        }
    }
}
