/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2026 SteamWar.de-Serverteam
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package de.steamwar;

import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

import java.util.List;

public class ClassTransformer extends ClassVisitor {

    private final String internalName;
    private final List<AccessWidenerEntry> entries;
    private final boolean appendPublicConstructor;

    public ClassTransformer(ClassVisitor cv, String internalName, List<AccessWidenerEntry> entries, boolean appendPublicConstructor) {
        super(Opcodes.ASM9, cv);
        this.internalName = internalName;
        this.entries = entries;
        this.appendPublicConstructor = appendPublicConstructor;
    }

    @Override
    public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
        int newAccess = access;
        for (AccessWidenerEntry e : entries) {
            if (!e.targets(internalName) || !"class".equals(e.memberType())) continue;
            newAccess = applyDirective(e.directive(), newAccess, false);
        }
        if (appendPublicConstructor) {
            MethodVisitor methodVisitor = visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
            methodVisitor.visitCode();
            methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
            methodVisitor.visitMethodInsn(
                    Opcodes.INVOKESPECIAL,
                    "java/lang/Object",
                    "<init>",
                    "()V",
                    false
            );
            methodVisitor.visitInsn(Opcodes.RETURN);
            methodVisitor.visitMaxs(1, 1);
            methodVisitor.visitEnd();
        }
        super.visit(version, newAccess, name, signature, superName, interfaces);
    }

    @Override
    public void visitInnerClass(String name, String outerName, String innerName, int access) {
        int newAccess = access;
        for (AccessWidenerEntry e : entries) {
            if (!e.target().equals(name) || !"class".equals(e.memberType())) continue;
            newAccess = applyDirective(e.directive(), newAccess, false);
        }
        super.visitInnerClass(name, outerName, innerName, newAccess);
    }

    @Override
    public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
        int newAccess = access;
        for (AccessWidenerEntry e : entries) {
            if (!e.targets(internalName) || !"method".equals(e.memberType())) continue;
            if (!name.equals(e.name()) || !descriptor.equals(e.descriptor())) continue;
            newAccess = applyDirective(e.directive(), newAccess, false);
        }
        return super.visitMethod(newAccess, name, descriptor, signature, exceptions);
    }

    @Override
    public FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value) {
        int newAccess = access;
        for (AccessWidenerEntry e : entries) {
            if (!e.targets(internalName) || !"field".equals(e.memberType())) continue;
            if (!name.equals(e.name())) continue;
            newAccess = applyDirective(e.directive(), newAccess, true);
        }
        return super.visitField(newAccess, name, descriptor, signature, value);
    }

    /**
     * Apply a directive to an access bitmask.
     *
     * @param directive accessible / mutable / extendable (with optional "transitive-" prefix)
     * @param access    current access flags
     * @param isField   true when processing a field (mutable removes final)
     */
    private static int applyDirective(String directive, int access, boolean isField) {
        // Strip transitive- prefix — the widening itself is the same
        String effective = directive.startsWith("transitive-") ? directive.substring("transitive-".length()) : directive;

        return switch (effective) {
            case "accessible" -> makePublic(access);
            case "extendable" -> makePublic(removeFinal(access));
            case "mutable" -> isField ? removeFinal(access) : access;
            default -> access;
        };
    }

    private static int makePublic(int access) {
        return (access & ~(Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED)) | Opcodes.ACC_PUBLIC;
    }

    private static int removeFinal(int access) {
        return access & ~Opcodes.ACC_FINAL;
    }
}
