// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use std::collections::{HashMap, HashSet};
use std::{io::Cursor, path::PathBuf};
use std::io::Seek;
use serde_json::{Map, Result, Value};

mod ftp;
mod java;
mod prism;
mod system_dirs;
mod util;
mod modpack;

struct ModpackEntry{
    name: String,
    id: u8
}

// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
#[tauri::command]
fn greet(name: &str) -> String {
    format!("Hello, {}! You've been greeted from Rust!", name)
}

#[tauri::command]
fn get_modpacks() -> Vec<ModpackEntry> {
    let mut buf = Cursor::new(vec![]);
    ftp::ftp_retr(PathBuf::new().join("modpacks.json"), &mut buf, |_| return);
    buf.rewind();
    let v: Value = serde_json::from_reader(buf).unwrap();
    println!("{}", v[0]["name"]);
    let mut packs: Vec<ModpackEntry> = Vec::new();
    for pack in v.as_array().unwrap() {
        packs.push(ModpackEntry{name: pack["name"].as_str().unwrap().to_string(), id: pack["id"].into()});
    }
    return packs;
}

fn main() {
    prism::install_prism();
    tauri::Builder::default()
        .invoke_handler(tauri::generate_handler![greet, get_modpacks])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
