package be.msyben.vinylManager.tools;

import be.msyben.vinylManager.entities.Album;
import be.msyben.vinylManager.entities.Collection;
import be.msyben.vinylManager.exceptions.VinylManagerException;
import be.msyben.vinylManager.tools.discogs.DiscogsSearch;
import be.msyben.vinylManager.tools.discogs.entities.FolderRelease;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

public class ImportDiscogsTool {

    private static final int NBR_IN_COL = 500;

    public static void importDiscogsProgrammaticOnly(String userid, String path) {
        WaitThread wt = new WaitThread(false);
        try {
            Path in = downloadIds(wt, userid, path);
            downloadAlbums(in, userid, path);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static Path downloadIds(WaitThread wt, String userid, String path) throws IOException {

        File f = new File(path, "list_"+userid+".txt");
        if (f.exists()) {
            System.out.println("Liste déjà téléchargée");
        } else {
            
            StringBuilder sb = new StringBuilder();
            for (FolderRelease fr : DiscogsSearch.getReleases(userid, wt)) {
                sb.append(fr.getId()).append("\r\n");
            }
            Files.writeString(f.toPath(), sb, Charset.forName("UTF-8"));
        }
        
        System.out.println("Création de la liste des ids terminée");
        return f.toPath();
    }

    private static void downloadAlbums(Path in, String userid, String path) throws IOException, VinylManagerException {
        
        List<String> lines = Files.readAllLines(in);
        List<Long> ids = new ArrayList<>();
        for (String line : lines) {
            try {
                ids.add(Long.parseLong(line));
            } catch (NumberFormatException e) {

            }
        }

        Collection c = new Collection();
        for (int i = 0; i < ids.size(); i++) {
            
            int vmIdx = i/NBR_IN_COL + 1;
            File f = new File(path, userid + "_" + vmIdx + ".vm");
            if (f.exists()) {
                System.out.println("Le fichier " + f.getName() + " existe déjà");
            } else {
                
                System.out.println("Ajout de l'album " + (i+1) + " / " + ids.size() + " dans ce qui sera le fichier " + f.getName());
                Album a = DiscogsSearch.getAlbum(ids.get(i), null);
                /*Album a =  new Album();
                a.setArtiste("A " + i);
                a.setTitre("T " + i);*/
                if (a != null) {
                    c.getAlbums().add(a);
                }
                
                if (i > 0 && (i+1) % NBR_IN_COL == 0) {
                    System.out.println("Sauvegarde de " + f.getName());
                    SerialAndZip.save(c, f);
                    c = new Collection();
                }
            }
            
        }

        int vmIdx = (ids.size()-1)/NBR_IN_COL + 1;
        File f = new File(path, userid + "_" + vmIdx + ".vm");
        if (f.exists()) {
            System.out.println("Le fichier " + f.getAbsolutePath() + " existe déjà");
        } else {
            System.out.println("Sauvegarde finale de " + f.getName());
            SerialAndZip.save(c, f);
            c = new Collection();
        }

    }
}
