import 'dart:convert';
import 'dart:io';

import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:feather_icons_flutter/feather_icons_flutter.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

class ImageOptions {
  static showImageOptions(BuildContext context, Function(String) image) {
    showModalBottomSheet(
        backgroundColor: Colors.transparent,
        context: context,
        builder: (BuildContext bc) {
          return _BottomSheet(
            children: <Widget>[
              _BottomSheetItem(
                title: "Select file souse",
              ),
              _BottomSheetItem(
                title: "Gallery",
                icon: FeatherIcons.image,
                onTap: () async {
                  File _image =
                      await ImagePicker.pickImage(source: ImageSource.gallery);
                  String fileName = _image.path;
                  final bytes = File(fileName).readAsBytesSync();
                  String base64Encode = base64.encode(bytes);
                  if (base64Encode != null) {
                    image(base64Encode);
                  }
                },
              ),
              _BottomSheetItem(
                title: "Camera",
                icon: FeatherIcons.camera,
                onTap: () async {
                  File _image =
                      await ImagePicker.pickImage(source: ImageSource.camera);
                  String fileName = _image.path;
                  final bytes = File(fileName).readAsBytesSync();
                  String base64Encode = base64.encode(bytes);
                  if (base64Encode != null) {
                    image(base64Encode);
                  }
                },
              ),
              _BottomSheetItem(
                title: "Cancel",
                onTap: (){},
              )
            ],
          );
        });
  }
}

class _BottomSheet extends StatelessWidget {
  final List<Widget> children;

  _BottomSheet({Key key, @required this.children}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(vertical: 12.0),
      decoration: BoxDecoration(
          color: Theme.of(context).backgroundColor,
          borderRadius: BorderRadius.only(
              topLeft: Radius.circular(16.0), topRight: Radius.circular(16.0))),
      child: SafeArea(
        top: false,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Container(
              decoration: BoxDecoration(
                  color: Theme.of(context).dividerColor,
                  borderRadius: BorderRadius.circular(3.0)),
              width: 40.0,
              height: 6.0,
            ),
            ...children
          ],
        ),
      ),
    );
  }
}

class _BottomSheetItem extends StatelessWidget {
  final Function onTap;
  final IconData icon;
  final String title;
  final ITEM_COLOR color;

  _BottomSheetItem(
      {Key key,
      this.onTap,
      @required this.title,
      this.icon,
      this.color = ITEM_COLOR.primary})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        if (onTap != null) {
          Navigator.pop(context);
          onTap();
        }
      },
      child: Padding(
        padding: EdgeInsets.symmetric(horizontal: 18.0, vertical: 18.0),
        child: Row(
          children: <Widget>[
            if (icon != null)
              Icon(
                icon,
                color: color == ITEM_COLOR.error
                    ? Theme.of(context).errorColor
                    : Theme.of(context).primaryColor,
                size: 18.0,
              ),
            if (icon != null) SizedBox(width: 24.0),
            Texts(
              title ?? "",
              style: "bodyText2",
              color: color == ITEM_COLOR.error
                  ? Theme.of(context).errorColor
                  : null,
            ),
          ],
        ),
      ),
    );
  }
}

enum ITEM_COLOR { primary, error }
