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

import 'package:hmg_patient_app/config/config.dart';
import 'package:hmg_patient_app/services/permission/permission_service.dart';
import 'package:hmg_patient_app/theme/colors.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/uitl/utils.dart';
import 'package:hmg_patient_app/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, File) image,
  ) {
    showModalBottomSheet(
        backgroundColor: Colors.transparent,
        context: context,
        builder: (BuildContext bc) {
          return _BottomSheet(
            children: <Widget>[
              _BottomSheetItem(
                title: TranslationBase.of(context).selectFileSouse,
              ),
              _BottomSheetItem(
                title: TranslationBase.of(context).gallery,
                icon: Icons.image,
                onTap: () async {
                  if (Platform.isAndroid) {
                    galleryImageAndroid(image);
                  } else {
                    File _image = File((await ImagePicker.platform.pickImage(source: ImageSource.gallery, imageQuality: 20))!.path);
                    String fileName = _image.path;
                    final bytes = File(fileName).readAsBytesSync();
                    String base64Encode = base64.encode(bytes);
                    if (base64Encode != null) {
                      image(base64Encode, _image);
                    }
                  }
                },
              ),
              _BottomSheetItem(
                title: TranslationBase.of(context).camera,
                icon: Icons.camera_alt,
                onTap: () async {
                  if (Platform.isAndroid) {
                    cameraImageAndroid(image);
                  } else {
                    File _image = File((await ImagePicker.platform.pickImage(source: ImageSource.camera, imageQuality: 20))!.path);
                    String fileName = _image.path;
                    final bytes = File(fileName).readAsBytesSync();
                    String base64Encode = base64.encode(bytes);
                    if (base64Encode != null) {
                      image(base64Encode, _image);
                    }
                  }
                },
              ),
              _BottomSheetItem(
                title: TranslationBase.of(context).cancel,
                onTap: () {},
              )
            ],
          );
        });
  }
}

galleryImageAndroid(Function(String, File) image) async {
  if (await PermissionService.isExternalStorageEnabled()) {
    File _image = File((await ImagePicker.platform.pickImage(source: ImageSource.gallery, imageQuality: 20))!.path);
    String fileName = _image.path;
    final bytes = File(fileName).readAsBytesSync();
    String base64Encode = base64.encode(bytes);
    if (base64Encode != null) {
      image(base64Encode, _image);
    }
  } else {
    Utils.showPermissionConsentDialog(AppGlobal.context, TranslationBase.of(AppGlobal.context).galleryPermission, () async {
      File _image = File((await ImagePicker.platform.pickImage(source: ImageSource.gallery, imageQuality: 20))!.path);
      String fileName = _image.path;
      final bytes = File(fileName).readAsBytesSync();
      String base64Encode = base64.encode(bytes);
      if (base64Encode != null) {
        image(base64Encode, _image);
      }
    });
  }
}

cameraImageAndroid(Function(String, File) image) async {
  if (await PermissionService.isCameraEnabled()) {
    File _image = File((await ImagePicker.platform.pickImage(source: ImageSource.camera, imageQuality: 20))!.path);
    String fileName = _image.path;
    final bytes = File(fileName).readAsBytesSync();
    String base64Encode = base64.encode(bytes);
    if (base64Encode != null) {
      image(base64Encode, _image);
    }
  } else {
    Utils.showPermissionConsentDialog(AppGlobal.context, TranslationBase.of(AppGlobal.context).cameraPermissionDialog, () async {
      File _image = File((await ImagePicker.platform.pickImage(source: ImageSource.camera, imageQuality: 20))!.path);
      String fileName = _image.path;
      final bytes = File(fileName).readAsBytesSync();
      String base64Encode = base64.encode(bytes);
      if (base64Encode != null) {
        image(base64Encode, _image);
      }
    });
  }
}

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: Colors.white, 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 ? CustomColors.accentColor : Theme.of(context).primaryColor,
                size: 18.0,
              ),
            if (icon != null) SizedBox(width: 24.0),
            Texts(
              title ?? "",
              style: "bodyText2",
              color: color == ITEM_COLOR.error ? CustomColors.accentColor : null,
            ),
          ],
        ),
      ),
    );
  }
}

enum ITEM_COLOR { primary, error }
