package com.javarush.task.task39.task3913;

import com.javarush.task.task39.task3913.query.IPQuery;

import java.io.*;
import java.nio.file.Path;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class LogParser implements IPQuery {
    Path logDir;
    MyComparator comparator = new MyComparator();
    List<String> logList = new ArrayList<>();

    public LogParser(Path logdir) {
        logDir = logdir;
        logListCreate();
    }

    private void logListCreate() {
        File [] logs = logDir.toFile().listFiles();
        for (File log : logs) {
            try {
                BufferedReader reader = new BufferedReader(new FileReader(log));
                if (log.getName().endsWith(".log")) {
                    while (reader.ready()) {
                        logList.add(reader.readLine());
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public int getNumberOfUniqueIPs(Date after, Date before) {
        return getUniqueIPs(after,before).size();
    }

    @Override
    public Set<String> getUniqueIPs(Date after, Date before) {
        Set<String> IPs = new HashSet<>();
        for (String logString : logList) {
            String IP = logString.split("\t")[0];
            Date date = getDateFromLog(logString);
            if (comparator.compare(date, after) >= 0 && comparator.compare(date, before)<=0) IPs.add(IP);
        }
        return IPs;
    }

    @Override
    public Set<String> getIPsForUser(String user, Date after, Date before) {
        Set<String> IPs = new HashSet<>();
        for (String logString : logList) {
            String IP = logString.split("\t")[0];
            Date date = getDateFromLog(logString);
            String userName = logString.split("\t")[1];
            if (comparator.compare(date, after) >= 0 && comparator.compare(date, before)<=0 && user.equals(userName)) IPs.add(IP);
        }
        return IPs;
    }

    @Override
    public Set<String> getIPsForEvent(Event event, Date after, Date before) {
        Set<String> IPs = new HashSet<>();
        for (String logString : logList) {
            String IP = logString.split("\t")[0];
            Date date = getDateFromLog(logString);
            String eventString = logString.split("\t")[3];
            if (comparator.compare(date, after) >= 0 && comparator.compare(date, before)<=0 && event.name().equals(eventString.split(" ")[0])) IPs.add(IP);
        }
        return IPs;
    }

    @Override
    public Set<String> getIPsForStatus(Status status, Date after, Date before) {
        Set<String> IPs = new HashSet<>();
        for (String logString : logList) {
            String IP = logString.split("\t")[0];
            Date date = getDateFromLog(logString);
            String stat = logString.split("\t")[5];
            if (comparator.compare(date, after) >= 0 && comparator.compare(date, before)<=0 && status.name().equals(stat)) IPs.add(IP);
        }
        return IPs;
    }


    private static class MyComparator implements Comparator<Date>{
        @Override
        public int compare(Date date, Date anotherDate) {
            if (anotherDate == null) return 0;
            return date.compareTo(anotherDate);
        }
    }

    public Date getDateFromLog(String logString) {
        String stringDate = logString.split("\t")[2];
        SimpleDateFormat format = new SimpleDateFormat();
        format.applyPattern("dd.MM.yyyy hh:mm:ss");
        Date date = null;
        try {
            date = format.parse(stringDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
}