// ignore_for_file: use_build_context_synchronously

import 'package:flutter/material.dart';
import '../../product/constant/enums/role_enums.dart';
import '../../product/services/api_services.dart';
import '../../product/services/language_services.dart';
import '../../product/utils/response_status_utils.dart';

handleDeleteDevice(BuildContext context, String thingID, String role) {
  showDialog(
    context: context,
    builder: (dialogContext) {
      return AlertDialog(
        title: Text(appLocalization(dialogContext).delete_device_dialog_title),
        content:
            Text(appLocalization(dialogContext).delete_device_dialog_content),
        actions: <Widget>[
          TextButton(
            child: Text(appLocalization(dialogContext).cancel_button_content),
            onPressed: () {
              Navigator.of(dialogContext).pop();
            },
          ),
          TextButton(
            child: Text(
              appLocalization(dialogContext).delete_button_content,
              style: const TextStyle(color: Colors.red),
            ),
            onPressed: () {
              deleteOrUnregisterDevice(context, thingID, role);
              Navigator.of(context).pop();
            },
          ),
        ],
      );
    },
  );
}

deleteOrUnregisterDevice(
    BuildContext context, String thingID, String role) async {
  APIServices apiServices = APIServices();
  if (role == RoleEnums.USER.name) {
    Map<String, dynamic> body = {
      "thing_id": thingID,
    };
    int statusCode = await apiServices.unregisterDevice(body);
    showSnackBarResponseByStatusCode(
        context,
        statusCode,
        appLocalization(context).notification_delete_device_success,
        appLocalization(context).notification_delete_device_failed);
  } else {
    int statusCode = await apiServices.deleteDeviceByAdmin(thingID);
    showSnackBarResponseByStatusCode(
        context,
        statusCode,
        appLocalization(context).notification_delete_device_success,
        appLocalization(context).notification_delete_device_failed);
  }
}
