import 'package:dio/dio.dart';
import 'package:extended_image/extended_image.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:momo/models/image_resp.dart';
import 'package:momo/provider/rerender.dart';
import 'package:momo/request/http_client.dart';

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

  @override
  ConsumerState<ImageDetail> createState() => _ImageDetailState();
}

class _ImageDetailState extends ConsumerState<ImageDetail> {
  Future<ImageResp?> loadImage(id) async {
    try {
      var resp = await dio.get("/image/detail/$id");
      return ImageResp.fromJson(resp.data);
    } catch (e) {
      if (e is DioError) {
        if (kDebugMode) {
          print(e.response?.data);
        }
      }
    }
    return null;
  }

// double dx=0;
  double _scale = 1;

  @override
  Widget build(BuildContext context) {
    String id = GoRouterState.of(context).queryParams["id"] ?? "";
    return Container(
      width: double.infinity,
      height: double.infinity,
      color: Colors.white,
      // TODO: 拖动
      child: Stack(
        alignment: Alignment.center,
        children: [
          Positioned(
              child: FutureBuilder(
                  future: loadImage(id),
                  builder: (context, snapshot) {
                    if (snapshot.hasData) {
                      return ClipRect(
                          child: SizedBox(
                        width: double.infinity,
                        height: double.infinity,
                        child: ExtendedImage.network(
                          "${dio.options.baseUrl}/image/${snapshot.data?.file_path}",
                          mode: ExtendedImageMode.gesture,
                          // scale: 3,
                        ),
                      ));
                    } else if (snapshot.hasError) {
                      return ErrorWidget("13");
                    }
                    return const Center(
                      child: CircularProgressIndicator(),
                    );
                  })),
          Positioned(
              bottom: 10,
              child: Card(
                // decoration: BoxDecoration(),
                elevation: 30,
                // color: Colors.pinkAccent,
                child: Padding(
                  padding: const EdgeInsets.fromLTRB(20, 5, 20, 5),
                  child: Row(
                    children: [
                      IconButton(
                          onPressed: () {
                            dio.delete("/image/delete/$id").then((resp) {
                              ref.read(uniqueIdProvider.notifier).updateId();
                              context.go("/");
                            });
                          },
                          icon: const Icon(Icons.delete)),
                      IconButton(
                          onPressed: _scale > 0.2 ? () {} : null,
                          icon: const Icon(Icons.remove_circle_outline)),
                      IconButton(
                          onPressed: _scale < 10 ? () {} : null,
                          icon: const Icon(Icons.add_circle_outline)),
                      IconButton(
                          onPressed: () {
                            setState(() {});
                          },
                          icon: const Icon(Icons.menu_rounded)),
                    ],
                  ),
                ),
              )),
        ],
      ),
    );
  }
}
