/*
 * Copyright 2021 DiffPlug
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.diffplug.spotless.maven.incremental;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Base64;
import java.util.Objects;

import org.apache.maven.model.Plugin;
import org.apache.maven.project.MavenProject;

import com.diffplug.spotless.Formatter;

class PluginFingerprint {

	private static final String SPOTLESS_PLUGIN_KEY = "com.diffplug.spotless:spotless-maven-plugin";

	private final String value;

	private PluginFingerprint(String value) {
		this.value = value;
	}

	static PluginFingerprint from(MavenProject project, Iterable<Formatter> formatters) {
		Plugin spotlessPlugin = project.getPlugin(SPOTLESS_PLUGIN_KEY);
		byte[] digest = digest(spotlessPlugin, formatters);
		String value = Base64.getEncoder().encodeToString(digest);
		return new PluginFingerprint(value);
	}

	static PluginFingerprint from(String value) {
		return new PluginFingerprint(value);
	}

	static PluginFingerprint empty() {
		return new PluginFingerprint("");
	}

	String value() {
		return value;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (o == null || getClass() != o.getClass()) {
			return false;
		}
		PluginFingerprint that = (PluginFingerprint) o;
		return value.equals(that.value);
	}

	@Override
	public int hashCode() {
		return Objects.hash(value);
	}

	@Override
	public String toString() {
		return "PluginFingerprint[" + value + "]";
	}

	private static byte[] digest(Plugin plugin, Iterable<Formatter> formatters) {
		try (ObjectDigestOutputStream out = ObjectDigestOutputStream.create()) {
			out.writeObject(plugin);
			for (Formatter formatter : formatters) {
				out.writeObject(formatter);
			}
			out.flush();
			return out.digest();
		} catch (IOException e) {
			throw new UncheckedIOException("Unable to serialize plugin " + plugin, e);
		}
	}
}

@@ -13,16 +13,21 @@
 ght 2021
+-2022
  DiffPlu
@@ -28,16 +28,16 @@
 iffPlug%0A
-
  *%0A * Li
@@ -697,32 +697,60 @@
 kedIOException;%0A
+import java.util.ArrayList;%0A
 import java.util
@@ -758,16 +758,39 @@
 Base64;%0A
+import java.util.List;%0A
 import j
@@ -808,16 +808,58 @@
 jects;%0A%0A
+import org.apache.maven.model.Dependency;%0A
 import o
@@ -2197,16 +2197,328 @@
 ters) %7B%0A
+%09%09// dependencies can be an unserializable org.apache.maven.model.merge.ModelMerger$MergingList%0A%09%09// replace it with a serializable ArrayList%0A%09%09List%3CDependency%3E dependencies = null;%0A%09%09if (plugin != null) %7B%0A%09%09%09dependencies = plugin.getDependencies();%0A%09%09%09plugin.setDependencies(new ArrayList%3C%3E(dependencies));%0A%09%09%7D%0A
 %09%09try (O
@@ -2761,16 +2761,16 @@
 on e) %7B%0A
-
 %09%09%09throw
@@ -2839,16 +2839,132 @@
 in, e);%0A
+%09%09%7D finally %7B%0A%09%09%09// reset the original list%0A%09%09%09if (plugin != null) %7B%0A%09%09%09%09plugin.setDependencies(dependencies);%0A%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%7D

Fix PluginFingerprint for plugin with dependencies