aboutsummaryrefslogtreecommitdiff
path: root/go-src/windows.go
blob: 93e27f938fbec7a5f504e3d7f59c786e88fc91f5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package ctfc

import (
	"strconv"
	"strings"

	"gitlab.com/Syndamia/ctfc/go-src/csi"
	"gitlab.com/Syndamia/ctfc/go-src/ui"
	"gitlab.com/Syndamia/ctfc/go-src/utils"
)

const showHelp = "ShowHelp"

type window func(...string)

func StartupWindow(...string) {
	csi.ClearScreen()

	ui.NormalBox(true, loginNavTitle, registerNavTitle)
	input := ui.InputField(startupWindowSpec)

	nextWindow := handleInputActions(input, false,
		inputAction{"L", loginWindow, nil},
		inputAction{"R", registerWindow, nil},
	)

	if nextWindow == nil {
		defer showError(invalidCommand, StartupWindow)
	} else {
		defer nextWindow()
	}
}

func loginWindow(values ...string) {
	// Takes form user input
	inputs := formWindow(loginNavTitle, StartupWindow,
		[]formInput{
			{usernameInName, inputBackSpec, nil},
			{passwordInName, "", nil},
		},
		values...,
	)
	if len(inputs) == 0 {
		return
	}

	if !usernameExists(inputs[0]) {
		defer showError(valueDoesNotExist(usernameInName), loginWindow)
	}

	// If login is successful, go to chats window, else show error
	if logInUser(inputs[0], inputs[1]) {
		defer chatsWindow()
	} else {
		defer showError(invalidCredentials, loginWindow, inputs[0])
	}
}

func registerWindow(values ...string) {
	inputs := formWindow(registerNavTitle, StartupWindow,
		[]formInput{
			{usernameInName, usernameSpec, stringValidUsername},
			{passwordInName, passwordSpec, stringValidPassword},
			{nameInName, nameSpec, stringValidName},
		},
		values...,
	)
	if len(inputs) == 0 {
		return
	}
	if usernameExists(inputs[0]) {
		defer showError(valueAlreadyTaken(usernameInName), registerWindow, values...)
		return
	}

	createUser(inputs)
	logInUser(inputs[0], inputs[1])
	defer chatsWindow()
}

func chatsWindow(values ...string) {
	csi.ClearScreen()

	initPaginatedValues(0, &values)

	ui.NormalBox(true, directMessagesNavTitle, accountNavTitle, logoutNavTitle)

	allChats := getAllChats()
	ui.EmptyLines(pageSize + 3) // empty line + message lines + page number line + empty line

	customLinesBelow := utils.If(values[1] == showHelp).Int(3, 2)

	lastLine := -1
	go routinePaginatedSubwindow(&allChats,
		func(s *[]string) { *s = getAllChats() },
		&lastLine,
		len(values[0]),
		customLinesBelow,
		true,
	)

	if values[1] == showHelp {
		ui.TextField(chatsWindowHelpMsg)
	}

	input := ui.InputField(chatsSpec)

	nextWindow := handleInputActions(input, true,
		inputAction{"C", createChatWindow, nil},
		inputAction{"H", chatsWindow, []string{values[0], showHelp}},
	)

	if nextWindow == nil {
		// If user input is number, navigate to chat of given number, else show error
		if chatI, err := strconv.Atoi(input); chatI >= 0 && chatI <= len(allChats) && err == nil {
			defer chatWindow(strings.Split(allChats[utils.MaxInt(len(allChats)-pageSize*len(values[0]), 0)+chatI-1], " : ")[0])
		} else if input == ">" {
			// If possible, increment to the next page (adds a dot to the end of the string)
			if len(values[0]) < totalPages(len(allChats)) {
				values[0] += "."
			}
			defer chatsWindow(values...)
		} else if input == "<" {
			// If possible, decrement to the previous page (removes a dot from the string)
			if len(values[0]) > 1 {
				utils.StrShortenRight(&values[0], 1)
			}
			defer chatsWindow(values...)
		} else if chatNameExists(input) {
			defer chatWindow(input)
		} else {
			defer showError(invalidCommand, chatsWindow)
		}
	} else {
		defer nextWindow()
	}
	lastLine = -2
}

