TIMESET_UP_ARROW   EQUS "\"♂\"" ; $ef
TIMESET_DOWN_ARROW EQUS "\"♀\"" ; $f5

InitTimeAndDay:
	; load GFX for arrows and such
	ld a, [hInMenu]
	push af
	ld a, $1
	ld [hInMenu], a
	ld de, TimeSetUpArrowGFX
	ld hl, vTiles0 tile TIMESET_UP_ARROW
	lb bc, BANK(TimeSetUpArrowGFX), 1
	call Request1bpp
	ld de, TimeSetDownArrowGFX
	ld hl, vTiles0 tile TIMESET_DOWN_ARROW
	lb bc, BANK(TimeSetDownArrowGFX), 1
	call Request1bpp

	; default time: 13:37
	ld a, 13
	ld [wInitHourBuffer], a
	ld a, 37
	ld [wInitMinuteBuffer], a

.loop
	call ClearScreen
	call InitCalendar
	call ClearScreen
	call InitClock
	call ClearScreen
	call .ConfirmTimeDateText
	call YesNoBox
	jr c, .loop

.done
	call ClearScreen

	ld a, [wInitHourBuffer]
	ld [wStringBuffer2 + 1], a
	ld a, [wInitMinuteBuffer]
	ld [wStringBuffer2 + 2], a
	xor a
	ld [wStringBuffer2 + 3], a
	call InitTimeOfDay

	ld a, [wTempDayOfWeek]
	ld [wStringBuffer2], a
	call InitDayOfWeek
	pop af
	ld [hInMenu], a
	ret

.ConfirmTimeDateText:
	hlcoord 0, 12
	lb bc, 4, 18
	call TextBox
	call LoadStandardMenuHeader
	hlcoord 1, 14
	call PlaceWeekdayString
	hlcoord 1, 16
	call DisplayClock
	ret

InitClock:
	hlcoord 0, 12
	lb bc, 4, 18
	call TextBox
	call LoadStandardMenuHeader
	ld hl, Text_WhatTimeIsIt
	call PrintText

.SwitchToHours:
	hlcoord 11, 7
	lb bc, 2, 7
	call TextBox
	hlcoord 13, 8
	ld [hl], TIMESET_UP_ARROW
	hlcoord 13, 10
	ld [hl], TIMESET_DOWN_ARROW
	hlcoord 12, 9
	call DisplayClock
	ld c, 10
	call DelayFrames

.SetHourLoop:
	call JoyTextDelay
	ld a, [hJoyPressed]
	ld hl, hJoyLast
	and A_BUTTON
        jr nz, .ValidateTime
	ld a, [hl]
	and D_LEFT
	jr nz, .SwitchToMinutes
	ld a, [hl]
	and D_RIGHT
	jr nz, .SwitchToMinutes
	call SetHour
	jr .SetHourLoop

.SwitchToMinutes:
	hlcoord 11, 7
	lb bc, 2, 7
	call TextBox
	hlcoord 16, 8
	ld [hl], TIMESET_UP_ARROW
	hlcoord 16, 10
	ld [hl], TIMESET_DOWN_ARROW
	hlcoord 12, 9
	call DisplayClock
	ld c, 10
	call DelayFrames

.SetMinutesLoop:
	call JoyTextDelay
	ld a, [hJoyPressed]
	ld hl, hJoyLast
	and A_BUTTON
        jr nz, .ValidateTime
	ld a, [hl]
	and D_LEFT
	jr nz, .SwitchToHours
	ld a, [hl]
	and D_RIGHT
	jr nz, .SwitchToHours
	call SetMinutes
	jr .SetMinutesLoop

.ValidateTime:
	ret

SetHour:
	ld a, [hJoyPressed]
	and D_UP
	jr nz, .up
	ld hl, hJoyLast
	ld a, [hl]
	and D_DOWN
	jr nz, .down
	call DelayFrame
	and a
	ret

.down
	ld hl, wInitHourBuffer
	ld a, [hl]
	and a
	jr nz, .decrease
	ld a, 23 + 1

.decrease:
	dec a
	ld [hl], a
	jr .finish_dpad

.up
	ld hl, wInitHourBuffer
	ld a, [hl]
	cp 23
	jr c, .increase
	ld a, -1

.increase:
	inc a
	ld [hl], a

.finish_dpad
	hlcoord 12, 9
	ld a, " "
	ld bc, 2
	call ByteFill
	hlcoord 12, 9
	call DisplayClock
	call WaitBGMap
	and a
	ret

