const std = @import("std");
const protocol = @import("protocol");
const Session = @import("../Session.zig");
const Packet = @import("../Packet.zig");
const Data = @import("../data.zig");
const Config = @import("config.zig");

const ArrayList = std.ArrayList;
const Allocator = std.mem.Allocator;
const CmdID = protocol.CmdID;

pub fn onGetMissionStatus(session: *Session, packet: *const Packet, allocator: Allocator) !void {
    const req = try packet.getProto(protocol.GetMissionStatusCsReq, allocator);
    var rsp = protocol.GetMissionStatusScRsp.init(allocator);
    const main_mission_config = try Config.loadMainMissionConfig(allocator, "resources/MainMission.json");
    rsp.retcode = 0;
    for (req.sub_mission_id_list.items) |id| {
        try rsp.SubMissionStatusList.append(protocol.Mission{ .id = id, .status = protocol.MissionStatus.MISSION_FINISH, .progress = 1 });
    }
    for (main_mission_config.main_mission_config.items) |main_missionConf| {
        try rsp.FinishedMainMissionIdList.append(main_missionConf.main_mission_id);
        try rsp.CurversionFinishedMainMissionIdList.append(main_missionConf.main_mission_id);
    }
    try session.send(CmdID.CmdGetMissionStatusScRsp, rsp);
}

pub fn onGetTutorialGuideStatus(session: *Session, _: *const Packet, allocator: Allocator) !void {
    var rsp = protocol.GetTutorialGuideScRsp.init(allocator);
    const tutorial_guide_config = try Config.loadTutorialGuideConfig(allocator, "resources/TutorialGuideGroup.json");

    rsp.retcode = 0;
    for (tutorial_guide_config.tutorial_guide_config.items) |guideConf| {
        try rsp.TutorialGuideList.append(protocol.TutorialGuide{ .id = guideConf.guide_group_id, .Status = protocol.TutorialStatus.TUTORIAL_FINISH });
    }

    try session.send(CmdID.CmdGetTutorialGuideScRsp, rsp);
}

pub fn onGetTutorialStatus(session: *Session, _: *const Packet, allocator: Allocator) !void {
    var rsp = protocol.GetTutorialScRsp.init(allocator);
    const tutorial_guide_config = try Config.loadTutorialConfig(allocator, "resources/TutorialData.json");
    rsp.retcode = 0;
    for (tutorial_guide_config.tutorial_config.items) |tutorialConf| {
        try rsp.TutorialList.append(protocol.Tutorial{ .id = tutorialConf.tutorial_id, .Status = protocol.TutorialStatus.TUTORIAL_FINISH });
    }
    try session.send(CmdID.CmdGetTutorialScRsp, rsp);
}

// added this to auto detect new tutorial guide id
pub fn onUnlockTutorialGuide(session: *Session, packet: *const Packet, allocator: Allocator) !void {
    const req = try packet.getProto(protocol.UnlockTutorialGuideCsReq, allocator);
    var rsp = protocol.UnlockTutorialGuideScRsp.init(allocator);
    rsp.retcode = 0;
    std.debug.print("UNLOCK TUTORIAL GUIDE ID: {}\n", .{req.group_id});
    try session.send(CmdID.CmdUnlockTutorialGuideScRsp, rsp);
}
