; screen variables shared with screencardread
;DEF vPreviousCardIndex EQU VARIABLES_START

ScreenCardBrowse:
  dw CardBrowseSetup
  dw CardBrowseUpdate
  dw CardBrowseDraw
  dw CardBrowseTeardown

CardBrowseSetup:
  ld a, 1 
  ld [vBlocked], a 
  
  ; make sure working oam is clear
  ld hl, ZEROES
  ld de, MY_OAM
  ld bc, $100
  call CopyRange
  
  ld hl, .asyncTask
  call Async_Spawn_HL
  
  ret
  
.asyncTask:
  ld hl, CardBrowse.UITilemap ; origin
  ld de, $9800 ; destination
  ld b, 18 ; height
  ld c, 20 ; width
  call CopyTilesToMap
  ld a, 0 
  ld [vBlocked], a 
  
  call RefreshCardTask
  
  ret

CardBrowseUpdate:
  call CardUpdate

  ld hl, vTime
  call IncrementTimer
  
  ld a, [vTime+1]
  cp a, $01
  jp c, .doneTimer ; reset the timer and do behavior when vTime passes $0100
  
  ld a, 0 
  ld [vTime], a 
  ld [vTime+1], a 
  
  ld hl, SquaresTiles
  ld a, [vFrameCountSquares]
  inc a
  call ArrayClampLooping
  ld [vFrameCountSquares], a 

.doneTimer
  ld hl, rMYBTNP
  bit 4, [hl]
  jp z, .doneWithB
  ld hl, ScreenMainMenu
  call ChangeScene
  ret
.doneWithB
  
  ld a, [vSelectedCardIndex]
  ld hl, rMYBTNP
  bit 0, [hl]
  jp z, :+ ; skip the following code if left is not pressed
  inc a 
:
  bit 1, [hl]
  jp z, :+ ; skip the following code if right is not pressed
  dec a 
:
  ld [vSelectedCardIndex], a 
  ld hl, Cards 
  call ArrayClampLooping
  ld [vSelectedCardIndex], a 
  
  ld a, [vSelectedCardIndex]
  ld hl, vPreviousCardIndex
  cp a, [hl]
  ret z ; if the selected card diddn't change, nothing to do
  
  ld a, [vBlocked]
  cp a, 0 
  ret nz
  
  ld hl, RefreshCardTask
  call Async_Spawn_HL
  
   ; make sure working oam is clear
  ld hl, ZEROES
  ld de, MY_OAM
  ld bc, $100
  call CopyRange
  
  ret

  
CardBrowseDraw:
  ; the card data is loaded asynchronously, initiated in CardReadUpdate
  
  ld hl, SquaresTiles
  inc hl 
  ld b, 0
  ld a, [vFrameCountSquares]
  ld c, a 
  add hl, bc 
  add hl, bc
  ld c, [hl]
  inc hl
  ld b, [hl]
  ld h, b
  ld l, c
  ld de, $8000+$100*16 + 1*16
  ld bc, (SquaresTileset8 - SquaresTileset7) / 8
  call CopyRangeUnsafeBy8s
  
  call CardDraw
  
  ld de, SAFE_DMA_LOCATION
  ld a, HIGH(MY_OAM)
  call RunDMA
  ret

RefreshCardTask:
  ld a, [vSelectedCardIndex]
  ld [vPreviousCardIndex], a  

  call DrawDeckMinimap
  call LoadCardData
  di
  nop
  ret
  
DrawDeckMinimap:
  ld hl, Cards 
  ld c, [hl] ; count of cards
  srl c 
  set 0, c
  
  ld hl, vSelectedCardIndex
  ld d, [hl] ; selected card!
  ld b, $ff ; start at -1 lol
  ld hl, $9800 + 32 + 11
  
.loop
  inc b
  ld a, b
  cp a, 5
  jp nz, .noNewLine
  ld [hl], $13
  ld hl, $9800 + 32*2 + 11
.noNewLine
  ld a, b
  cp a, c 
  jp nc, .doneDrawing
  ld a, d 
  srl a 
  cp a, b
  jp z, .drawSelectedCard
.drawUnselectedCards
  ld [hl], $12
  inc hl
  jp .loop
.drawSelectedCard
  bit 0, d 
  jp nz, .drawSelectedSecond
  ld [hl], $14
  inc hl
  jp .loop
.drawSelectedSecond
  ld [hl], $15
  inc hl
  jp .loop
.doneDrawing
  ld [hl], $13
  ret
  
  
CardBrowseTeardown:
  ret



CardBrowse.UITilemap:
	db $0e, $0a, $0a, $0a, $0a, $0a, $0a, $0a, $0a, $0f, $09, $02, $02, $02, $02, $02, $02, $02, $08, $01
	db $0b, $00, $00, $00, $00, $00, $00, $00, $00, $0c, $03, $00, $00, $00, $00, $00, $00, $00, $04, $01
	db $0b, $00, $00, $00, $00, $00, $00, $00, $00, $0c, $03, $00, $00, $00, $00, $00, $00, $00, $04, $01
	db $0b, $00, $00, $00, $00, $00, $00, $00, $00, $0c, $06, $05, $05, $05, $05, $05, $05, $05, $07, $01
	db $0b, $00, $00, $00, $00, $00, $00, $00, $00, $0c, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01
	db $0b, $00, $00, $00, $00, $00, $00, $00, $00, $0c, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01
	db $0b, $00, $00, $00, $00, $00, $00, $00, $00, $0c, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01
	db $0b, $00, $00, $00, $00, $00, $00, $00, $00, $0c, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01
	db $0b, $00, $00, $00, $00, $00, $00, $00, $00, $0c, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01
	db $0b, $00, $00, $00, $00, $00, $00, $00, $00, $0c, $01, $01, $01, $01, $01, $01, $01, $01, $01, $01
	db $0b, $00, $00, $00, $00, $00, $00, $00, $00, $0c, $02, $02, $02, $02, $02, $02, $02, $02, $02, $02
	db $0b, $00, $00, $00, $00, $00, $00, $00, $00, $0c, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00
	db $0b, $00, $00, $00, $00, $00, $00, $00, $00, $0c, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00
	db $0b, $00, $00, $00, $00, $00, $00, $00, $00, $0c, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00
	db $0b, $00, $00, $00, $00, $00, $00, $00, $00, $0c, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00
	db $0b, $00, $00, $00, $00, $00, $00, $00, $00, $0c, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00
	db $0b, $00, $00, $00, $00, $00, $00, $00, $00, $0c, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00
	db $11, $0d, $0d, $0d, $0d, $0d, $0d, $0d, $0d, $10, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00
CardBrowse.UITilemapEnd: