import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:styled_widget/styled_widget.dart';

import '../providers/api.dart';

class ApiImage extends HookConsumerWidget {
  final String path;

  const ApiImage(this.path, {super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final future = useMemoized(() async {
      final client = ref.read(apiClientProvider);
      final res = await client.get(path); // GET /avatar 或 /captcha
      return res.bodyBytes;
    }, [path]);

    final snapshot = useFuture(future);

    if (snapshot.connectionState != ConnectionState.done) {
      return const CircularProgressIndicator();
    }

    if (snapshot.hasError) {
      return const Icon(Icons.error);
    }

    return Image.memory(
      snapshot.data!,
      gaplessPlayback: true, // 避免闪烁
    );
  }
}

ApiImage captchaImage() => ApiImage('misc.php?mod=seccode');