import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:momo/models/image_list_resp.dart';
import 'package:momo/models/image_resp.dart';
import 'package:momo/request/http_client.dart';

class Gallery extends ConsumerStatefulWidget {
  const Gallery({Key? key}) : super(key: key);

  @override
  ConsumerState<Gallery> createState() => _GalleryState();
}

List<ImageResp> imageList = [];

class _GalleryState extends ConsumerState<Gallery> {
  Future<dynamic> loadImages() async {
    var resp = await dio.get("/image/history");
    if (mounted) {
      setState(() {
        imageList = ImageListResp.fromJson(resp.data).list;
      });
    }
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    loadImages();
  }

  @override
  Widget build(BuildContext context) {
    return GridView.builder(
        itemCount: imageList.length,
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: MediaQuery.of(context).size.width > 640 ? 5 : 3),
        itemBuilder: (BuildContext context, int index) {
          return InkWell(
            onTap: () {
              context.go("/detail");
            },
            child: Image.network(
              "${dio.options.baseUrl}/image/thumbnail/${imageList[index].file_path}",
              fit: BoxFit.cover,
            ),
          );
        });
  }
}
