require 'common'

CM_EVENTS_NAMES = {
    [0] = "NO_MESSAGE",
    [1] = "TOURNAMENT_MODE_ACTIVATED",
    [2] = "SOAK_STARTED",
    [3] = "INITIAL_USER_ADDED",
    [4] = "ABOUT_TO_INIT_MODE",
    [5] = "CAREER_TYPE_SELECTED",
    [6] = "USER_MATCH_COMPLETED",
    [7] = "USER_MATCH_COMPLETED_IN_TOURNAMENT",
    [8] = "CPU_MATCH_COMPLETED",
    [9] = "USER_INTERNATIONAL_MATCH_COMPLETED",
    [10] = "CPU_INTERNATIONAL_MATCH_COMPLETED",
    [11] = "PRE_SEASON_STARTED",
    [12] = "PRE_SEASON_COMPLETED",
    [13] = "HOURS_PASSED",
    [14] = "DAY_PASSED",
    [15] = "WEEK_PASSED",
    [16] = "MONTH_PASSED",
    [17] = "MID_SEASON_REACHED",
    [18] = "END_OF_SEASON_REACHED",
    [19] = "SUSPENSION",
    [20] = "BACK_FROM_SUSPENSION",
    [21] = "MAIN_COMPETITION_SCHEDULED",
    [22] = "SEASON_RESET",
    [23] = "SEASON_ENDED",
    [24] = "FIXTURES_RESCHEDULED",
    [25] = "ENTERED_HUB_FIRST_TIME",
    [26] = "DATA_READY",
    [27] = "PREPARE_FOR_SAVE",
    [28] = "POST_LOAD_PREPARE",
    [29] = "PREPARE_FOR_FIRST_SAVE",
    [30] = "COMPETITION_COMPLETE",
    [31] = "STAGE_STARTED",
    [32] = "STAGE_COMPLETE",
    [33] = "ABOUT_TO_ENTER_PREMATCH",
    [34] = "ABOUT_TO_LEAVE_PREMATCH",
    [35] = "POST_MATCH_REPORTS_DISPLAYED",
    [36] = "DEBUG_LOG_FOR_MATCH_NOW",
    [37] = "TEAMS_READY_FOR_USER_MATCH",
    [38] = "ABOUT_TO_ENTER_A_MATCH",
    [39] = "FE_TO_BE",
    [40] = "ABOUT_TO_SIM_OVER_A_MATCH",
    [41] = "ABOUT_TO_ENTER_QUICK_SIM",
    [42] = "ABOUT_TO_ENTER_INTERACTIVE_SIM",
    [43] = "ABOUT_TO_ENTER_PLAY_HIGHLIGHTS",
    [44] = "HIGHLIGHT_PLAYED",
    [45] = "TEAM_MOVE",
    [46] = "LEAGUE_OBJECTIVE_UPDATED",
    [47] = "CUP_OBJECTIVE_UPDATED",
    [48] = "COMPETITION_STARTED",
    [49] = "PLAYER_GROWTH",
    [50] = "BOARD_EMAIL_EVENT",
    [51] = "MANAGER_PRESTIGE_EVENT",
    [52] = "PLAYER_UNFIT",
    [53] = "PLAYER_FORM_EVENT",
    [54] = "PLAYER_INSERTED_INTO_PLAYERS_TABLE",
    [55] = "PLAYER_DELETED_FROM_PLAYERS_TABLE",
    [56] = "PLAYERS_RETIRED",
    [57] = "ALL_RETIRED_PLAYERS_COMPLETE",
    [58] = "PLAYER_CONTRACT_TERMINATION",
    [59] = "PLAYER_CONTRACT_ACCEPTED",
    [60] = "PLAYER_CONTRACT_NEGOTIATION",
    [61] = "PLAYER_WITH_POTENTIAL",
    [62] = "BACK_FROM_INJURY",
    [63] = "PRIZE_MONEY_RECEIVED",
    [64] = "SCREEN_HAS_DONE_LOADING",
    [65] = "USER_TEAM_PLAYER_FORM_CHANGE",
    [66] = "USER_TEAM_ADVANCES_IN_CUP",
    [67] = "USER_TEAM_OUT_OF_CUP",
    [68] = "USER_TEAM_PLAYER_RETIRES_MID_SEASON",
    [69] = "USER_TEAM_PLAYER_RETIRES",
    [70] = "CPU_TRANSFER_INFO",
    [71] = "NUM_DAYS_BEFORE_USER_GAME",
    [72] = "NUM_DAYS_BEFORE_LAST_LEAGUE_GAME",
    [73] = "LOAN_IS_RETURNING_OR_BEING_RECALLED",
    [74] = "PLAY_TIME_MORALE_CHANGE",
    [75] = "PLAYER_UNHAPPY_WITH_WAGE",
    [76] = "PLAYER_MADE_AVAILABLE_FOR_TRANSFER",
    [77] = "PLAYER_MADE_AVAILABLE_FOR_LOAN",
    [78] = "ENTERING_TEAM_MANAGEMENT",
    [79] = "LEAVING_TEAM_MANAGEMENT",
    [80] = "YOUTH_SCOUT_REPORT_IS_AVAILABLE",
    [81] = "TRANSFER_MOVE_ABOUT_TO_COMPLETE",
    [82] = "TRANSFER_MOVE_COMPLETE",
    [83] = "USER_PRESIGNED_CONTRACT_CREATED",
    [84] = "TRANSFER_SCOUTED_PLAYER",
    [85] = "SCOUT_SENT_ON_AREA_MISSION",
    [86] = "SCOUT_RETURNED_WITH_REPORT",
    [87] = "GTN_SCOUT_HIRED",
    [88] = "GTN_SCOUT_FIRED",
    [89] = "COMMENTATOR_NOTES_DATA_INIT",
    [90] = "TRANSFER_BID_REJECTED",
    [91] = "TRANSFER_BID_RETRACTED",
    [92] = "TRANSFER_BID_COUNTER_OFFER",
    [93] = "TRANSFER_BID_COUNTER_OFFER_REJECTED_BY_CPU",
    [94] = "TRANSFER_WINDOW_CLOSE_DAY_REACHED",
    [95] = "TRANSFER_WINDOW_NUM_DAYS_TIL_CLOSE",
    [96] = "TRANSFER_WINDOW_NUM_DAYS_SINCE_OPEN",
    [97] = "TRANSFER_WINDOW_OPENING_SOON",
    [98] = "TRANSFER_WINDOW_CLOSED",
    [99] = "LOAN_BUY_LOAN_CONTRACT_ACCEPTED",
    [100] = "LOAN_BUY_LOAN_CONTRACT_REJECTED",
    [101] = "LOAN_BUY_FUTURE_FEE_DUE",
    [102] = "LOAN_BUY_NEGOTIATE_PLAYER_CONTRACT_OFFER",
    [103] = "LOAN_BUY_FUTURE_FEE_NOT_PAYED",
    [104] = "LIVE_TABLE_FIRST_UPDATE",
    [105] = "YOUTH_PLAYER_ADDED_TO_YOUTH_ACADEMY",
    [106] = "PLAYER_ADDED_TO_TEAM",
    [107] = "PLAYER_REMOVED_FROM_TEAM",
    [108] = "YOUTH_PLAYER_PROMOTION",
    [109] = "YOUTH_PLAYERS_STATS_UPDATE",
    [110] = "YOUTH_PLAYERS_RISK_FACTOR",
    [111] = "YOUTH_PLAYERS_RETIREMENT",
    [112] = "YOUTH_SCOUT_IS_AVAILABLE",
    [113] = "YOUTH_SCOUT_HIRED",
    [114] = "YOUTH_SCOUT_FIRED",
    [115] = "YOUTH_SCOUT_IS_SENT_ON_MISSION",
    [116] = "DEADLINE_DAYS_STARTED",
    [117] = "DEADLINE_DAYS_ENDED",
    [118] = "LONGTERM_INJURY",
    [119] = "USER_AWARD",
    [120] = "FINANCES_BOARD_ACCEPTED_THE_REQUEST",
    [121] = "FINANCES_BOARD_MADE_A_COUNTER_OFFER",
    [122] = "FINANCES_BOARD_REJECTED_THE_REQUEST",
    [123] = "FINANCES_BOARD_DOESNT_NOT_HAVE_CONFIDENCE",
    [124] = "INTERNATIONAL_JOB_OFFER_MADE",
    [125] = "INTERNATIONAL_JOB_OFFER_ACCEPTED",
    [126] = "INTERNATIONAL_JOB_OFFER_DECLINED",
    [127] = "INTERNATIONAL_JOB_OFFER_STALLED",
    [128] = "INTERNATIONAL_JOB_OFFER_EXPIRED",
    [129] = "INTERNATIONAL_JOB_OVER",
    [130] = "INTERNATIONAL_JOB_FIRED",
    [131] = "INTERNATIONAL_JOB_RESIGNED",
    [132] = "INTERNATIONAL_JOB_REVIEW",
    [133] = "INTERNATIONAL_JOB_PAP_SELECTED",
    [134] = "INTERNATIONAL_JOB_PAP_NOT_SELECTED",
    [135] = "INTERNATIONAL_JOB_PAP_UPCOMING_FIXTURES",
    [136] = "INTERNATIONAL_TOP_PLAYERS",
    [137] = "JOB_OFFER_RECEIVED",
    [138] = "PAP_TRANSFER_LOAN_REQUEST",
    [139] = "PAP_REQUEST_MANAGER_DECISION",
    [140] = "PAP_OFFER_RECEIVED",
    [141] = "PAP_OFFER_EXPIRED",
    [142] = "JOB_OFFER_ACCEPTED",
    [143] = "JOB_OFFER_REJECTED",
    [144] = "JOB_OFFER_EXPIRED",
    [145] = "ABOUT_TO_SWITCH_JOBS",
    [146] = "PAP_JOB_SWITCH_ACCEPTED",
    [147] = "PAP_JOB_SWITCH_REJECTED",
    [148] = "JOB_OFFERS_GENERATED",
    [149] = "PAP_WITHDRAW_LIST_REQ",
    [150] = "PAP_WITHDRAW_LIST_RESP",
    [151] = "PAP_TRANSFER_LISTED_BY_CPU_MANAGER",
    [152] = "PAP_REQUEST_TO_PLAY_A_MATCH",
    [153] = "TABLE_RIVALS",
    [154] = "PLAYER_ANNOUNCMENT",
    [155] = "COMPETITION_CHAMPION",
    [156] = "LIVE_TABLE_UPDATE",
    [157] = "CAREER_TRAINING_SESSION_COMPLETED",
    [158] = "CAREER_TRAINING_SESSION_RESET",
    [159] = "PAP_TRAINING_DRILL_COMPLETED",
    [160] = "USER_TRAINING_COMPLETED",
    [161] = "USER_SHORTLISTED",
    [162] = "USER_TRANSFERLISTED",
    [163] = "USER_LOANLISTED",
    [164] = "USER_TRANSFER_CLUB_STAGE_FINISHED",
    [165] = "USER_LOAN_CLUB_STAGE_FINISHED",
    [166] = "USER_TRANSFER_PLAYER_STAGE_FINISHED",
    [167] = "USER_LOAN_PLAYER_STAGE_FINISHED",
    [168] = "USER_TRANSFER_OFFER_RECEIVED",
    [169] = "USER_LOAN_OFFER_RECEIVED",
    [170] = "USER_CONTRACT_RENEWAL",
    [171] = "HIGHLIGHTS_READY",
    [172] = "OBJECTIVES_GENERATED",
    [173] = "USER_PLAYER_POSITION_CHANGED",
    [174] = "USER_PLAYER_TRAINING_PLAN_CHANGED",
    [175] = "USER_PLAYER_TRAINING_PLAN_FINISHED",
    [176] = "TRAINING_DAY_COMPLETED",
    [177] = "USER_EXPIRING_LOANS",
    [178] = "TRAININGDAYTYPE_HAS_CHANGED_FOR_ONE_OR_MORE_DAYS",
    [179] = "MATCH_RESTARTED",
    [180] = "FIXTURE_USER_CHANGED",
    [181] = "GOLDEN_BOOT_AWARD_EVENT",
    [182] = "SET_NUM_USERS",
    [183] = "NON_NEWS_MAX",
    [184] = "SCRIPT_DRIVEN_NEWS",
    [185] = "INJURY",
    [186] = "GOLDEN_BOOT_NEWS_EVENT",
    [187] = "TEAM_OF_THE_WEEK",
    [188] = "GOAL_KEEPER_OF_THE_TOURNAMENT",
    [189] = "PLAYER_OF_THE_YEAR",
    [190] = "FIGHT_4_PLAYER_OF_THE_YEAR",
    [191] = "MANAGER_OF_THE_MONTH",
    [192] = "MANAGER_OF_THE_SEASON",
    [193] = "PLAYER_OF_THE_MONTH",
    [194] = "FIGHT_4_PLAYER_OF_THE_MONTH",
    [195] = "PLAYER_OF_THE_TOURNAMENT",
    [196] = "TEAM_OF_THE_COMPETITION",
    [197] = "TRANSFERS_MESSAGING_EVENTS",
    [198] = "CHAMPION_OF_COMPETITION",
    [199] = "CHAMPION_OF_CHAMPIONS_LEAGUE",
    [200] = "CHAMPION_OF_EUROPA_LEAGUE",
    [201] = "CHAMPION_OF_UEFA_SUPERCUP",
    [202] = "CHAMPION_OF_FIFA_WORLD_CUP",
    [203] = "PROMOTED_TEAMS",
    [204] = "RELEGATED_TEAMS",
    [205] = "ENTERED_CHAMPION_SLOT",
    [206] = "ENTERED_PROMOTION_SLOT",
    [207] = "ENTERED_RELEGATION_SLOT",
    [208] = "FIRST_LEAGUE_FIXTURE",
    [209] = "TEAM_WINS_AGAINST_RIVAL",
    [210] = "UNDERDOG_TEAM_WINS_AGAINST_BIGGER_OPPONENT",
    [211] = "PLAYERS_TEAM_PLANS_TO_BUY_PLAYER",
    [212] = "NEWS_WORTHY_TRANSFER_EVENT",
    [213] = "HAT_TRICK",
    [214] = "UPCOMING_MATCH",
    [215] = "CUP_TOURNAMENT_STARTED",
    [216] = "CUP_TOURNAMENT_STARTED2",
    [217] = "STAR_PLAYER_LISTED",
    [218] = "PRE_CONTRACT_MADE",
    [219] = "PRE_CONTRACT_COMPLETE",
    [220] = "BOLSTER",
    [221] = "TRANSFER_REPLACE_PLAYER",
    [222] = "TRANSFER_RUMOUR_TOP_SCORER_ASSISTER",
    [223] = "PRE_SEASON_TOURNAMENT",
    [224] = "CLUB_WORTH_INCREASED_NEWS",
    [225] = "CAREER_REAL_PLAYER_START_NEWS",
    [226] = "CAREER_VPRO_START_NEWS",
    [227] = "CAREER_MANAGER_START_NEWS",
    [228] = "OBJECTIVES_INTRO_NEWS",
    [229] = "MATCH_DECIDED_IN_PENALTY_SHOOTOUT",
    [230] = "NON_USER_MATCH_RESULT",
    [231] = "INTERNATIONAL_WEEK_NEWS",
    [232] = "INTERNATIONAL_KNOCKOUT_STAGE_NEWS",
    [233] = "INTERNATIONAL_GROUP_STAGE_NEWS",
    [234] = "6_POINTER_LOGIC",
    [235] = "MARQUEE_MATCH_NEWS",
    [236] = "RIVALS_NON_USER_NEWS",
    [237] = "CLINCH_NEWS_FOR_LEAGUE_TITLE_POSSIBLE_WIN",
    [238] = "CLINCH_NEWS_FOR_LEAGUE_TITLE_POSSIBLE_DRAW",
    [239] = "CLINCH_NEWS_FOR_LEAGUE_PROMOTION_POSSIBLE_WIN",
    [240] = "CLINCH_NEWS_FOR_LEAGUE_PLAYOFF_POSSIBLE_WIN",
    [241] = "CLINCH_NEWS_FOR_CHAMPIONS_LEAGUE_QUALIFICATION_WIN",
    [242] = "CLINCH_NEWS_FOR_EUROPA_LEAGUE_QUALIFICATION_WIN",
    [243] = "ONES_TO_WATCH",
    [244] = "ONES_FOR_THE_FUTURE",
    [245] = "CUP_POSSIBLE_QUALIFICATION_WITH_A_WIN",
    [246] = "CUP_POSSIBLE_QUALIFICATION_WITH_A_WIN_AND_USER_TEAM_EXPECTED_TO_QUALIFY",
    [247] = "CUP_POSSIBLE_ELIMINATION_WITH_A_DRAW",
    [248] = "CUP_POSSIBLE_ELIMINATION_IF_USER_DOES_NOT_WIN",
    [249] = "CUP_STAGE_MATCH",
    [250] = "FIGHT_FOR_TOP_SCORER",
    [251] = "LEAGUE_TITLE_CLINCHED",
    [252] = "LEAGUE_PLAYOFF_CLINCHED",
    [253] = "LEAGUE_PROMOTION_CLINCHED",
    [254] = "EUROPA_LEAGUE_QUALIFICATION_CLINCHED",
    [255] = "CHAMPIONS_LEAGUE_QUALIFICATION_CLINCHED",
    [256] = "CUP_POSSIBLE_ELIMINATION_CLINCHED",
    [257] = "CUP_POSSIBLE_ELIMINATION_CLINCHED_EXPECTED",
    [258] = "CUP_QUALIFICATION_CLINCHED",
    [259] = "UCL_DRAW_DATE_GROUPS_ANTICIPATION_NEWS",
    [260] = "EL_DRAW_DATE_GROUPS_ANTICIPATION_NEWS",
    [261] = "SUDA_DRAW_DATE_GROUPS_ANTICIPATION_NEWS",
    [262] = "UCL_DRAW_DATE_GROUPS_NEWS",
    [263] = "EL_DRAW_DATE_GROUPS_NEWS",
    [264] = "SUDA_DRAW_DATE_GROUPS_NEWS",
    [265] = "UCL_DRAW_DATE_GROUPS_REACTION_NEWS",
    [266] = "EL_DRAW_DATE_GROUPS_REACTION_NEWS",
    [267] = "SUDA_DRAW_DATE_GROUPS_REACTION_NEWS",
    [268] = "UCL_DRAW_DATE_FIRST_KO_ROUND_ANTICIPATION_NEWS",
    [269] = "EL_DRAW_DATE_FIRST_KO_ROUND_ANTICIPATION_NEWS",
    [270] = "EL_DRAW_DATE_FIRST_KO_ROUND_NEWS",
    [271] = "EL_DRAW_DATE_FIRST_KO_ROUND_REACTION_NEWS",
    [272] = "UCL_DRAW_DATE_KO_ROUND_NEWS",
    [273] = "EL_DRAW_DATE_KO_ROUND_NEWS",
    [274] = "UCL_DRAW_DATE_KO_ROUND_REACTION_NEWS",
    [275] = "EL_DRAW_DATE_KO_ROUND_REACTION_NEWS",
    [276] = "UEFA_TEAM_OF_THE_YEAR",
    [277] = "FIGHT_4_UEFA_TEAM_OF_THE_YEAR",
    [278] = "DEBUG_NEWS",
    [279] = "CHAMPION_OF_COPA_LIBERTADORES",
    [280] = "CHAMPION_OF_COPA_SUDAMERICANA",
    [281] = "CHAMPION_OF_RECOPA_SUDAMERICANA",
    [282] = "LIBE_DRAW_DATE_GROUPS_ANTICIPATION_NEWS",
    [283] = "LIBE_DRAW_DATE_GROUPS_NEWS",
    [284] = "LIBE_DRAW_DATE_GROUPS_REACTION_NEWS",
    [285] = "LIBE_DRAW_DATE_FIRST_KO_ROUND_ANTICIPATION_NEWS",
    [286] = "SUDA_DRAW_DATE_FIRST_KO_ROUND_ANTICIPATION_NEWS",
    [287] = "LIBE_DRAW_DATE_KO_ROUND_NEWS",
    [288] = "SUDA_DRAW_DATE_KO_ROUND_NEWS",
    [289] = "LIBE_DRAW_DATE_KO_ROUND_REACTION_NEWS",
    [290] = "SUDA_DRAW_DATE_KO_ROUND_REACTION_NEWS",
    [291] = "CHAMPION_OF_EUROPA_CONFERENCE_LEAGUE",
    [292] = "UECL_DRAW_DATE_GROUPS_ANTICIPATION_NEWS",
    [293] = "UECL_DRAW_DATE_GROUPS_NEWS",
    [294] = "UECL_DRAW_DATE_GROUPS_REACTION_NEWS",
    [295] = "UECL_DRAW_DATE_FIRST_KO_ROUND_ANTICIPATION_NEWS",
    [296] = "UECL_DRAW_DATE_FIRST_KO_ROUND_NEWS",
    [297] = "UECL_DRAW_DATE_FIRST_KO_ROUND_REACTION_NEWS",
    [298] = "UECL_DRAW_DATE_KO_ROUND_NEWS",
    [299] = "UECL_DRAW_DATE_KO_ROUND_REACTION_NEWS",
}

