// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Autogenerated from Pigeon (v9.2.5), do not edit directly.
// See also: https://pub.dev/packages/pigeon

package dev.flutter.packages.file_selector_android;

import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import io.flutter.plugin.common.BasicMessageChannel;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MessageCodec;
import io.flutter.plugin.common.StandardMessageCodec;
import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

/** Generated class from Pigeon. */
@SuppressWarnings({"unused", "unchecked", "CodeBlock2Expr", "RedundantSuppression", "serial"})
public class GeneratedFileSelectorApi {

  /** Error class for passing custom error details to Flutter via a thrown PlatformException. */
  public static class FlutterError extends RuntimeException {

    /** The error code. */
    public final String code;

    /** The error details. Must be a datatype supported by the api codec. */
    public final Object details;

    public FlutterError(@NonNull String code, @Nullable String message, @Nullable Object details) {
      super(message);
      this.code = code;
      this.details = details;
    }
  }

  @NonNull
  protected static ArrayList<Object> wrapError(@NonNull Throwable exception) {
    ArrayList<Object> errorList = new ArrayList<Object>(3);
    if (exception instanceof FlutterError) {
      FlutterError error = (FlutterError) exception;
      errorList.add(error.code);
      errorList.add(error.getMessage());
      errorList.add(error.details);
    } else {
      errorList.add(exception.toString());
      errorList.add(exception.getClass().getSimpleName());
      errorList.add(
          "Cause: " + exception.getCause() + ", Stacktrace: " + Log.getStackTraceString(exception));
    }
    return errorList;
  }

  /** Generated class from Pigeon that represents data sent in messages. */
  public static final class FileResponse {
    private @NonNull String path;

    public @NonNull String getPath() {
      return path;
    }

    public void setPath(@NonNull String setterArg) {
      if (setterArg == null) {
        throw new IllegalStateException("Nonnull field \"path\" is null.");
      }
      this.path = setterArg;
    }

    private @Nullable String mimeType;

    public @Nullable String getMimeType() {
      return mimeType;
    }

    public void setMimeType(@Nullable String setterArg) {
      this.mimeType = setterArg;
    }

    private @Nullable String name;

    public @Nullable String getName() {
      return name;
    }

    public void setName(@Nullable String setterArg) {
      this.name = setterArg;
    }

    private @NonNull Long size;

    public @NonNull Long getSize() {
      return size;
    }

    public void setSize(@NonNull Long setterArg) {
      if (setterArg == null) {
        throw new IllegalStateException("Nonnull field \"size\" is null.");
      }
      this.size = setterArg;
    }

    private @NonNull byte[] bytes;

    public @NonNull byte[] getBytes() {
      return bytes;
    }

    public void setBytes(@NonNull byte[] setterArg) {
      if (setterArg == null) {
        throw new IllegalStateException("Nonnull field \"bytes\" is null.");
      }
      this.bytes = setterArg;
    }

    /** Constructor is non-public to enforce null safety; use Builder. */
    FileResponse() {}

    public static final class Builder {

      private @Nullable String path;

      public @NonNull Builder setPath(@NonNull String setterArg) {
        this.path = setterArg;
        return this;
      }

      private @Nullable String mimeType;

      public @NonNull Builder setMimeType(@Nullable String setterArg) {
        this.mimeType = setterArg;
        return this;
      }

      private @Nullable String name;

      public @NonNull Builder setName(@Nullable String setterArg) {
        this.name = setterArg;
        return this;
      }

      private @Nullable Long size;

      public @NonNull Builder setSize(@NonNull Long setterArg) {
        this.size = setterArg;
        return this;
      }

      private @Nullable byte[] bytes;

      public @NonNull Builder setBytes(@NonNull byte[] setterArg) {
        this.bytes = setterArg;
        return this;
      }

      public @NonNull FileResponse build() {
        FileResponse pigeonReturn = new FileResponse();
        pigeonReturn.setPath(path);
        pigeonReturn.setMimeType(mimeType);
        pigeonReturn.setName(name);
        pigeonReturn.setSize(size);
        pigeonReturn.setBytes(bytes);
        return pigeonReturn;
      }
    }

