/*
 * Copyright 2025 Google LLC
 *
 * 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 genai.imagegeneration;

// [START googlegenaisdk_imggen_subj_refer_ctrl_refer_with_txt_imgs]

import com.google.genai.Client;
import com.google.genai.types.ControlReferenceConfig;
import com.google.genai.types.ControlReferenceImage;
import com.google.genai.types.EditImageConfig;
import com.google.genai.types.EditImageResponse;
import com.google.genai.types.GeneratedImage;
import com.google.genai.types.Image;
import com.google.genai.types.SubjectReferenceConfig;
import com.google.genai.types.SubjectReferenceImage;
import java.util.List;
import java.util.Optional;

public class ImageGenSubjectReferenceWithTextAndImage {

  public static void main(String[] args) {
    // TODO(developer): Replace these variables before running the sample.
    String modelId = "imagen-3.0-capability-001";
    String outputGcsUri = "gs://your-bucket/your-prefix";
    subjectCustomization(modelId, outputGcsUri);
  }

  // Generates an image using subject customization by adapting a subject reference image
  // with a control reference image and a text prompt.
  public static Optional<String> subjectCustomization(String modelId, String outputGcsUri) {
    // Client Initialization. Once created, it can be reused for multiple requests.
    try (Client client = Client.builder().location("global").vertexAI(true).build()) {
      // Create subject and control reference images of a photograph stored in Google Cloud Storage
      // using https://storage.googleapis.com/cloud-samples-data/generative-ai/image/person.png
      SubjectReferenceImage subjectReferenceImage =
          SubjectReferenceImage.builder()
              .referenceId(1)
              .referenceImage(
                  Image.builder()
                      .gcsUri("gs://cloud-samples-data/generative-ai/image/person.png")
                      .build())
              .config(
                  SubjectReferenceConfig.builder()
                      .subjectDescription("a headshot of a woman")
                      .subjectType("SUBJECT_TYPE_PERSON")
                      .build())
              .build();

      ControlReferenceImage controlReferenceImage =
          ControlReferenceImage.builder()
              .referenceId(2)
              .referenceImage(
                  Image.builder()
                      .gcsUri("gs://cloud-samples-data/generative-ai/image/person.png")
                      .build())
              .config(
                  ControlReferenceConfig.builder().controlType("CONTROL_TYPE_FACE_MESH").build())
              .build();

      // The `[1]` and `[2]` in the prompt refer to the `referenceId` assigned to
      // the subject reference and control reference images.
      EditImageResponse imageResponse =
          client.models.editImage(
              modelId,
              "a portrait of a woman[1] in the pose of the control image[2]in a watercolor style by"
                  + " a professional artist, light and low-contrast stokes, bright pastel colors,"
                  + " a warm atmosphere, clean background, grainy paper, bold visible brushstrokes,"
                  + " patchy details",
              List.of(subjectReferenceImage, controlReferenceImage),
              EditImageConfig.builder()
                  .editMode("EDIT_MODE_DEFAULT")
                  .numberOfImages(1)
                  .safetyFilterLevel("BLOCK_MEDIUM_AND_ABOVE")
                  .personGeneration("ALLOW_ADULT")
                  .outputGcsUri(outputGcsUri)
                  .build());

      Image generatedImage =
          imageResponse
              .generatedImages()
              .flatMap(generatedImages -> generatedImages.stream().findFirst())
              .flatMap(GeneratedImage::image)
              .orElseThrow(() -> new IllegalStateException("No image was generated by the model."));

      generatedImage.gcsUri().ifPresent(System.out::println);
      // Example response:
      // gs://your-bucket/your-prefix

      return generatedImage.gcsUri();
    }
  }
}
// [END googlegenaisdk_imggen_subj_refer_ctrl_refer_with_txt_imgs]
