package com.limegroup.gnutella.malware;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Comparator;
import java.util.Scanner;

import org.limewire.collection.SortedList;
import org.limewire.core.api.malware.AntivirusUpdateType;
import org.limewire.logging.Log;
import org.limewire.logging.LogFactory;

public class NFOFile {
    
    private static final Log LOG = LogFactory.getLog(NFOFile.class);
    
    public class Entry implements Comparable <Entry>{
        private final String path;
        private final Integer version;
        private final AntivirusUpdateType type;
        
        public Entry(String path, Integer version, AntivirusUpdateType type) {
            this.path = path;
            this.version = version;
            this.type = type;
        }

        public String getPath() {
            return path;
        }
        
        public int getVersion() {
            return version;
        }
        
        public AntivirusUpdateType getType() {
            return type;
        }

        @Override
        public int compareTo(Entry o) {
            return version.compareTo(o.version);
        }
    }
    
    public static class EntryComparator implements Comparator<Entry> {
        @Override
        public int compare(Entry o1, Entry o2) {
            return o1.compareTo(o2);
        }
    }
    
    private SortedList<Entry> incrementalEntries = new SortedList<Entry>(new EntryComparator());
    private SortedList<Entry> fullEntries = new SortedList<Entry>(new EntryComparator());
    
    public NFOFile(File file) throws FileNotFoundException {
        Scanner scanner = new Scanner(file);
        while(scanner.hasNextLine()) {
            String line = scanner.nextLine();
            if(!line.startsWith("#") && !line.startsWith("!")) {
                String[] words = line.split(":");
                if(words.length == 2) {
                    words[0] = words[0].trim();
                    words[1] = words[1].trim();
                    String name = words[1];
                    int extIndex = name.lastIndexOf('.');
                    if(extIndex > -1) {
                        name = name.substring(0, extIndex);
                        if(name.length() >= 4) {
                            try {
                                int version = Integer.valueOf(name.substring(name.length() - 4));
                                if(words[0].equals("IVDB")) {
                                    incrementalEntries.add(new Entry(words[1], version, AntivirusUpdateType.INCREMENTAL));    
                                } else if(words[0].equals("VDB")) {
                                    fullEntries.add(new Entry(words[1], version, AntivirusUpdateType.FULL));    
                                }
                            } catch (NumberFormatException nfe) {
                                LOG.debug("unable to parse version", nfe);
                            }
                        }
                    }
                }
            }
        }
        scanner.close();        
    }
    
    public SortedList<Entry> getIncrementalEntries() {
        return incrementalEntries;
    }
    
    public SortedList<Entry> getFullEntries() {
        return fullEntries;
    }
}
