/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * 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.facebook.litho.specmodels.model;

import com.facebook.litho.specmodels.internal.ImmutableList;
import com.squareup.javapoet.TypeName;
import java.util.Objects;
import java.util.function.Supplier;

/**
 * This class represents a partial representation of the {@link javax.lang.model.type.TypeMirror}
 * class structure generated by the Processor.
 */
public class TypeSpec {

  private final TypeName mTypeName;
  private final boolean mIsValid;

  public TypeSpec(TypeName typeName) {
    this(typeName, true);
  }

  TypeSpec(TypeName typeName, boolean isValid) {
    mTypeName = typeName;
    mIsValid = isValid;
  }

  public TypeName getTypeName() {
    return mTypeName;
  }

  /**
   * Indicate whether or not this is a fully qualified type, i.e. whether full type information was
   * available at the time the annotation processor or a similar process ran.
   */
  public boolean isValid() {
    return mIsValid;
  }

  /**
   * The comparison will be performed only if the TypeSpec is a DeclaredTypeSpec. A type is
   * considered subType of itself.
   */
  public boolean isSubType(TypeName type) {
    return false;
  }

  public boolean isSubInterface(TypeName interfaceTypeName) {
    return false;
  }

  /** The comparison will be performed only if the TypeSpec is a DeclaredTypeSpec. */
  public boolean isSameDeclaredType(TypeName type) {
    return false;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    final TypeSpec typeSpec = (TypeSpec) o;
    return mIsValid == typeSpec.mIsValid && Objects.equals(mTypeName, typeSpec.mTypeName);
  }

  @Override
  public int hashCode() {
    return Objects.hash(mTypeName, mIsValid);
  }

  public static class DeclaredTypeSpec extends TypeSpec {
    private final String mQualifiedName;
    private final Supplier<TypeSpec> mSuperclass;
    private final Supplier<ImmutableList<TypeSpec>> mSuperinterfaces;
    private final Supplier<ImmutableList<TypeSpec>> mTypeArguments;

    public DeclaredTypeSpec(
        TypeName typeName,
        String qualifiedName,
        Supplier<TypeSpec> superclass,
        Supplier<ImmutableList<TypeSpec>> superinterfaces,
        Supplier<ImmutableList<TypeSpec>> typeArguments) {
      super(typeName);
      mQualifiedName = qualifiedName;
      mSuperclass = superclass;
      mSuperinterfaces = superinterfaces;
      mTypeArguments = typeArguments;
    }

    @Override
    public boolean isSubType(TypeName type) {
      return type.toString().equals(mQualifiedName)
          || (mSuperclass.get() != null && mSuperclass.get().isSubType(type));
    }

    @Override
    public boolean isSubInterface(TypeName type) {
      return type.toString().equals(mQualifiedName)
          || mSuperinterfaces.get().stream().anyMatch(typeSpec -> typeSpec.isSubInterface(type));
    }

    @Override
    public boolean isSameDeclaredType(TypeName type) {
      return type.toString().equals(mQualifiedName);
    }

    public ImmutableList<TypeSpec> getTypeArguments() {
      return mTypeArguments.get();
    }
  }
}