func createChatWindow(values ...string) {
	inputs := formWindow(createChatNavTitle, chatsWindow,
		[]formInput{
			{chatNameInName, chatNameSpec, stringValidChatName},
			{chatDescInName, chatDescSpec, stringValidChatDesc},
		},
		values...,
	)
	if len(inputs) == 0 {
		return
	}
	if chatNameExists(inputs[0]) {
		defer showError(valueAlreadyTaken(chatNameInName), createChatWindow, values...)
		return
	}

	createChat(inputs[0], inputs[1], loggedInUser.Username)
	defer chatWindow(inputs[0])
}

func chatWindow(values ...string) {
	csi.ClearScreen()

	// We determine page number by length of a string
	// This method should be faster than having to cast to int all the time
	initPaginatedValues(1, &values)

	currChat := getChat(values[0])

	ui.NormalBox(true, chatNavTitle, directMessagesNavTitle, accountNavTitle, logoutNavTitle)
	ui.TextFields(
		currChat.Name+" : "+currChat.Description,
		"Brought to you by "+currChat.Owner.Name+" ("+currChat.Owner.Username+")",
	)

	ui.EmptyLines(pageSize + 3) // empty line + messages lines + page number line + empty line

	customLinesBelow := utils.If(values[2] == showHelp).Int(3, 2)

	lastLine := -1
	go routinePaginatedSubwindow(&currChat.Messages,
		func(s *[]string) { *s = getChat(values[0]).Messages },
		&lastLine,
		len(values[1]),
		customLinesBelow,
		false,
	)

	if values[2] == showHelp {
		ui.TextField(chatWindowHelpMsg)
		values[2] = ""
	}

	input := ui.InputField(chatWindowSpec)

	nextWindow := handleInputActions(input, true,
		inputAction{"H", chatWindow, []string{values[0], values[1], showHelp}},
	)

	if nextWindow == nil {
		switch input {
		case ">":
			// If possible, increment to the next page (adds a dot to the end of the string)
			if len(values[1]) < totalPages(len(currChat.Messages)) {
				values[1] += "."
			}
		case "<":
			// If possible, decrement to the previous page (removes a dot from the string)
			if len(values[1]) > 1 {
				utils.StrShortenRight(&values[1], 1)
			}
		default: // Send a message
			values[1] = "." // Make sure to be at the first page, when sending a message
			// Since "C" is a command, to just enter the letter C you'll need to input "\C"
			currChat.addMessage(strings.TrimPrefix(input, "\\"), loggedInUser.Username)
		}

		defer chatWindow(values...)
	} else {
		defer nextWindow()
	}
	lastLine = -2 // Practically stops execution of the paginated subwindow routine
}

func accountWindow(...string) {
	csi.ClearScreen()

	ui.NormalBox(true, chatNavTitle, directMessagesNavTitle, logoutNavTitle)
	ui.TextField(usernameInName + " : " + loggedInUser.Username)
	ui.NumberedFields(
		passwordInName+" : "+hiddenValue,
		nameInName+" : "+loggedInUser.Name,
	)

	ui.EmptyLine()
	input := ui.InputField(accountWindowSpec)

	nextWindow := handleInputActions(input, true)

	if nextWindow == nil {
		if editI, err := strconv.Atoi(input); editI > 0 && editI < 2 && err == nil {
			switch editI {
			case 1:
				pass := formWindow("Editing", accountWindow,
					[]formInput{
						{"Current password", inputBackSpec, nil},
						{"New password", "", stringValidPassword},
					},
				)
				if !updatePassword(pass[0], pass[1]) {
					defer showError(invalidArgument, accountWindow)
				}
			}
		} else {
			defer showError(invalidCommand, accountWindow)
		}
	} else {
		defer nextWindow()
	}
}

func logoutWindow(...string) {
	csi.ClearScreen()

	ui.NormalBox(true, "Are you sure you want to Log out of "+loggedInUser.Username+"?")
	ui.EmptyLine()

	input := ui.InputField(logoutWindowSpec)

	if strings.ToLower(input) == "y" {
		logoutUser()
		defer StartupWindow()
	} else {
		defer chatsWindow()
	}
}