function get_cm_event_name_by_id(event_id)
    if (event_id < 0 or event_id >= 300) then
        -- INVALID ID
        return string.format("EVENT_%d", event_id)
    end

    return CM_EVENTS_NAMES[event_id]
end

function get_user_senior_team_playerids()
    local result = {}
    local user_teamid = GetUserTeamID()
    
    -- From this table should be the quickest I guess
    local rows = GetDBTableRows("career_playercontract")
    for i=1, #rows do
         local row = rows[i]
         local teamid = math.floor(row.teamid.value)
         -- Filter out youth players
         if (teamid == user_teamid) then
            local iplayerid = math.floor(row.playerid.value)
            result[iplayerid] = true
         end  
    end
    return result
end

function user_team_set_form(v)
    local team_playerids = get_user_senior_team_playerids()
    local players_count = table_count(team_playerids)

    local updated_players = 0

    local rows = GetDBTableRows("players")
    for i=1, #rows do
        local row = rows[i]
        local playerid = math.floor(row.playerid.value)

        if (team_playerids[playerid]) then
            SetPlayerForm(playerid, v)
            updated_players = updated_players + 1
        end

        if (updated_players == players_count) then
            return
        end
    end
end

function user_team_set_sharpness(v)
    local team_playerids = get_user_senior_team_playerids()
    local players_count = table_count(team_playerids)

    local updated_players = 0

    local rows = GetDBTableRows("players")
    for i=1, #rows do
        local row = rows[i]
        local playerid = math.floor(row.playerid.value)

        if (team_playerids[playerid]) then
            SetPlayerSharpness(playerid, v)
            updated_players = updated_players + 1
        end

        if (updated_players == players_count) then
            return
        end
    end
end

function user_team_set_morale(v)
    local team_playerids = get_user_senior_team_playerids()
    local players_count = table_count(team_playerids)

    local updated_players = 0

    local rows = GetDBTableRows("players")
    for i=1, #rows do
        local row = rows[i]
        local playerid = math.floor(row.playerid.value)

        if (team_playerids[playerid]) then
            SetPlayerMorale(playerid, v)
            updated_players = updated_players + 1
        end

        if (updated_players == players_count) then
            return
        end
    end
end

