//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package springfox.documentation.schema;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import springfox.documentation.spi.schema.UniqueTypeNameAdapter;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * 修复swagger bug
 */
public class TypeNameIndexingAdapter implements UniqueTypeNameAdapter {
    private static final Logger LOGGER = LoggerFactory.getLogger ( TypeNameIndexingAdapter.class );
    private final Map <String, String> knownNames = new HashMap ( );

    public TypeNameIndexingAdapter() {
    }

    public Map <String, String> getNames() {
        return Collections.unmodifiableMap ( this.knownNames );
    }

    public Optional <String> getTypeName(String typeId) {
        return Optional.ofNullable ( (String) this.knownNames.get ( typeId ) );
    }

//    private boolean checkTypeRegistration(String typeName, String typeId) {
//        if (this.knownNames.containsKey(typeId)) {
//            if (!((String)this.knownNames.get(typeId)).equals(typeName)) {
//                LOGGER.debug("Rewriting type {} with model id: {} is not allowed, because it is already registered", typeName, typeId);
//                throw new IllegalStateException("Model already registered with different name.");
//            } else {
//                return true;
//            }
//        } else {
//            return false;
//        }
//    }

    private boolean checkTypeRegistration(String typeName, String typeId) {
        return this.knownNames.containsKey ( typeId );
    }

    public void registerType(String typeName, String typeId) {
        if ( !this.checkTypeRegistration ( typeName, typeId ) ) {
            this.knownNames.put ( typeId, typeName );
        }
    }

    public void registerUniqueType(String typeName, String typeId) {
        if ( !this.checkTypeRegistration ( typeName, typeId ) ) {
            Integer nameIndex = 0;

            String tempName;
            for (tempName = typeName; this.knownNames.values ( ).contains ( tempName ); tempName = typeName + "_" + nameIndex) {
                nameIndex = nameIndex + 1;
            }

            this.knownNames.put ( typeId, tempName );
        }
    }

    public void setEqualityFor(String typeIdOf, String typeIdTo) {
        if ( !this.knownNames.containsKey ( typeIdTo ) ) {
            LOGGER.warn ( "Model with model id: {} was not found, because it is not registered", typeIdTo );
            throw new IllegalStateException ( "Model was not found" );
        } else if ( this.knownNames.containsKey ( typeIdOf ) && !((String) this.knownNames.get ( typeIdOf )).equals ( this.knownNames.get ( typeIdTo ) ) ) {
            LOGGER.warn ( "Model with model id: {} already has equality to other model", typeIdTo );
            throw new IllegalStateException ( "Model already has equality to other model" );
        } else {
            this.knownNames.put ( typeIdOf, (String) this.knownNames.get ( typeIdTo ) );
        }
    }
}
