package com.sleekbyte.tailor.output;

import com.sleekbyte.tailor.common.Location;
import com.sleekbyte.tailor.common.Messages;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

/**
 * Generates and outputs formatted analysis messages for Xcode
 */
public class Printer implements AutoCloseable {

    private File inputFile;
    private Set<String> msgBuffer = new HashSet<>();

    public Printer(File inputFile) {
        this.inputFile = inputFile;
    }

    /**
     * Prints warning message
     *
     * @param warningMsg warning message to print
     * @param location   location object containing line and column number for printing
     */
    public void warn(String warningMsg, Location location) {
        print(Messages.WARNING, warningMsg, location);
    }

    /**
     * Prints error message
     *
     * @param errorMsg error message to print
     * @param location location object containing line and column number for printing
     */
    public void error(String errorMsg, Location location) {
        print(Messages.ERROR, errorMsg, location);
    }

    private void print(String classification, String msg, Location location) {
        String outputString = "";
        try {
            String column = (location.column != 0) ? (":" + location.column) : "";
            outputString = this.inputFile.getCanonicalPath() + ":" + location.line + column + ": " +
                    classification + ": " + msg;
        } catch (IOException e) {
            System.err.println("Error in getting canonical path of input file: " + e.getMessage());
        }
        this.msgBuffer.add(outputString);
    }

    // Visible for testing only
    public static String genOutputStringForTest(String filePath, int line, String classification,
                                                String msg) {
        return filePath + ":" + line + ": " + classification + ": " + msg;
    }

    // Visible for testing only
    public static String genOutputStringForTest(String filePath, int line, int column, String classification,
                                                String msg) {
        return filePath + ":" + line + ":" + column + ": " + classification + ": " + msg;
    }

    @Override
    public void close() {
        // TODO: #55: this.msgBuffer.sort(lineNumber1 < lineNumber2, columnNumber1 < columnNumber2);
        this.msgBuffer.forEach(System.out::println);
        this.msgBuffer.clear();
    }

    public class ViolationMessage implements Comparable<ViolationMessage> {
        private final String filePath;
        private final int lineNumber;
        private final int columnNumber;
        private final String classification;
        private final String violationMessage;

        public ViolationMessage(String filePath, int lineNumber, int columnNumber, String classification, String violationMessage) {
            this.filePath = filePath;
            this.lineNumber = lineNumber;
            this.columnNumber = columnNumber;
            this.classification = classification;
            this.violationMessage = violationMessage;
        }

        public int compareTo(ViolationMessage data) {
            return 0;
        }

        // TODO: Implement equals and hashCode (equals to be consistent with compareTo).

        public String toString() {
            return String.format("%s:%d:%d: %s: %s", this.filePath, this.lineNumber, this.columnNumber, this.classification, this.violationMessage);
        }
    }
}

@@ -3130,32 +3130,50 @@
 age;%0A        %7D%0A%0A
+        @Override%0A
         public i
@@ -3185,16 +3185,22 @@
 mpareTo(
+final 
 Violatio
@@ -3212,12 +3212,15 @@
 age 
-data
+message
 ) %7B%0A
@@ -3264,88 +3264,566 @@
     
-// TODO: Implement equals and hashCode (equals to be consistent with compareTo).
+@Override%0A        public boolean equals(Object o) %7B%0A            if (!(o instanceof ViolationMessage)) %7B%0A                return false;%0A            %7D%0A            ViolationMessage message;%0A            message = (ViolationMessage)o;%0A            return this.filePath == message.filePath%0A                    && this.lineNumber == message.lineNumber%0A                    && this.columnNumber == message.lineNumber%0A                    && this.classification == message.classification%0A                    && this.violationMessage == message.violationMessage;%0A        %7D
 %0A%0A  

Implement ViolationMessage::equals()