// ignore_for_file: use_build_context_synchronously

import 'package:flutter/material.dart';
import '../../product/utils/response_status_utils.dart';
import '../../product/constant/enums/role_enums.dart';
import '../../product/services/api_services.dart';
import '../../product/utils/qr_utils.dart';
import '../../product/constant/icon/icon_constants.dart';
import '../../product/extention/context_extention.dart';
import '../../product/services/language_services.dart';

addNewDevice(BuildContext context, String role) async {
  TextEditingController extIDController = TextEditingController(text: "");
  TextEditingController deviceNameController = TextEditingController(text: "");
  ScaffoldMessenger.of(context).showSnackBar(
    SnackBar(
      dismissDirection: DismissDirection.none,
      duration: context.dynamicMinutesDuration(1),
      content: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text('${appLocalization(context).add_device_title}: ',
                  style: context.titleMediumTextStyle),
              Container(
                alignment: Alignment.centerRight,
                child: IconButton(
                    onPressed: () async {
                      ScaffoldMessenger.of(context).hideCurrentSnackBar();
                    },
                    icon: IconConstants.instance.getMaterialIcon(Icons.close)),
              )
            ],
          ),
          TextField(
            textInputAction: TextInputAction.next,
            controller: extIDController,
            decoration: InputDecoration(
                hintText: appLocalization(context).input_extID_device_hintText,
                suffixIcon: IconButton(
                    onPressed: () async {
                      String result =
                          await QRScanUtils.instance.scanQR(context);
                      extIDController.text = result;
                    },
                    icon: IconConstants.instance
                        .getMaterialIcon(Icons.qr_code_scanner_outlined))),
          ),
          role == RoleEnums.ADMIN.name
              ? TextField(
                  textInputAction: TextInputAction.done,
                  controller: deviceNameController,
                  decoration: InputDecoration(
                      hintText:
                          appLocalization(context).input_name_device_hintText),
                )
              : const SizedBox.shrink(),
          Center(
            child: TextButton(
                onPressed: () async {
                  String extID = extIDController.text;
                  String deviceName = deviceNameController.text;
                  addDevices(context, role, extID, deviceName);
                  ScaffoldMessenger.of(context).hideCurrentSnackBar();
                },
                child: Text(appLocalization(context).add_button_content)),
          )
        ],
      ),
    ),
  );
}

void addDevices(
    BuildContext context, String role, String extID, String deviceName) async {
  APIServices apiServices = APIServices();
  Map<String, dynamic> body = {};
  if (role == RoleEnums.ADMIN.name) {
    body = {"ext_id": extID, "name": deviceName};
    int statusCode = await apiServices.createDeviceByAdmin(body);
    showSnackBarResponseByStatusCode(
        context,
        statusCode,
        appLocalization(context).notification_create_device_success,
        appLocalization(context).notification_create_device_failed);
  } else {
    body = {"ext_id": extID};
    int statusCode = await apiServices.registerDevice(body);
    showSnackBarResponseByStatusCode(
        context,
        statusCode,
        appLocalization(context).notification_add_device_success,
        appLocalization(context).notification_device_not_exist);
  }
}
