'
' Created by SharpDevelop.
' User: Jano
' Date: 22.7.2017
' Time: 20:04
' 
' To change this template use Tools | Options | Coding | Edit Standard Headers.
'
Imports System
Imports System.Security.Cryptography
Imports System.Text
Imports System.IO
Public Partial Class FPridajPouziv
	Public Sub New
		' The Me.InitializeComponent call is required for Windows Forms designer support.
		Me.InitializeComponent()
		
		'
		' TODO : Add constructor code after InitializeComponents
		'
	End Sub
	
	Sub FPridajPouzivLoad(sender As Object, e As EventArgs)
		
	End Sub
	
	Sub FPridajPouzivClosed(sender As Object, e As FormClosedEventArgs)
		MainForm.okno=0 'prepinam nula jedna, ked je otvorene prave jedno okno
	End Sub
	
	Sub Button2Click(sender As Object, e As EventArgs)
		textBox1.Text=""
		textBox2.Text="" 'vycistim formular, resetnem ho
	End Sub
	
	Sub Button1Click(sender As Object, e As EventArgs) 'prida noveho pouzivatela
		Dim o As New Module1()
		Dim md5heslo As String
		Try
			If Not System.Text.RegularExpressions.Regex.IsMatch(TextBox1.Text, "^[A-Za-z0-9]+$") Then
				o.showMessage("Nick can contain only A-Z a-z 0-9")
				Return
			End If
			
			md5heslo=o.vratMD5(textBox2.Text.Trim) 'ukazka vytvorenia md5 hasha
			If textBox2.Text.Trim = "" Then
				o.showMessage("Password can not be empty string!")
				Return
			End If
			If textBox1.Text.Trim = "" Then
				o.showMessage("Nick can not be empty string!")
				Return
			End If
			If File.Exists(My.Computer.FileSystem.SpecialDirectories.MyDocuments.ToString & "/uziv" & textBox1.Text.Trim & ".txt") Then
				o.showMessage("This user exists. Please, create other.")
				Return
			Else
				o.zapisUziv(textBox1.Text.Trim , md5heslo)
				o.showMessage("We tried to create this user. If you can not see any error, then user is created.")
				Me.Close
			End If
		 Catch ex As Exception
		 	MsgBox(ex.Message)	
		 End Try
	End Sub
	
	Sub Button3Click(sender As Object, e As EventArgs) 'zmaze pouzivatela
		Dim hesloUziv As String
		Try
		Dim o As New Module1()
		If File.Exists(My.Computer.FileSystem.SpecialDirectories.MyDocuments.ToString & "/uziv" & textBox1.Text.Trim & ".txt") Then
				If o.vratMD5(textBox2.Text.Trim) = o.readUzivHeslo(textBox1.Text.Trim) Then
				   o.vymazUziv(textBox1.Text.Trim)
				   o.showMessage("We tried to delete user.")
				   Me.Close
				   Return
				End If					
		Else
				If textBox1.Text.Trim = "" Or textBox2.Text.Trim = "" Then
					o.showMessage("Name or password of the user is empty.")
					Return
				End If
				
				If Not File.Exists(My.Computer.FileSystem.SpecialDirectories.MyDocuments.ToString & "/uziv" & textBox1.Text.Trim & ".txt") Then
					o.showMessage("This user does not exist. Create him.")
					Return
				End If				
				o.showMessage("Within deleting the user something went wrong. Check name and password.")
				Return		
		End If
		Catch ex As Exception
		 	MsgBox(ex.Message)	
		End Try
	End Sub
End Class