    @NonNull
    ArrayList<Object> toList() {
      ArrayList<Object> toListResult = new ArrayList<Object>(5);
      toListResult.add(path);
      toListResult.add(mimeType);
      toListResult.add(name);
      toListResult.add(size);
      toListResult.add(bytes);
      return toListResult;
    }

    static @NonNull FileResponse fromList(@NonNull ArrayList<Object> list) {
      FileResponse pigeonResult = new FileResponse();
      Object path = list.get(0);
      pigeonResult.setPath((String) path);
      Object mimeType = list.get(1);
      pigeonResult.setMimeType((String) mimeType);
      Object name = list.get(2);
      pigeonResult.setName((String) name);
      Object size = list.get(3);
      pigeonResult.setSize(
          (size == null) ? null : ((size instanceof Integer) ? (Integer) size : (Long) size));
      Object bytes = list.get(4);
      pigeonResult.setBytes((byte[]) bytes);
      return pigeonResult;
    }
  }

  /** Generated class from Pigeon that represents data sent in messages. */
  public static final class FileTypes {
    private @NonNull List<String> mimeTypes;

    public @NonNull List<String> getMimeTypes() {
      return mimeTypes;
    }

    public void setMimeTypes(@NonNull List<String> setterArg) {
      if (setterArg == null) {
        throw new IllegalStateException("Nonnull field \"mimeTypes\" is null.");
      }
      this.mimeTypes = setterArg;
    }

    private @NonNull List<String> extensions;

    public @NonNull List<String> getExtensions() {
      return extensions;
    }

    public void setExtensions(@NonNull List<String> setterArg) {
      if (setterArg == null) {
        throw new IllegalStateException("Nonnull field \"extensions\" is null.");
      }
      this.extensions = setterArg;
    }

    /** Constructor is non-public to enforce null safety; use Builder. */
    FileTypes() {}

    public static final class Builder {

      private @Nullable List<String> mimeTypes;

      public @NonNull Builder setMimeTypes(@NonNull List<String> setterArg) {
        this.mimeTypes = setterArg;
        return this;
      }

      private @Nullable List<String> extensions;

      public @NonNull Builder setExtensions(@NonNull List<String> setterArg) {
        this.extensions = setterArg;
        return this;
      }

      public @NonNull FileTypes build() {
        FileTypes pigeonReturn = new FileTypes();
        pigeonReturn.setMimeTypes(mimeTypes);
        pigeonReturn.setExtensions(extensions);
        return pigeonReturn;
      }
    }

    @NonNull
    ArrayList<Object> toList() {
      ArrayList<Object> toListResult = new ArrayList<Object>(2);
      toListResult.add(mimeTypes);
      toListResult.add(extensions);
      return toListResult;
    }

    static @NonNull FileTypes fromList(@NonNull ArrayList<Object> list) {
      FileTypes pigeonResult = new FileTypes();
      Object mimeTypes = list.get(0);
      pigeonResult.setMimeTypes((List<String>) mimeTypes);
      Object extensions = list.get(1);
      pigeonResult.setExtensions((List<String>) extensions);
      return pigeonResult;
    }
  }

  public interface Result<T> {
    @SuppressWarnings("UnknownNullness")
    void success(T result);

    void error(@NonNull Throwable error);
  }

  private static class FileSelectorApiCodec extends StandardMessageCodec {
    public static final FileSelectorApiCodec INSTANCE = new FileSelectorApiCodec();

    private FileSelectorApiCodec() {}

    @Override
    protected Object readValueOfType(byte type, @NonNull ByteBuffer buffer) {
      switch (type) {
        case (byte) 128:
          return FileResponse.fromList((ArrayList<Object>) readValue(buffer));
        case (byte) 129:
          return FileTypes.fromList((ArrayList<Object>) readValue(buffer));
        default:
          return super.readValueOfType(type, buffer);
      }
    }

    @Override
    protected void writeValue(@NonNull ByteArrayOutputStream stream, Object value) {
      if (value instanceof FileResponse) {
        stream.write(128);
        writeValue(stream, ((FileResponse) value).toList());
      } else if (value instanceof FileTypes) {
        stream.write(129);
        writeValue(stream, ((FileTypes) value).toList());
      } else {
        super.writeValue(stream, value);
      }
    }
  }

