package Vestel;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {

    private static int startOfTextPool;

    public static class Function {
        public int data1;
        public int data2;
        public int data3;
        public int statusOffset;
        public int status;
        public int nameOffset;
        public String name;
        public String text = "";

        public Function(final ByteBuffer byteArrayWrapper) {
            data1 = byteArrayWrapper.getInt(); // first three ints are some unknown values
            data2 = byteArrayWrapper.getInt();
            data3 = byteArrayWrapper.getInt();
            statusOffset = byteArrayWrapper.position();
            status = byteArrayWrapper.getInt(); // fourth int is the status of the function
            if (status >= startOfTextPool && status <= byteArrayWrapper.limit()) { // sometimes the status is actually a pointer into the text pool
                text = "\"" + extractStringFromBuffer(byteArrayWrapper, status) + "\"";
            } else {
                text = Integer.toString(status) + " " + hex(status); // otherwise it's just printed
            }
            nameOffset = byteArrayWrapper.getInt(); // fifth int is the address of the functions name
            name = extractStringFromBuffer(byteArrayWrapper, nameOffset);
        }

        private static String extractStringFromBuffer(final ByteBuffer byteArrayWrapper, final int offset) {
            if (offset < startOfTextPool || offset > byteArrayWrapper.limit()) {
                return "";
            }
            byteArrayWrapper.mark();
            byteArrayWrapper.position(offset);
            byte b;
            final StringBuilder sb = new StringBuilder();
            while ((b = byteArrayWrapper.get()) != 0) {
                sb.append((char) b);
            }
            byteArrayWrapper.reset();
            return sb.toString();
        }

        private static String hex(final int nr) {
            return "[0x" + Integer.toHexString(nr) + "]";
        }

        @Override
        public String toString() {
            return hex(statusOffset) + " " + name + " = " + text + " - " + hex(data1) + hex(data2) + hex(data3);
        }
    }

    public static void main(final String[] args) throws IOException {
        printFile(Paths.get(args[0]));
    }

    private static void printFile(final Path path) throws IOException {
        System.out.println("Reading file: " + path.toAbsolutePath());
        final byte[] fileContent = Files.readAllBytes(path);
        final ByteBuffer byteArrayWrapper = ByteBuffer.wrap(fileContent);
        byteArrayWrapper.order(ByteOrder.LITTLE_ENDIAN);
        byteArrayWrapper.position(0x24); // address of TextPool
        startOfTextPool = byteArrayWrapper.getInt();
        byteArrayWrapper.position(0x30); // position of first function
        while (byteArrayWrapper.position() < startOfTextPool) {
            System.out.println(new Function(byteArrayWrapper));
        }
    }
}