package org.xbib.logging;

import java.time.Instant;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.LogRecord;

@SuppressWarnings("serial")
class WrappedExtLogRecord extends ExtLogRecord {

    private static final String LOGGER_CLASS_NAME = java.util.logging.Logger.class.getName();

    private transient final LogRecord logRecord;

    private transient boolean resolved;

    WrappedExtLogRecord(final LogRecord logRecord) {
        super(logRecord.getLevel(), logRecord.getMessage(), LOGGER_CLASS_NAME);
        this.logRecord = logRecord;
    }

    @Override
    public String getLoggerName() {
        return logRecord.getLoggerName();
    }

    @Override
    public void setLoggerName(final String name) {
        super.setLoggerName(name);
        logRecord.setLoggerName(name);
    }

    @Override
    public ResourceBundle getResourceBundle() {
        return logRecord.getResourceBundle();
    }

    @Override
    public void setResourceBundle(final ResourceBundle bundle) {
        super.setResourceBundle(bundle);
        logRecord.setResourceBundle(bundle);
    }

    @Override
    public String getResourceBundleName() {
        return logRecord.getResourceBundleName();
    }

    @Override
    public void setResourceBundleName(final String name) {
        super.setResourceBundleName(name);
        logRecord.setResourceBundleName(name);
    }

    @Override
    public Level getLevel() {
        return logRecord.getLevel();
    }

    @Override
    public void setLevel(final Level level) {
        super.setLevel(level);
        logRecord.setLevel(level);
    }

    @Override
    public long getSequenceNumber() {
        return logRecord.getSequenceNumber();
    }

    @Override
    public void setSequenceNumber(final long seq) {
        super.setSequenceNumber(seq);
        logRecord.setSequenceNumber(seq);
    }

    @Override
    public String getSourceClassName() {
        if (!resolved) {
            resolve();
        }
        return super.getSourceClassName();
    }

    @Override
    public void setSourceClassName(final String sourceClassName) {
        resolved = true;
        super.setSourceClassName(sourceClassName);
        logRecord.setSourceClassName(sourceClassName);
    }

    @Override
    public String getSourceMethodName() {
        if (!resolved) {
            resolve();
        }
        return super.getSourceMethodName();
    }

    @Override
    public void setSourceMethodName(final String sourceMethodName) {
        resolved = true;
        super.setSourceMethodName(sourceMethodName);
        logRecord.setSourceMethodName(sourceMethodName);
    }

    private void resolve() {
        resolved = true;
        final String sourceMethodName = logRecord.getSourceMethodName();
        final String sourceClassName = logRecord.getSourceClassName();
        super.setSourceMethodName(sourceMethodName);
        super.setSourceClassName(sourceClassName);
        final StackTraceElement[] st = new Throwable().getStackTrace();
        for (StackTraceElement element : st) {
            if (element.getClassName().equals(sourceClassName) && element.getMethodName().equals(sourceMethodName)) {
                super.setSourceLineNumber(element.getLineNumber());
                super.setSourceFileName(element.getFileName());
                return;
            }
        }
    }

    @Override
    public int getSourceLineNumber() {
        if (!resolved) {
            resolve();
        }
        return super.getSourceLineNumber();
    }

    @Override
    public void setSourceLineNumber(final int sourceLineNumber) {
        resolved = true;
        super.setSourceLineNumber(sourceLineNumber);
    }

    @Override
    public String getSourceFileName() {
        if (!resolved) {
            resolve();
        }
        return super.getSourceFileName();
    }

    @Override
    public void setSourceFileName(final String sourceFileName) {
        resolved = true;
        super.setSourceFileName(sourceFileName);
    }

    @Override
    public String getMessage() {
        return logRecord.getMessage();
    }

    @Override
    public void setMessage(final String message) {
        super.setMessage(message);
        logRecord.setMessage(message);
    }

    @Override
    public void setMessage(String message, FormatStyle formatStyle) {
        super.setMessage(message, formatStyle);
        logRecord.setMessage(message);
    }

    @Override
    public Object[] getParameters() {
        return logRecord.getParameters();
    }

    @Override
    public void setParameters(final Object[] parameters) {
        logRecord.setParameters(parameters);
    }

    @Deprecated
    @SuppressWarnings("deprecation")
    @Override
    public int getThreadID() {
        return logRecord.getThreadID();
    }

    @Deprecated
    @SuppressWarnings("deprecation")
    @Override
    public void setThreadID(final int threadID) {
        super.setThreadID(threadID);
        logRecord.setThreadID(threadID);
    }

    @Override
    public long getLongThreadID() {
        return logRecord.getLongThreadID();
    }

    @Override
    public ExtLogRecord setLongThreadID(final long id) {
        super.setLongThreadID(id);
        logRecord.setLongThreadID(id);
        return this;
    }

    @Override
    public long getMillis() {
        return logRecord.getMillis();
    }

    @Deprecated
    @SuppressWarnings("deprecation")
    @Override
    public void setMillis(final long millis) {
        logRecord.setMillis(millis);
    }

    @Override
    public Instant getInstant() {
        return logRecord.getInstant();
    }

    @Override
    public void setInstant(Instant instant) {
        logRecord.setInstant(instant);
    }

    @Override
    public Throwable getThrown() {
        return logRecord.getThrown();
    }

    @Override
    public void setThrown(final Throwable thrown) {
        logRecord.setThrown(thrown);
    }
}