DisplayClock:
	push hl
	lb bc, 1, 2
	ld de, wInitHourBuffer
        call PrintNum
        ld [hl], ":"
        inc hl
        ld de, wInitMinuteBuffer
        lb bc, PRINTNUM_LEADINGZEROS | 1, 2
        call PrintNum
	pop hl
	ret

SetMinutes:
	ld a, [hJoyPressed]
	and D_UP
	jr nz, .up
	ld hl, hJoyLast
	ld a, [hl]
	and D_DOWN
	jr nz, .down
	call DelayFrame
	and a
	ret

.down
	ld hl, wInitMinuteBuffer
	ld a, [hl]
	and a
	jr nz, .decrease
	ld a, 59 + 1

.decrease
	dec a
	ld [hl], a
	jr .finish_dpad

.up
	ld hl, wInitMinuteBuffer
	ld a, [hl]
	cp 59
	jr c, .increase
	ld a, -1

.increase
	inc a
	ld [hl], a

.finish_dpad
	hlcoord 12, 9
	ld a, " "
	ld bc, 7
	call ByteFill
	hlcoord 12, 9
	call DisplayClock
	call WaitBGMap
	and a
	ret

PrintTwoDigitNumberRightAlign:
	push hl
	ld a, " "
	ld [hli], a
	ld [hl], a
	pop hl
	lb bc, PRINTNUM_RIGHTALIGN | 1, 2
	call PrintNum
	ret

Text_WhatTimeIsIt:
	; What time is it?
	text_jump UnknownText_0x1bc2eb
	db "@"

TimeSetBackgroundGFX:
INCBIN "gfx/new_game/timeset_bg.1bpp"
TimeSetUpArrowGFX:
INCBIN "gfx/new_game/up_arrow.1bpp"
TimeSetDownArrowGFX:
INCBIN "gfx/new_game/down_arrow.1bpp"

InitCalendar:
	; default weekday: Sunday
	xor a
	ld [wTempDayOfWeek], a
	hlcoord 0, 12
	lb bc, 4, 18
	call TextBox
	call LoadStandardMenuHeader
	ld hl, .WhatDayIsItText
	call PrintText
	hlcoord 9, 7
	ld b, 2
	ld c, 9
	call TextBox
	hlcoord 14, 7
	ld [hl], TIMESET_UP_ARROW
	hlcoord 14, 10
	ld [hl], TIMESET_DOWN_ARROW
	hlcoord 10, 9
	call PlaceWeekdayString
	call ApplyTilemap
	ld c, 10
	call DelayFrames
.loop
	call JoyTextDelay
	call .GetJoypadAction
	jr nc, .loop
	ret

.GetJoypadAction:
	ld a, [hJoyPressed]
	and A_BUTTON
	jr z, .not_A
	scf
	ret

.not_A
	ld hl, hJoyLast
	ld a, [hl]
	and D_UP
	jr nz, .up
	ld a, [hl]
	and D_DOWN
	jr nz, .down
	call DelayFrame
	and a
	ret

.down
	ld hl, wTempDayOfWeek
	ld a, [hl]
	and a
	jr nz, .decrease
	ld a, SATURDAY + 1

.decrease
	dec a
	ld [hl], a
	jr .finish_dpad

.up
	ld hl, wTempDayOfWeek
	ld a, [hl]
	cp 6
	jr c, .increase
	ld a, SUNDAY - 1

.increase
	inc a
	ld [hl], a

.finish_dpad
	xor a
	ld [hBGMapMode], a
	hlcoord 10, 8
	ld b, 2
	ld c, 9
	call ClearBox
	hlcoord 10, 9
	call PlaceWeekdayString
	call WaitBGMap
	and a
	ret

.WhatDayIsItText:
	; What day is it?
	text_jump UnknownText_0x1bc369
	db "@"

PlaceWeekdayString:
	push hl
	ld a, [wTempDayOfWeek]
	ld e, a
	ld d, 0
	ld hl, .WeekdayStrings
	add hl, de
	add hl, de
	ld a, [hli]
	ld d, [hl]
	ld e, a
	pop hl
	call PlaceString
	ret

.WeekdayStrings:
; entries correspond to wCurDay constants (see constants/wram_constants.asm)
	dw .Sunday
	dw .Monday
	dw .Tuesday
	dw .Wednesday
	dw .Thursday
	dw .Friday
	dw .Saturday
	dw .Sunday

