/* 
 * Diplomová práce ÚSI VUT 
 * Forenzní analýza webového prohlížeče
 *
 * Autor: Michal Bača
 * Datum: 20.5.2015
 */
package yafft.Utils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;

/**
 *
 * @author michal
 */
public class FFProfile {

    private final File profile;
    private String version;
    private int versionInt;

    private File history;
    private File cookies;
    private File downloads;
    private File downloads35;
    private File forms;
    private File passwords;

    public FFProfile(File prof) {
        this.version = null;
        this.profile = prof;
        this.history = new File(prof.getAbsoluteFile() + java.io.File.separator + "places.sqlite");
        this.cookies = new File(prof.getAbsoluteFile() + java.io.File.separator + "cookies.sqlite");
        this.forms = new File(prof.getAbsoluteFile() + java.io.File.separator + "formhistory.sqlite");
        this.passwords = new File(prof.getAbsoluteFile() + java.io.File.separator + "logins.json");

        //get FF version
        if (new File(prof.getAbsoluteFile() + java.io.File.separator + "compatibility.ini").isFile()) {
            try {
                Stream<String> line = Files.lines(Paths.get(prof.getAbsoluteFile() + java.io.File.separator + "compatibility.ini")).filter(f -> f.contains("LastVersion"));

                this.version = line.findFirst().get();
                String[] tmp = this.version.split("[=.,]+");
                this.versionInt = Integer.parseInt(tmp[1]);

            } catch (IOException ex) {
                Logger.getLogger(FFProfile.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            this.versionInt = 00;
            this.version = "Firefox Unknown";        }

        
            this.downloads35 = new File(prof.getAbsoluteFile() + java.io.File.separator + "downloads.json");
            this.downloads = new File(prof.getAbsoluteFile() + java.io.File.separator + "places.sqlite");
            System.out.println("FF version detected: " + this.versionInt + "\nDownloads DB: " + this.downloads35);
      

        //check if file exista
       /* if (!this.history.exists()) {
            this.history = null;

        }

        if (!this.cookies.exists()) {
            this.cookies = null;

        }

         if (!this.downloads.isFile()) {
         this.downloads = new File(prof.getAbsoluteFile() + java.io.File.separator + "places.sqlite");

         }
        
         if (!this.downloads.isFile()) {
         this.downloads = new File(prof.getAbsoluteFile() + java.io.File.separator + "places.sqlite");

         }
        if (!this.forms.exists()) {
            this.forms = null;
        }

        if (!this.passwords.exists()) {
            this.passwords = null;
        }
*/
        //System.out.println(this.history);
        //System.out.println(this.cookies);
    }

    public File getPasswords() {
        return passwords;
    }

    public void setPasswords(File passwords) {
        this.passwords = passwords;
    }

    public File getHistory() {
        return history;
    }

    public File getPlaces() {
        return this.history;
    }

    public File getCookies() {
        return cookies;
    }

    public File getProfile() {
        return profile;
    }

    public String getVersion() {
        return version;
    }

    public int getVersionInt() {
        return versionInt;
    }

    public File getDownloads() {

        return this.downloads;

    }

    public File getDownloads35() {
        return this.downloads35;

    }

    public File getForms() {
        return forms;
    }

    @Override
    /*   public String toString() {
     return "FFProfile{" + "history=" + history + ", cookies=" + cookies + "downloads=" + this.downloads+ '}';
     }*/

    public String toString() {
        return this.profile.getName();

    }

}
