/*
 * Copyright (C) 2019-2022 LitterBox contributors
 *
 * This file is part of LitterBox.
 *
 * LitterBox is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 *
 * LitterBox 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LitterBox. If not, see <http://www.gnu.org/licenses/>.
 */
package de.uni_passau.fim.se2.litterbox.analytics.smells;

import de.uni_passau.fim.se2.litterbox.analytics.AbstractIssueFinder;
import de.uni_passau.fim.se2.litterbox.analytics.Issue;
import de.uni_passau.fim.se2.litterbox.analytics.IssueSeverity;
import de.uni_passau.fim.se2.litterbox.analytics.IssueType;
import de.uni_passau.fim.se2.litterbox.ast.model.ActorDefinition;
import de.uni_passau.fim.se2.litterbox.ast.model.Program;
import de.uni_passau.fim.se2.litterbox.ast.model.Script;
import de.uni_passau.fim.se2.litterbox.ast.model.StmtList;
import de.uni_passau.fim.se2.litterbox.ast.model.identifier.StrId;
import de.uni_passau.fim.se2.litterbox.ast.model.procedure.ProcedureDefinition;
import de.uni_passau.fim.se2.litterbox.ast.parser.symboltable.ArgumentInfo;

import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * The parameter names in custom blocks do not have to be unique.
 * Therefore, when two parameters have the same name, no matter the type or which one is used inside the custom
 * block, it will always be evaluated as the last input to the block. The ambiguous parameter is not used though.
 */
public class AmbiguousParameterNameUnused extends AbstractIssueFinder {
    public static final String NAME = "ambiguous_parameter_name_unused";

    private boolean inStmtList = false;
    private boolean foundAmbiguousParam = false;
    private boolean used = false;
    private List<String> paraNames = new LinkedList<>();

    @Override
    public void visit(Script node) {
        //NOP should not be detected in Scripts
    }

    @Override
    public Set<Issue> check(Program program) {
        foundAmbiguousParam = false;
        return super.check(program);
    }

    private void checkArguments(ArgumentInfo[] arguments) {
        paraNames = new LinkedList<>();

        for (int i = 0; i < arguments.length; i++) {
            ArgumentInfo current = arguments[i];
            for (int j = i + 1; j < arguments.length; j++) {
                if (i != j && current.getName().equals(arguments[j].getName())) {
                    if (!paraNames.contains(current.getName())) {
                        paraNames.add(current.getName());
                    }
                    foundAmbiguousParam = true;
                }
            }
        }
    }

    @Override
    public void visit(ActorDefinition actor) {
        super.visit(actor);

        if (foundAmbiguousParam) {
            foundAmbiguousParam = false;
        }
    }

    @Override
    public void visit(StmtList node) {
        inStmtList = true;
        visitChildren(node);
        inStmtList = false;
    }

    @Override
    public void visit(ProcedureDefinition node) {
        currentProcedure = node;
        checkArguments(procMap.get(node.getIdent()).getArguments());

        visitChildren(node);

        if (!used && foundAmbiguousParam) {
            addIssue(node, node.getMetadata().getDefinition(), IssueSeverity.MEDIUM);
        }

        // TODO: This handling with used/found seems really error prone
        used = false;
        foundAmbiguousParam = false;
        paraNames.clear();
        currentProcedure = null;
    }

    @Override
    public void visit(StrId node) {
        if (inStmtList && foundAmbiguousParam && paraNames.contains(node.getName())) {
            used = true;
        }
        visitChildren(node);
    }

    @Override
    public String getName() {
        return NAME;
    }

    @Override
    public IssueType getIssueType() {
        return IssueType.SMELL;
    }
}
