package be.msyben.vinylManager.tools;

import be.msyben.vinylManager.entities.Album;
import be.msyben.vinylManager.entities.Collection;
import be.msyben.vinylManager.entities.Image;
import be.msyben.vinylManager.exceptions.VinylManagerException;
import java.io.File;

public final class CollectionTools {
    private CollectionTools() {}
    
    
    public static void resizeCollectionImage(String path, int maxSize) {
        try {
            File f = new File(path);
            File fout = new File(f.getParent(), "resized.vm");
            Collection c = (Collection) SerialAndZip.load(f);
            resizeAllImages(c, maxSize);
            SerialAndZip.save(c, fout);
        } catch (VinylManagerException ex) {
            ex.printStackTrace();
        }
    }
    private static void resizeAllImages(Collection c, int maxSize) {
        int total = c.getAlbums().size();
        int idx = 0;
        for (Album a : c.getAlbums()) {
            System.out.println(idx++ + " / " + total);
            if (a.getImage() != null && a.getImage().getContenu() != null) {
                try {
                    a.getImage().setContenu(Tools.getReducedImage(a.getImage().getContenu(), maxSize, maxSize));
                } catch (VinylManagerException e) {
                    e.printStackTrace();
                }
            }
            
            if (a.getOtherImages() != null) {
                for (Image i : a.getOtherImages()) {
                    if (i != null && i.getContenu() != null) {
                        try {
                            i.setContenu(Tools.getReducedImage(i.getContenu(), maxSize, maxSize));
                        } catch (VinylManagerException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}
