#include "macros/scrcmd.inc"
#include "res/text/bank/canalave_city_gym.h"


    ScriptEntry _0012
    ScriptEntry _0016
    ScriptEntry _0126
    ScriptEntry _015A
    ScriptEntryEnd

_0012:
    InitPersistedMapFeaturesForCanalaveGym
    End

_0016:
    PlayFanfare SEQ_SE_CONFIRM
    LockAll
    FacePlayer
    CheckBadgeAcquired BADGE_ID_MINE, VAR_RESULT
    GoToIfEq VAR_RESULT, 1, _0107
    CreateJournalEvent LOCATION_EVENT_GYM_WAS_TOO_TOUGH, 35, 0, 0, 0
    Message 0
    CloseMessage
    StartTrainerBattle TRAINER_LEADER_BYRON
    CheckWonBattle VAR_RESULT
    GoToIfEq VAR_RESULT, FALSE, _0120
    Message 1
    BufferPlayerName 0
    Message 2
    PlaySound SEQ_BADGE
    WaitSound
    GiveBadge BADGE_ID_MINE
    IncrementTrainerScore2 TRAINER_SCORE_EVENT_BADGE_EARNED
    SetTrainerFlag TRAINER_BLACK_BELT_DAVID
    SetTrainerFlag TRAINER_WORKER_JACKSON
    SetTrainerFlag TRAINER_WORKER_GARY
    SetTrainerFlag TRAINER_ACE_TRAINER_CESAR
    SetTrainerFlag TRAINER_ACE_TRAINER_BREANNA
    SetTrainerFlag TRAINER_BLACK_BELT_RICKY
    SetTrainerFlag TRAINER_WORKER_GERARDO
    CreateJournalEvent LOCATION_EVENT_BEAT_GYM_LEADER, 35, TRAINER_LEADER_BYRON, 0, 0
    ClearFlag FLAG_UNK_0x01B2
    ClearFlag FLAG_UNK_0x02CC
    ClearFlag FLAG_UNK_0x01B8
    ClearFlag FLAG_UNK_0x01B6
    ClearFlag FLAG_UNK_0x01B7
    SetVar VAR_UNK_0x4078, 2
    SetFlag FLAG_UNK_0x0198
    Message 3
    GoTo _00BF

_00BF:
    SetVar VAR_0x8004, ITEM_TM91
    SetVar VAR_0x8005, 1
    GoToIfCannotFitItem VAR_0x8004, VAR_0x8005, VAR_RESULT, _00FD
    CallCommonScript 0x7FC
    SetFlag FLAG_UNK_0x0092
    BufferItemName 0, VAR_0x8004
    BufferTMHMMoveName 1, VAR_0x8004
    Message 4
    WaitABXPadPress
    CloseMessage
    ReleaseAll
    End

_00FD:
    CallCommonScript 0x7E1
    CloseMessage
    ReleaseAll
    End

_0107:
    GoToIfUnset FLAG_UNK_0x0092, _00BF
    BufferRivalName 1
    Message 5
    WaitABXPadPress
    CloseMessage
    ReleaseAll
    End

_0120:
    BlackOutFromBattle
    ReleaseAll
    End

_0126:
    PlayFanfare SEQ_SE_CONFIRM
    LockAll
    FacePlayer
    CheckBadgeAcquired BADGE_ID_MINE, VAR_RESULT
    GoToIfEq VAR_RESULT, 1, _014C
    Message 6
    WaitABXPadPress
    CloseMessage
    ReleaseAll
    End

_014C:
    BufferPlayerName 0
    Message 7
    WaitABXPadPress
    CloseMessage
    ReleaseAll
    End

_015A:
    PlayFanfare SEQ_SE_CONFIRM
    LockAll
    CheckBadgeAcquired BADGE_ID_MINE, VAR_RESULT
    GoToIfEq VAR_RESULT, 1, _0184
    BufferRivalName 0
    BufferRivalName 1
    Message 8
    WaitABXPadPress
    CloseMessage
    ReleaseAll
    End

_0184:
    BufferRivalName 0
    BufferPlayerName 1
    BufferRivalName 2
    Message 9
    WaitABXPadPress
    CloseMessage
    ReleaseAll
    End
