// iuiautomationselectionpattern2.dart

// THIS FILE IS GENERATED AUTOMATICALLY AND SHOULD NOT BE EDITED DIRECTLY.

// ignore_for_file: unused_import
// ignore_for_file: constant_identifier_names, non_constant_identifier_names
// ignore_for_file: no_leading_underscores_for_local_identifiers

import 'dart:ffi';

import 'package:ffi/ffi.dart';

import '../callbacks.dart';
import '../combase.dart';
import '../constants.dart';
import '../exceptions.dart';
import '../guid.dart';
import '../macros.dart';
import '../propertykey.dart';
import '../structs.g.dart';
import '../utils.dart';
import '../variant.dart';
import '../win32/ole32.g.dart';
import 'iuiautomationselectionpattern.dart';
import 'iunknown.dart';

/// @nodoc
const IID_IUIAutomationSelectionPattern2 =
    '{0532bfae-c011-4e32-a343-6d642d798555}';

/// Extends the IUIAutomationSelectionPattern interface to provide
/// information about selected items.
///
/// {@category com}
class IUIAutomationSelectionPattern2 extends IUIAutomationSelectionPattern {
  // vtable begins at 9, is 8 entries long.
  IUIAutomationSelectionPattern2(super.ptr);

  factory IUIAutomationSelectionPattern2.from(IUnknown interface) =>
      IUIAutomationSelectionPattern2(
        interface.toInterface(IID_IUIAutomationSelectionPattern2),
      );

  Pointer<COMObject> get currentFirstSelectedItem {
    final retValuePtr = calloc<COMObject>();

    final hr = (ptr.ref.vtable + 9)
        .cast<
          Pointer<
            NativeFunction<Int32 Function(Pointer, Pointer<COMObject> retVal)>
          >
        >()
        .value
        .asFunction<
          int Function(Pointer, Pointer<COMObject> retVal)
        >()(ptr.ref.lpVtbl, retValuePtr);

    if (FAILED(hr)) {
      free(retValuePtr);
      throw WindowsException(hr);
    }

    return retValuePtr;
  }

  Pointer<COMObject> get currentLastSelectedItem {
    final retValuePtr = calloc<COMObject>();

    final hr = (ptr.ref.vtable + 10)
        .cast<
          Pointer<
            NativeFunction<Int32 Function(Pointer, Pointer<COMObject> retVal)>
          >
        >()
        .value
        .asFunction<
          int Function(Pointer, Pointer<COMObject> retVal)
        >()(ptr.ref.lpVtbl, retValuePtr);

    if (FAILED(hr)) {
      free(retValuePtr);
      throw WindowsException(hr);
    }

    return retValuePtr;
  }

  Pointer<COMObject> get currentCurrentSelectedItem {
    final retValuePtr = calloc<COMObject>();

    final hr = (ptr.ref.vtable + 11)
        .cast<
          Pointer<
            NativeFunction<Int32 Function(Pointer, Pointer<COMObject> retVal)>
          >
        >()
        .value
        .asFunction<
          int Function(Pointer, Pointer<COMObject> retVal)
        >()(ptr.ref.lpVtbl, retValuePtr);

    if (FAILED(hr)) {
      free(retValuePtr);
      throw WindowsException(hr);
    }

    return retValuePtr;
  }

  int get currentItemCount {
    final retValuePtr = calloc<Int32>();

    try {
      final hr =
          (ptr.ref.vtable + 12)
              .cast<
                Pointer<
                  NativeFunction<Int32 Function(Pointer, Pointer<Int32> retVal)>
                >
              >()
              .value
              .asFunction<int Function(Pointer, Pointer<Int32> retVal)>()(
            ptr.ref.lpVtbl,
            retValuePtr,
          );

      if (FAILED(hr)) throw WindowsException(hr);

      final retValue = retValuePtr.value;
      return retValue;
    } finally {
      free(retValuePtr);
    }
  }

  Pointer<COMObject> get cachedFirstSelectedItem {
    final retValuePtr = calloc<COMObject>();

    final hr = (ptr.ref.vtable + 13)
        .cast<
          Pointer<
            NativeFunction<Int32 Function(Pointer, Pointer<COMObject> retVal)>
          >
        >()
        .value
        .asFunction<
          int Function(Pointer, Pointer<COMObject> retVal)
        >()(ptr.ref.lpVtbl, retValuePtr);

    if (FAILED(hr)) {
      free(retValuePtr);
      throw WindowsException(hr);
    }

    return retValuePtr;
  }

  Pointer<COMObject> get cachedLastSelectedItem {
    final retValuePtr = calloc<COMObject>();

    final hr = (ptr.ref.vtable + 14)
        .cast<
          Pointer<
            NativeFunction<Int32 Function(Pointer, Pointer<COMObject> retVal)>
          >
        >()
        .value
        .asFunction<
          int Function(Pointer, Pointer<COMObject> retVal)
        >()(ptr.ref.lpVtbl, retValuePtr);

    if (FAILED(hr)) {
      free(retValuePtr);
      throw WindowsException(hr);
    }

    return retValuePtr;
  }

  Pointer<COMObject> get cachedCurrentSelectedItem {
    final retValuePtr = calloc<COMObject>();

    final hr = (ptr.ref.vtable + 15)
        .cast<
          Pointer<
            NativeFunction<Int32 Function(Pointer, Pointer<COMObject> retVal)>
          >
        >()
        .value
        .asFunction<
          int Function(Pointer, Pointer<COMObject> retVal)
        >()(ptr.ref.lpVtbl, retValuePtr);

    if (FAILED(hr)) {
      free(retValuePtr);
      throw WindowsException(hr);
    }

    return retValuePtr;
  }

  int get cachedItemCount {
    final retValuePtr = calloc<Int32>();

    try {
      final hr =
          (ptr.ref.vtable + 16)
              .cast<
                Pointer<
                  NativeFunction<Int32 Function(Pointer, Pointer<Int32> retVal)>
                >
              >()
              .value
              .asFunction<int Function(Pointer, Pointer<Int32> retVal)>()(
            ptr.ref.lpVtbl,
            retValuePtr,
          );

      if (FAILED(hr)) throw WindowsException(hr);

      final retValue = retValuePtr.value;
      return retValue;
    } finally {
      free(retValuePtr);
    }
  }
}
