UserLoginControl.cs 6.36 KB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MoyaAdminLib;
using System.Web.Script.Serialization;
using System.Net;
using ThermoPrinterLibrary;

namespace RamaSignup
{
    public partial class UserLoginControl : UserControl
    {
        public event EventHandler LoginOk;
        public delegate void ThermoPrinterEvent(ThermoPrinter.Models model);
        public event ThermoPrinterEvent SetPrinter; 

        public UserLoginControl()
        {
            InitializeComponent();
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.BackColor = Color.Transparent;
            infoLabel.Text = "Hei! Kirjoita sähköpostiosoitteesi";
        }

        private void UserLoginControl_Load(object sender, EventArgs e)
        {
            
        }

        private void txtEmail_TextChanged(object sender, EventArgs e)
        {
            string txt = txtEmail.Text;
            if (IsValidEmail(txt))
            {
                timer1.Stop();
                timer1.Start();
            }
            else
            {
                if (txtEmail.Text == "thermo2")
                {
                    if(SetPrinter != null)
                        SetPrinter(ThermoPrinter.Models.IDP3210);
                    txtEmail.Text = "OK";
                }
                else if (txtEmail.Text == "thermo1")
                {
                    if(SetPrinter != null)
                        SetPrinter(ThermoPrinterLibrary.ThermoPrinter.Models.TMT188II);
                    txtEmail.Text = "OK";
                }

                timer1.Stop();
                CurrentUser = null;
                btnNewProfile.Visible = false;
                infoLabel.Text = "Hei! Kirjoita sähköpostiosoitteesi";
            }
        }

        bool IsValidEmail(string email)
        {
            if (!email.Contains("@") || !email.Contains(".")) 
                return false;
            string[] parts = email.Split(new string[] { "@" }, StringSplitOptions.RemoveEmptyEntries);
            if (parts.Count<string>() == 2)
            {
                if (!parts[1].Contains("."))
                    return false;
                string[] domainparts = parts[1].Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
                if (domainparts.Count<string>() < 2)
                    return false;
                string ltd = domainparts[domainparts.Count<string>() - 1];
                if (ltd.Length < 2) 
                    return false;
            }
            else return false;
            
            

            try
            {
                var addr = new System.Net.Mail.MailAddress(email);
                return addr.Address == email;
            }
            catch
            {
                return false;
            }
        }
        public User CurrentUser;
        private void timer1_Tick(object sender, EventArgs e)
        {
            RestClient client = new RestClient();
            try
            {
                string json = client.MakeRequest("user", "email=" + txtEmail.Text);
                JavaScriptSerializer ser = new JavaScriptSerializer();
                Eventuser euser = ser.Deserialize<Eventuser>(json);
                if (euser != null)
                {
                    btnNewProfile.Visible = false;
                    CurrentUser = new User(euser);
                    infoLabel.Text = "Hei! " + CurrentUser.ToString() + " Kirjoita salasanasi ja kirjaudu";
                    showLogin(true);
                }
            }
            catch (Exception ex)
            {
                CurrentUser = null;
                infoLabel.Text = "Uusi kävijä?";
                btnNewProfile.Visible = true;
                showLogin(false);
            }
            timer1.Stop();
            
        }

        void showLogin(bool show)
        {
            passwordTextBox.Visible = show;
            loginButton.Visible = show;
        }
        private void login()
        {
            RestClient client;

            client = new RestClient(RestClient.ApiURL, HttpVerb.POST);
            //client.PostData = "password=" + WebUtility.UrlEncode(passwordTextBox.Text);
            client.PostData = "password=" + passwordTextBox.Text;
            client.ContentType = "application/x-www-form-urlencoded";
            try
            {
                string json = client.MakeRequest("v2/user/" + CurrentUser.UserId + "/check-password");
                //string json2 = client.MakeRequest("user/"+ CurrentUser.UserId +"/check-password");
                ///MoyaWeb/rest/user/eventusers
                JavaScriptSerializer ser = new JavaScriptSerializer();
                Eventuser euser = ser.Deserialize<Eventuser>(json);
                if (euser != null)
                {
                    CurrentUser = new User(euser);
                    CurrentUser.Email = txtEmail.Text;
                    if (LoginOk != null)
                        LoginOk(this, null);
                }
                else
                    wrongPass();

            }
            catch (Exception ex)
            {
                wrongPass();
            }
            
        }
        private void loginButton_Click(object sender, EventArgs e)
        {
            login();
        }
        private void wrongPass()
        {
            infoLabel.Text = "Väärä salasana. Syötä salasana uudelleen";
        }

        private void passwordTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
                login();
        }

        private void btnNewProfile_Click(object sender, EventArgs e)
        {
            CurrentUser = new User();
            CurrentUser.Email = txtEmail.Text;
            //Send info to parent control
            if (LoginOk != null)
                LoginOk(this, null);
        }


        internal void Clear()
        {
            CurrentUser = null;
            txtEmail.Text = "";
            passwordTextBox.Visible = false;
            passwordTextBox.Text = "";
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.BackColor = Color.Transparent;
            infoLabel.Text = "Hei! Kirjoita sähköpostiosoitteesi";
            loginButton.Visible = false;
        }
    }
}