use std::clone;
use tauri::Emitter;

use serde::Serialize;

#[derive(Clone, Serialize)]
pub struct DownloadStatus {
    downloaded: usize,
    total: usize,
    time_elapsed: usize,
    download_name: String,
}

pub fn download_callback(
    download_name: String,
    window: Option<tauri::AppHandle>,
    count: usize,
    size: usize,
) {
    unsafe {
        static mut total: usize = 0;
        total += count;
        if let Some(window1) = window.clone() {
            window1.emit(
                "download_progress",
                DownloadStatus {
                    downloaded: total,
                    total: size,
                    time_elapsed: 0,
                    download_name: download_name,
                },
            );
        }
        println!(
            "Downloading {}MB / {}MB",
            total / (1024 * 1024),
            size / (1024 * 1024)
        );
        if count == 0 {
            if let Some(window2) = window {
                window2.emit("download_finished", true);
            }
            total = 0;
        }
    }
}
