/*
 * Copyright (c) 2023 - 2024. KeyWare.Co.Ltd All rights reserved.
 * 项目名称：C++ 信息安全性设计准则
 * 项目描述：用于检查C++源代码的安全性设计准则的Sonarqube插件
 * 版权说明：本软件属北京关键科技股份有限公司所有，在未获得北京关键科技股份有限公司正式授权情况下，任何企业和个人，不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。
 */
package com.keyware.sonar.cxx.rules;

import org.sonar.api.SonarEdition;
import org.sonar.api.SonarProduct;
import org.sonar.api.SonarQubeSide;
import org.sonar.api.SonarRuntime;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.resources.Language;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.api.utils.Version;
import org.sonar.plugins.cxx.CustomCxxRulesDefinition;
import org.sonar.plugins.cxx.CxxLanguage;

import java.util.Collections;
import java.util.Objects;
import java.util.Set;

/**
 * TODO CxxSecurityDesignRulesRepository
 *
 * @author GuoXin
 * @date 2024/1/6
 */
public class CxxSecurityDesignRulesRepository extends CustomCxxRulesDefinition {
    // 不要修改这个值，因为路径在 CheckVerifier 中是硬编码的
    private static final String RESOURCE_BASE_PATH = "org/sonar/l10n/java/rules/java";

    public static final String REPOSITORY_KEY = "keyware-cxx-security-design";
    public static final String REPOSITORY_NAME = "C++ 安全性设计准则";


    // 添加需要视为模板规则的规则的规则键
    private static final Set<String> RULE_TEMPLATES_KEY = Collections.emptySet();

    private final SonarRuntime runtime;

    public CxxSecurityDesignRulesRepository(SonarRuntime runtime) {
        this.runtime = runtime;
    }

    @Override
    public void define(RulesDefinition.Context context) {
        RulesDefinition.NewRepository repository = context.createRepository(REPOSITORY_KEY, "cxx").setName(REPOSITORY_NAME);



        setTemplates(repository);

        repository.done();
    }

    @Override
    public Language getLanguage() {
        return new CxxLanguage(new MapSettings().asConfig());
    }

    @Override
    public String repositoryName() {
        return REPOSITORY_NAME;
    }

    @Override
    public String repositoryKey() {
        return REPOSITORY_KEY;
    }

    @Override
    public Class[] checkClasses() {
        return new Class[0];
    }

    private static void setTemplates(RulesDefinition.NewRepository repository) {
        RULE_TEMPLATES_KEY.stream()
                .map(repository::rule)
                .filter(Objects::nonNull)
                .forEach(rule -> rule.setTemplate(true));
    }


    public static class MockedSonarRuntime implements SonarRuntime {

        @Override
        public Version getApiVersion() {
            return Version.create(9, 9);
        }

        @Override
        public SonarProduct getProduct() {
            return SonarProduct.SONARQUBE;
        }

        @Override
        public SonarQubeSide getSonarQubeSide() {
            return SonarQubeSide.SCANNER;
        }

        @Override
        public SonarEdition getEdition() {
            return SonarEdition.COMMUNITY;
        }
    }
}