.Sunday:    db "Sunday@"
.Monday:    db "Monday@"
.Tuesday:   db "Tuesday@"
.Wednesday: db "Wednesday@"
.Thursday:  db "Thursday@"
.Friday:    db "Friday@"
.Saturday:  db "Saturday@"


InitialSetDSTFlag:
	ld a, [wDST]
	set 7, a
	ld [wDST], a
	hlcoord 1, 14
	lb bc, 3, 18
	call ClearBox
	ld hl, .Text
	call PlaceHLTextAtBC
	ret

.Text:
	start_asm
	call UpdateTime
	ld a, [hHours]
	ld b, a
	ld a, [hMinutes]
	ld c, a
	decoord 1, 14
	farcall PrintHoursMins
	ld hl, .DSTIsThatOK
	ret

.DSTIsThatOK:
	; DST, is that OK?
	text_jump Text_DSTIsThatOK
	db "@"

InitialClearDSTFlag:
	ld a, [wDST]
	res 7, a
	ld [wDST], a
	hlcoord 1, 14
	lb bc, 3, 18
	call ClearBox
	ld hl, .Text
	call PlaceHLTextAtBC
	ret

.Text:
	start_asm
	call UpdateTime
	ld a, [hHours]
	ld b, a
	ld a, [hMinutes]
	ld c, a
	decoord 1, 14
	farcall PrintHoursMins
	ld hl, .IsThatOK
	ret

.IsThatOK:
	; , is that OK?
	text_jump UnknownText_0x1c5ff1
	db "@"

ClearScreen:
	xor a
	ld [hBGMapMode], a
	hlcoord 0, 0
	ld bc, SCREEN_HEIGHT * SCREEN_WIDTH
	xor a
	call ByteFill
	ld a, $1
	ld [hBGMapMode], a
	ret


DebugDisplayTime:
	hlcoord 1, 14
	lb bc, 3, SCREEN_WIDTH - 2
	call ClearBox
	ld hl, .Text
	call PlaceHLTextAtBC
	ret

.Text:
	start_asm
	call UpdateTime

	hlcoord 1, 14
	ld [hl], "R"
	inc hl
	ld [hl], "T"
	inc hl
	ld [hl], " "
	inc hl

	ld de, hRTCDayLo
	call .PrintTime

	hlcoord 1, 16
	ld [hl], "D"
	inc hl
	ld [hl], "F"
	inc hl
	ld [hl], " "
	inc hl

	ld de, wStartDay
	call .PrintTime

	ld [hl], " "
	inc hl

	ld a, [wDST]
	bit 7, a
	jr z, .off

	ld [hl], "O"
	inc hl
	ld [hl], "N"
	inc hl
	jr .done

.off
	ld [hl], "O"
	inc hl
	ld [hl], "F"
	inc hl
	ld [hl], "F"
	inc hl

.done
	ld hl, .NowOnDebug
	ret

.NowOnDebug:
	text "<PARA>Now on DEBUG…"
	prompt

.PrintTime:
	lb bc, 1, 3
	call PrintNum
	ld [hl], "."
	inc hl
	inc de
	lb bc, PRINTNUM_LEADINGZEROS | 1, 2
	call PrintNum
	ld [hl], ":"
	inc hl
	inc de
	lb bc, PRINTNUM_LEADINGZEROS | 1, 2
	call PrintNum
	ret

PrintHour:
	ld l, e
	ld h, d
	push bc
	call GetTimeOfDayString
	call PlaceString
	ld l, c
	ld h, b
	inc hl
	pop bc
	call AdjustHourForAMorPM
	ld [wDeciramBuffer], a
	ld de, wDeciramBuffer
	call PrintTwoDigitNumberRightAlign
	ret

GetTimeOfDayString:
	ld a, c
	cp MORN_HOUR
	jr c, .nite
	cp DAY_HOUR
	jr c, .morn
	cp NITE_HOUR
	jr c, .day
.nite
	ld de, .nite_string
	ret
.morn
	ld de, .morn_string
	ret
.day
	ld de, .day_string
	ret

.nite_string: db "NITE@"
.morn_string: db "MORN@"
.day_string:  db "DAY@"

AdjustHourForAMorPM:
; Convert the hour stored in c (0-23) to a 1-12 value
	ld a, c
	or a
	jr z, .midnight
	cp NOON_HOUR
	ret c
	ret z
	sub NOON_HOUR
	ret

.midnight
	ld a, NOON_HOUR
	ret
