// ignore_for_file: use_build_context_synchronously

import 'package:flutter/material.dart';
import 'inter_family_bloc.dart';
import '../../product/constant/icon/icon_constants.dart';
import '../../product/services/language_services.dart';
import '../../product/utils/qr_utils.dart';

createOrJoinGroupDialog(
    BuildContext context,
    InterFamilyBloc interFamilyBloc,
    String role,
    String titleDialogName,
    String titleTextField1,
    String textFieldContent1,
    bool icon,
    String groupID,
    String titleTextField2,
    String textFieldContent2) {
  TextEditingController editingController = TextEditingController();
  TextEditingController readOnlycontroller = TextEditingController();
  TextEditingController descriptionController = TextEditingController();

  if (textFieldContent1 != "") {
    editingController.text = textFieldContent1;
  }
  if (textFieldContent2 != "") {
    descriptionController.text = textFieldContent2;
  }
  // final byte = b
  showDialog(
      context: context,
      builder: (dialogContext) {
        return AlertDialog(
          title: Center(child: Text(titleDialogName)),
          content: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              icon
                  ? Column(
                      children: [
                        TextField(
                          controller: editingController,
                          textInputAction: TextInputAction.done,
                          decoration: InputDecoration(
                            suffixIcon: IconButton(
                                onPressed: () async {
                                  String data = await QRScanUtils.instance
                                      .scanQR(dialogContext);
                                  Map<String, dynamic> items =
                                      QRScanUtils.instance.getQRData(data);
                                  String groupID = items['group_id'];
                                  String groupName = items['group_name'];
                                  editingController.text = groupID;
                                  readOnlycontroller.text = groupName;
                                },
                                icon: IconConstants.instance
                                    .getMaterialIcon(Icons.qr_code_outlined)),
                            label: Text(titleTextField1),
                            border: const OutlineInputBorder(
                                borderRadius:
                                    BorderRadius.all(Radius.circular(16.0))),
                          ),
                        ),
                        const SizedBox(height: 10),
                        TextField(
                          readOnly: true,
                          controller: readOnlycontroller,
                          decoration: InputDecoration(
                              label: Text(titleTextField2),
                              border: const OutlineInputBorder(
                                  borderRadius:
                                      BorderRadius.all(Radius.circular(16.0)))),
                        ),
                      ],
                    )
                  : Column(
                      children: [
                        TextField(
                          textInputAction: TextInputAction.next,
                          controller: editingController,
                          decoration: InputDecoration(
                              label: Text(titleTextField1),
                              border: const OutlineInputBorder(
                                  borderRadius:
                                      BorderRadius.all(Radius.circular(16.0)))),
                        ),
                        const SizedBox(height: 10),
                        TextField(
                          onChanged: (value) {},
                          maxLines: 3,
                          textInputAction: TextInputAction.done,
                          controller: descriptionController,
                          decoration: InputDecoration(
                              labelText:
                                  appLocalization(context).description_group,
                              border: const OutlineInputBorder(
                                  borderRadius:
                                      BorderRadius.all(Radius.circular(16.0)))),
                        ),
                      ],
                    ),
            ],
          ),
          actions: <Widget>[
            TextButton(
              onPressed: () {
                Navigator.of(dialogContext).pop();
              },
              child: Text(
                appLocalization(context).cancel_button_content,
                style: const TextStyle(color: Colors.red),
              ),
            ),
            TextButton(
                onPressed: () async {
                  if (!icon) {
                    String groupName = editingController.text;
                    String description = descriptionController.text;
                    if (titleDialogName ==
                        appLocalization(context).add_new_group) {
                      try {
                        await interFamilyBloc.createGroup(
                            context, groupName, description);
                        interFamilyBloc.getAllGroup(role);
                        Navigator.of(dialogContext).pop();
                      } catch (e) {
                        // log("Lỗi khi tạo nhóm: $e");
                      }
                    } else if (titleDialogName ==
                        appLocalization(context)
                            .change_group_infomation_content) {
                      try {
                        await interFamilyBloc.changeGroupInfomation(
                            context, groupID, groupName, description);
                        interFamilyBloc.getAllGroup(role);
                        Navigator.of(dialogContext).pop();
                      } catch (e) {
                        // log("Lỗi khi sửa nhóm: $e");
                      }
                    }
                  } else {
                    String groupId = editingController.text;
                    await interFamilyBloc.joinGroup(context, groupId);
                    Navigator.of(dialogContext).pop();
                  }
                },
                child: Text(appLocalization(context).confirm_button_content))
          ],
        );
      });
}
