import 'package:chat/generated/i18n.dart';
import 'package:chat/photo/provider/asset_provider.dart';
import 'package:chat/photo/provider/config_provider.dart';
import 'package:chat/photo/ui/page/first_photo_item.dart';
import 'package:chat/utils/loading_builder.dart';
import 'package:chat/utils/screen.dart';
import 'package:flutter/material.dart';
import 'package:photo_manager/photo_manager.dart';

class PhotoSelectMenu extends StatefulWidget {
  final GlobalKey parentKey;
  final Function onSelectFolder;
  PhotoSelectMenu({this.parentKey, this.onSelectFolder});
  @override
  _PhotoSelectMenuState createState() => _PhotoSelectMenuState();
}

class _PhotoSelectMenuState extends State<PhotoSelectMenu> {
  AssetProvider get assetProvider =>
      PhotoPickerProvider.of(context).assetProvider;

  OverlayEntry overlayEntry;
  bool isQuit = false;
  @override
  void dispose() {
    isQuit = true;
    clearOverlay();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
        child: Container(
          child: Row(mainAxisSize: MainAxisSize.min, children: <Widget>[
            Text(assetProvider.current == null
                ? I18n.of(context).Photo_album
                : assetProvider.current.name),
            Icon(
              overlayEntry == null
                  ? Icons.keyboard_arrow_down
                  : Icons.keyboard_arrow_up,
              color: Color(0xFFB5B4B4),
              size: 20,
            ),
          ]),
        ),
        onTap: overlayEntry == null ? showAllFolders : clearOverlay);
  }

  void showAllFolders() {
    final RenderBox appBarBox =
        widget.parentKey.currentContext.findRenderObject();

    overlayEntry = OverlayEntry(
        builder: (context) => Positioned(
            width: Screen.width,
            top: appBarBox.size.height,
            child: Material(
              elevation: 1,
              color: Colors.black26,
              child: Container(
                  height: Screen.height,
                  decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.only(
                          bottomLeft: Radius.circular(10.0),
                          bottomRight: Radius.circular(10.0))),
                  child: FutureLoadingBuilder<List<AssetPathEntity>>(
                      future: PhotoManager.getAssetPathList(
                          type: RequestType.image),
                      builder: (BuildContext context,
                          List<AssetPathEntity> pathList) {
                        return ListView(
                            children: List<Widget>.generate(
                                pathList.length,
                                (i) => FirstPhotoItem(
                                      pathEntity: pathList[i],
                                      onSelectFolder: () {
                                        widget.onSelectFolder(pathList[i]);
                                        clearOverlay();
                                      },
                                    )));
                      })),
            )));

    Overlay.of(context).insert(overlayEntry);
    setState(() {});
  }

  void clearOverlay() {
    if (overlayEntry != null) {
      overlayEntry.remove();
      overlayEntry = null;

      if (!isQuit) {
        setState(() {});
      }
    }
  }
}
