import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:loading_indicator/loading_indicator.dart';
import '../../themes/app_dimension.dart';

import '../../themes/app_colors.dart';
import '../../themes/styles_text.dart';
import '../models/item_dropdown.dart';
import 'dialog_widget.dart';

class UtilWidget {
  static void showLoading() {
    if (Get.isDialogOpen!) Get.back();

    Get.dialog(
      Center(
        child: Container(
          width: 60.w,
          height: 60.h,
          decoration: BoxDecoration(
            color: Colors.transparent,
            borderRadius: BorderRadius.circular(8),
          ),
          child: LoadingIndicator(
            indicatorType: Indicator.ballClipRotate,
            colors: [
              AppColors.primary1,
            ],
          ),
        ),
      ),
      barrierDismissible: false,
    );
  }

  static void hideLoading() {
    if (Get.isDialogOpen!) Get.back();
  }

  static void hideDialog() {
    if (Get.isDialogOpen!) Get.back();
  }

  static void showAlertConfirm({
    required BuildContext context,
    required String icon,
    required String title,
    required String message,
    required String confirmTitle,
    required Function onPressedConfirm,
    Function? onPressedCancel,
    String? cancelTitle,
    Function? onPressedOther,
    String? otherTitle,
    Color? colorConfirmButton,
  }) {
    showDialog(
      context: context,
      barrierDismissible: true,
      builder: (BuildContext context) {
        return Dialog(
          insetPadding: EdgeInsets.all(10.r),
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(0)),
          child: DialogWidget(
            icon: icon,
            title: title,
            message: message,
            confirmTitle: confirmTitle,
            onPressedConfirm: onPressedConfirm,
            cancelTitle: cancelTitle,
            onPressedCancel: onPressedCancel,
            otherTitle: otherTitle,
            onPressedOther: onPressedOther,
            colorConfirmButton: colorConfirmButton,
          ),
        );
      },
    );
  }

  static void showPicker({
    required BuildContext context,
    required Map<String, String> values,
    required Function(int) onCallback,
    required int initIndex,
  }) {
    showModalBottomSheet(
      context: context,
      builder: (BuildContext context) {
        return Container(
          width: Get.width.w,
          height: 200.h,
          child: CupertinoPicker(
            scrollController: FixedExtentScrollController(
              initialItem: initIndex,
            ),
            backgroundColor: Colors.white,
            onSelectedItemChanged: (value) {
              onCallback(value);
            },
            itemExtent: 28.0.h,
            children: values.entries.map(
              (e) {
                return Text(e.value);
              },
            ).toList(),
          ),
        );
      },
    );
  }

  static void showBottomSheet({
    required BuildContext context,
    required List<ItemDropDown> dataSources,
    required Function(int) onSelected,
    required FixedExtentScrollController controller,
  }) {
    showModalBottomSheet(
        context: context,
        builder: (context) {
          return Container(
            padding: EdgeInsets.symmetric(
              vertical: 10.h,
              horizontal: 20.w,
            ),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    InkWell(
                      onTap: () {
                        Get.back();
                      },
                      child: Text(
                        'Huỷ',
                        style: StylesText.body2.copyWith(
                          color: AppColors.neutral2,
                        ),
                      ),
                    ),
                    InkWell(
                      onTap: () {
                        Get.back();
                        onSelected(controller.selectedItem);
                      },
                      child: Text(
                        'Chọn',
                        style: StylesText.body2,
                      ),
                    )
                  ],
                ),
                Row(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Expanded(
                      child: Container(
                        height: 302.h,
                        child: CupertinoPicker(
                          magnification: 1.22,
                          squeeze: 1.2,
                          useMagnifier: true,
                          itemExtent: 32.h,
                          scrollController: controller,
                          onSelectedItemChanged: (int selectedItem) {},
                          children: List<Widget>.generate(dataSources.length, (index) {
                            return Center(
                              child: Text(
                                dataSources[index].value ?? '',
                                style: StylesText.body6,
                              ),
                            );
                          }),
                        ),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          );
        });
  }

  static void showNormalToast(String message) {
    Fluttertoast.cancel();
    Fluttertoast.showToast(msg: message);
  }

  static void showToastError(String message) {
    Fluttertoast.cancel();
    Fluttertoast.showToast(
      msg: message,
      backgroundColor: Colors.red,
      textColor: Colors.white,
    );
  }

  static void showToastWarning(String message) {
    Fluttertoast.cancel();
    Fluttertoast.showToast(
      msg: message,
      backgroundColor: Colors.orange[400],
      textColor: Colors.white,
    );
  }

  static void showToastSuccess(String message) {
    Fluttertoast.cancel();
    Fluttertoast.showToast(
      msg: message,
      backgroundColor: AppColors.primary1,
      textColor: Colors.white,
    );
  }

  static void showCustomDialog({
    required BuildContext context,
    required Widget content,
    bool? isDismissible,
    Function()? onCloseDialog,
  }) {
    showDialog(
      context: context,
      barrierDismissible: isDismissible ?? true,
      builder: (BuildContext context) {
        return Dialog(
          insetPadding: const EdgeInsets.all(2),
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
          child: Stack(
            children: [
              Container(
                margin: const EdgeInsets.only(
                  top: 4,
                  left: 4,
                  right: 4,
                  bottom: 4,
                ),
                child: content,
              ),
              Positioned(
                top: -10,
                right: -10,
                child: Material(
                  color: Colors.transparent,
                  child: IconButton(
                    icon: Icon(
                      Icons.highlight_off,
                      size: 30,
                      color: AppColors.black,
                    ),
                    onPressed: onCloseDialog ??
                        () {
                          Get.back();
                        },
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }
}
