const std = @import("std");
const protocol = @import("protocol");
const Session = @import("../Session.zig");
const Packet = @import("../Packet.zig");
const Data = @import("../data.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);

    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 });
    }
    try rsp.FinishedMainMissionIdList.appendSlice(&Data.FinishedMainMissionIdList);
    try rsp.CurversionFinishedMainMissionIdList.appendSlice(&Data.FinishedMainMissionIdList);

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

pub fn onGetTutorialGuideStatus(session: *Session, _: *const Packet, allocator: Allocator) !void {
    var rsp = protocol.GetTutorialGuideScRsp.init(allocator);

    rsp.retcode = 0;
    for (Data.TutorialGuideIdList) |id| {
        try rsp.TutorialGuideList.append(protocol.TutorialGuide{ .id = id, .Status = protocol.TutorialStatus.TUTORIAL_FINISH });
    }

    try session.send(CmdID.CmdGetTutorialGuideScRsp, rsp);
}
pub fn onFinishTutorialGuideStatus(session: *Session, _: *const Packet, allocator: Allocator) !void {
    var rsp = protocol.FinishTutorialGuideScRsp.init(allocator);

    rsp.retcode = 0;
    for (Data.TutorialGuideIdList) |id| {
        rsp.TutorialGuide = .{ .id = id, .Status = protocol.TutorialStatus.TUTORIAL_FINISH };
    }

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

pub fn onGetTutorialStatus(session: *Session, _: *const Packet, allocator: Allocator) !void {
    var rsp = protocol.GetTutorialScRsp.init(allocator);
    rsp.retcode = 0;
    for (Data.FinishedTutorialIdList) |id| {
        try rsp.TutorialList.append(protocol.Tutorial{ .id = 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);
}
