Imports System.IO

Public Class Form1
    Dim basedir As String = "F:\OneDrive\Documents\Other\Text files"
    Dim npuLocation As String = Application.StartupPath()
    Dim currentFile As String = ""
    Dim newFile, showSidebar As Boolean
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TextFileHolder.Font = My.Settings.Font
        TextFileHolder.WordWrap = My.Settings.WordWrap
        WordWrapToolStripMenuItem.Checked = My.Settings.WordWrap
        showSidebar = Not My.Settings.ShowSidebar
        SidebarToolStripMenuItem_Click(sender, e)
        NightModeToolStripMenuItem.Checked = Not My.Settings.NightMode
        NightModeToolStripMenuItem_Click(sender, e) ';D
        basedir = My.Settings.WorkingDirectory

        SaveFileDialog1.InitialDirectory = basedir
        FontDialog1.Font = TextFileHolder.Font
        redraw()
        updateFilePicker()
        updateStatusBar()
    End Sub

    Private Sub updateFilePicker()
        FilePicker.Items.Clear()
        If Not basedir.EndsWith("\") Then basedir += "\"
        Dim fileNames = My.Computer.FileSystem.GetFiles(basedir, FileIO.SearchOption.SearchTopLevelOnly, "*.txt")
        Dim files As Boolean = False
        For Each fileName As String In fileNames
            FilePicker.Items.Add(fileName.Remove(0, Len(basedir)))
            files = True
        Next
        If Not files Then MsgBox("No txt files were found in this directory.")
    End Sub

    Private Sub redraw() Handles Me.Resize, Me.ResizeEnd
        Select Case showSidebar
            Case True
                FilePicker.Height = Me.Height - 60 - StatusBar.Height
                TextFileHolder.Height = Me.Height - 37 - StatusBar.Height
                TextFileHolder.Width = Me.Width - 195
            Case False
                TextFileHolder.Height = Me.Height - 37 - StatusBar.Height
                TextFileHolder.Width = Me.Width - 15
        End Select
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles FilePicker.SelectedIndexChanged
        TextFileHolder.Text = "Loading, please wait..."
        Refresh()
        Dim chosenFile As StreamReader
        currentFile = FilePicker.SelectedItem
        Dim fileToLoad As String = basedir & currentFile
        chosenFile = New StreamReader(fileToLoad, encoding:=System.Text.Encoding.UTF8, detectEncodingFromByteOrderMarks:=True)
        TextFileHolder.Text = chosenFile.ReadToEnd
        Text = "Notepad Ultra: " & currentFile
        newFile = "False"
        chosenFile.Close()
        updateStatusBar()
    End Sub

    Private Sub WordWrapToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles WordWrapToolStripMenuItem.Click
        Select Case WordWrapToolStripMenuItem.Checked
            Case False
                WordWrapToolStripMenuItem.Checked = True
                TextFileHolder.WordWrap = True
            Case True
                WordWrapToolStripMenuItem.Checked = False
                TextFileHolder.WordWrap = False
        End Select
    End Sub

    Private Sub DeleteToolStripMenuItem2_Click(sender As Object, e As EventArgs) Handles DeleteToolStripMenuItem2.Click
        If currentFile = "" Then
            MsgBox("You can't delete the current file when there isn't a current file, you dunce!")
        Else
            If MsgBox("Are you sure you want to delete " & currentFile & "?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                My.Computer.FileSystem.DeleteFile(basedir & currentFile)
                updateFilePicker()
            End If
        End If
    End Sub

    Private Sub NewToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewToolStripMenuItem.Click
        currentFile = "unnamed"
        newFile = True
    End Sub

    Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveToolStripMenuItem.Click
        If newFile Then SaveAsToolStripMenuItem_Click(sender, e) Else
        My.Computer.FileSystem.WriteAllText(basedir & currentFile, TextFileHolder.Text, False, System.Text.Encoding.UTF8)
        Dim startInfo As New ProcessStartInfo("""" & npuLocation & "\unix2dos.exe""", """" & basedir & currentFile & """")
        startInfo.WindowStyle = ProcessWindowStyle.Hidden
        Process.Start(startInfo)
    End Sub

    Private Sub SaveAsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveAsToolStripMenuItem.Click
        SaveFileDialog1.ShowDialog()
    End Sub

    Private Sub OneDriveTextFilesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OneDriveTextFilesToolStripMenuItem.Click
        basedir = "F:\OneDrive\Documents\Other\Text files"
        updateFilePicker()
    End Sub

    Private Sub FontToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles FontToolStripMenuItem.Click
        FontDialog1.ShowDialog()
        TextFileHolder.Font = FontDialog1.Font
    End Sub

    Private Sub NightModeToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NightModeToolStripMenuItem.Click
        Select Case NightModeToolStripMenuItem.Checked
            Case False
                NightModeToolStripMenuItem.Checked = True
                TextFileHolder.BackColor = Color.Black
                TextFileHolder.ForeColor = Color.LightGray
                FilePicker.BackColor = Color.Black
                FilePicker.ForeColor = Color.LightGray
            Case True
                NightModeToolStripMenuItem.Checked = False
                TextFileHolder.BackColor = Color.White
                TextFileHolder.ForeColor = Color.Black
                FilePicker.BackColor = Color.White
                FilePicker.ForeColor = Color.Black
        End Select
    End Sub

    Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
        saveSettings()
        End
    End Sub

    Private Sub SidebarToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SidebarToolStripMenuItem.Click
        Select Case SidebarToolStripMenuItem.Checked
            Case False
                SidebarToolStripMenuItem.Checked = True
                showSidebar = True
                FilePicker.Visible = True
                TextFileHolder.Left = 185
            Case True
                SidebarToolStripMenuItem.Checked = False
                showSidebar = False
                FilePicker.Visible = False
                TextFileHolder.Left = 0
        End Select
        redraw()
    End Sub

    Private Sub updateStatusBar() Handles MyBase.KeyPress
        statBytes.Text = "Size: " & System.Text.Encoding.Unicode.GetByteCount(TextFileHolder.Text) & " Bytes"
        statChars.Text = "Characters: " & TextFileHolder.Text.Length
        If Me.Text.EndsWith(".txt") Then statDate.Text = "File Created: " & My.Computer.FileSystem.GetFileInfo(basedir & currentFile).CreationTime Else statDate.Text = "File Created: N/A"
    End Sub

    Private Sub opacify() Handles Me.Move
        Opacity = 0.5
        chocolatePuddingOvenTimer.Stop()
        chocolatePuddingOvenTimer.Start()
    End Sub

    Private Sub chocolatePuddingOvenTimer_Tick(sender As Object, e As EventArgs) Handles chocolatePuddingOvenTimer.Tick
        Opacity = 1
        chocolatePuddingOvenTimer.Stop()
    End Sub

    Private Sub ToolStripSplitButton1_ButtonClick(sender As Object, e As EventArgs) Handles ToolStripSplitButton1.ButtonClick
        Process.Start(basedir)
    End Sub

    Private Sub toggleSidebar(sender As Object, e As EventArgs) Handles ToolStripSplitButton2.ButtonClick
        SidebarToolStripMenuItem_Click(sender, e)
    End Sub

    Private Sub RestartToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RestartToolStripMenuItem.Click
        saveSettings()
        Application.Restart()
    End Sub

    Private Sub saveSettings()
        My.Settings.Font = TextFileHolder.Font
        My.Settings.WordWrap = TextFileHolder.WordWrap
        My.Settings.NightMode = NightModeToolStripMenuItem.Checked
        My.Settings.ShowSidebar = showSidebar
        My.Settings.WorkingDirectory = basedir
        My.Settings.Save()
    End Sub

    Private Sub emergencySave() Handles Me.FormClosing
        saveSettings()
    End Sub

    Private Sub SHOWRECENTSHEREToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SHOWRECENTSHEREToolStripMenuItem.Click
        currentFile = ""
        FolderBrowserDialog1.ShowDialog()
        basedir = FolderBrowserDialog1.SelectedPath
        updateFilePicker()
    End Sub

    Private Sub ChangeToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ChangeToolStripMenuItem.Click
        SHOWRECENTSHEREToolStripMenuItem_Click(sender, e)
    End Sub

    Private Sub UndoToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles UndoToolStripMenuItem.Click
        TextFileHolder.Undo()
    End Sub

    Private Sub EDocumentsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles EDocumentsToolStripMenuItem.Click
        basedir = "E:\Documents"
        updateFilePicker()
    End Sub
End Class