  /**
   * An API to call to native code to select files or directories.
   *
   * <p>Generated interface from Pigeon that represents a handler of messages from Flutter.
   */
  public interface FileSelectorApi {
    /**
     * Opens a file dialog for loading files and returns a file path.
     *
     * <p>Returns `null` if user cancels the operation.
     */
    void openFile(
        @Nullable String initialDirectory,
        @NonNull FileTypes allowedTypes,
        @NonNull Result<FileResponse> result);
    /**
     * Opens a file dialog for loading files and returns a list of file responses chosen by the
     * user.
     */
    void openFiles(
        @Nullable String initialDirectory,
        @NonNull FileTypes allowedTypes,
        @NonNull Result<List<FileResponse>> result);
    /**
     * Opens a file dialog for loading directories and returns a directory path.
     *
     * <p>Returns `null` if user cancels the operation.
     */
    void getDirectoryPath(@Nullable String initialDirectory, @NonNull Result<String> result);

    /** The codec used by FileSelectorApi. */
    static @NonNull MessageCodec<Object> getCodec() {
      return FileSelectorApiCodec.INSTANCE;
    }
    /**
     * Sets up an instance of `FileSelectorApi` to handle messages through the `binaryMessenger`.
     */
    static void setup(@NonNull BinaryMessenger binaryMessenger, @Nullable FileSelectorApi api) {
      {
        BasicMessageChannel<Object> channel =
            new BasicMessageChannel<>(
                binaryMessenger, "dev.flutter.pigeon.FileSelectorApi.openFile", getCodec());
        if (api != null) {
          channel.setMessageHandler(
              (message, reply) -> {
                ArrayList<Object> wrapped = new ArrayList<Object>();
                ArrayList<Object> args = (ArrayList<Object>) message;
                String initialDirectoryArg = (String) args.get(0);
                FileTypes allowedTypesArg = (FileTypes) args.get(1);
                Result<FileResponse> resultCallback =
                    new Result<FileResponse>() {
                      public void success(FileResponse result) {
                        wrapped.add(0, result);
                        reply.reply(wrapped);
                      }

                      public void error(Throwable error) {
                        ArrayList<Object> wrappedError = wrapError(error);
                        reply.reply(wrappedError);
                      }
                    };

                api.openFile(initialDirectoryArg, allowedTypesArg, resultCallback);
              });
        } else {
          channel.setMessageHandler(null);
        }
      }
      {
        BasicMessageChannel<Object> channel =
            new BasicMessageChannel<>(
                binaryMessenger, "dev.flutter.pigeon.FileSelectorApi.openFiles", getCodec());
        if (api != null) {
          channel.setMessageHandler(
              (message, reply) -> {
                ArrayList<Object> wrapped = new ArrayList<Object>();
                ArrayList<Object> args = (ArrayList<Object>) message;
                String initialDirectoryArg = (String) args.get(0);
                FileTypes allowedTypesArg = (FileTypes) args.get(1);
                Result<List<FileResponse>> resultCallback =
                    new Result<List<FileResponse>>() {
                      public void success(List<FileResponse> result) {
                        wrapped.add(0, result);
                        reply.reply(wrapped);
                      }

                      public void error(Throwable error) {
                        ArrayList<Object> wrappedError = wrapError(error);
                        reply.reply(wrappedError);
                      }
                    };

                api.openFiles(initialDirectoryArg, allowedTypesArg, resultCallback);
              });
        } else {
          channel.setMessageHandler(null);
        }
      }
      {
        BasicMessageChannel<Object> channel =
            new BasicMessageChannel<>(
                binaryMessenger, "dev.flutter.pigeon.FileSelectorApi.getDirectoryPath", getCodec());
        if (api != null) {
          channel.setMessageHandler(
              (message, reply) -> {
                ArrayList<Object> wrapped = new ArrayList<Object>();
                ArrayList<Object> args = (ArrayList<Object>) message;
                String initialDirectoryArg = (String) args.get(0);
                Result<String> resultCallback =
                    new Result<String>() {
                      public void success(String result) {
                        wrapped.add(0, result);
                        reply.reply(wrapped);
                      }

                      public void error(Throwable error) {
                        ArrayList<Object> wrappedError = wrapError(error);
                        reply.reply(wrappedError);
                      }
                    };

                api.getDirectoryPath(initialDirectoryArg, resultCallback);
              });
        } else {
          channel.setMessageHandler(null);
        }
      }
    }
  }
}
