const std = @import("std");
const cwd = std.fs.cwd;
const json = std.json;
const print = std.debug.print;
const oracleFileName = "oracle-cards-20240701090158.json";

const Card = struct {
    name: []const u8 = "", //string
    mana_cost: []const u8 = "", //string
    cmc: f32 = 0, //technically a float? but I think we can always cast safely cast. EDIT: NOPE
    type_line: []const u8 = "", //string
    oracle_text: []const u8 = "", //string
    power: []const u8 = "", //coerced to string
    toughness: []const u8 = "", //coerced to string
    card_faces: ?[]Card = null, //array of cards
};

const testCardName = "Raffine, Scheming Seer";

const cardList = std.MultiArrayList(Card);
pub fn main() !void {
    var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
    defer arena.deinit();

    const allocator = arena.allocator();
    const oracleFile = try cwd().openFile(oracleFileName, .{});
    const fileReader = oracleFile.reader();
    // var jsonReader = json.reader(allocator, oracleFile.reader());
    // var fields = std.ArrayList(Field).init(allocator);
    // var isVal = false;
    // var cards = std.StringHashMap(*StringHashMap);
    // var currentCard = std.ArrayList(std.json.Token).init(allocator);
    var depth: u32 = 0;
    var cardString = std.ArrayList(u8).init(allocator);
    var idx: u32 = 0;
    while (fileReader.readByte()) |char| {
        if (char == '{') depth += 1;
        if (depth != 0) try cardString.append(char);
        if (char == '}') {
            depth -= 1;
            if (depth == 0) {
                // print("{s}\n", .{cardString.items});
                const parsedCard = try std.json.parseFromSlice(Card, allocator, cardString.items, .{ .ignore_unknown_fields = true });
                print("{s}\n", .{parsedCard.value.name});
                cardString.clearAndFree();
            }
        }
        idx += 1;
    } else |e| {
        return e;
    }
}
