/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.fory.serializer;

import java.util.function.Supplier;
import org.apache.fory.config.Config;
import org.apache.fory.context.CopyContext;
import org.apache.fory.context.ReadContext;
import org.apache.fory.context.WriteContext;
import org.apache.fory.resolver.TypeResolver;

@SuppressWarnings({"rawtypes", "unchecked"})
public class LazySerializer extends Serializer {
  private final Config config;
  private final TypeResolver typeResolver;
  private final Supplier<Serializer> serializerSupplier;
  private Serializer serializer;

  public LazySerializer(
      TypeResolver typeResolver, Class type, Supplier<Serializer> serializerSupplier) {
    super(typeResolver.getConfig(), type);
    this.config = typeResolver.getConfig();
    this.typeResolver = typeResolver;
    this.serializerSupplier = serializerSupplier;
  }

  @Override
  public void write(WriteContext writeContext, Object value) {
    if (serializer == null) {
      serializer = serializerSupplier.get();
      typeResolver.setSerializer(value.getClass(), serializer);
      if (config.isXlang()) {
        typeResolver.getTypeInfo(value.getClass()).setSerializer(serializer);
      }
    }
    serializer.write(writeContext, value);
  }

  @Override
  public Object read(ReadContext readContext) {
    boolean unInit = serializer == null;
    if (unInit) {
      serializer = serializerSupplier.get();
    }
    Object value = serializer.read(readContext);
    if (unInit) {
      typeResolver.setSerializer(value.getClass(), serializer);
      if (config.isXlang()) {
        typeResolver.getTypeInfo(value.getClass()).setSerializer(serializer);
      }
    }
    return value;
  }

  @Override
  public Object copy(CopyContext copyContext, Object value) {
    if (serializer == null) {
      serializer = serializerSupplier.get();
      typeResolver.setSerializer(value.getClass(), serializer);
    }
    return serializer.copy(copyContext, value);
  }

  public static class LazyObjectSerializer extends LazySerializer {
    public LazyObjectSerializer(
        TypeResolver typeResolver, Class type, Supplier<Serializer> serializerSupplier) {
      super(typeResolver, type, serializerSupplier);
    }
  }
}
