// ignore_for_file: use_build_context_synchronously

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:intl/intl.dart';
import 'package:sfm_app/feature/home/device_alias_model.dart';
import '../../../product/constant/enums/app_route_enums.dart';
import '../../../product/constant/image/image_constants.dart';
import '../../../product/extension/context_extension.dart';
import '../../../product/services/language_services.dart';
import '../../../product/utils/device_utils.dart';

import '../../../product/constant/icon/icon_constants.dart';

Future<Widget> notificationCard(BuildContext context, String notiticationType,
    String notificationTitle, DeviceWithAlias device) async {
  String location = "";
  if (device.areaPath != "") {
    location = await DeviceUtils.instance
        .getFullDeviceLocation(context, device.areaPath!,"");
  }
  String path = "";
  // DateTime time = DateTime.now();
  String time = "";
  for (var sensor in device.status!.sensors!) {
    if (sensor.name! == "7") {
      DateTime dateTime =
          DateTime.fromMillisecondsSinceEpoch((sensor.time!) * 1000);
      time = DateFormat('yyyy-MM-dd HH:mm:ss').format(dateTime);
    }
  }
  if (notiticationType == "lowBattery") {
    path = ImageConstants.instance.getImage("low_battery");
  }
  return Card(
    child: Padding(
      padding: context.paddingLow,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  SizedBox(
                    child: Text(
                      notificationTitle,
                      style: const TextStyle(
                        letterSpacing: 1,
                        fontWeight: FontWeight.bold,
                        color: Color.fromARGB(255, 250, 84, 34),
                        fontSize: 18,
                      ),
                    ),
                  ),
                  SizedBox(height: context.lowValue),
                  SizedBox(
                    child: Text(
                      "${appLocalization(context).device_title} ${device.isOwner! ? device.name : device.alias}",
                      style: context.responsiveBodyLargeWithBold,
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                      softWrap: true,
                      textAlign: TextAlign.start,
                    ),
                  ),
                ],
              ),
              SizedBox(
                height: context.dynamicWidth(0.15),
                width: context.dynamicWidth(0.15),
                child: Image.asset(path),
              ),
            ],
          ),
          SizedBox(height: context.lowValue),
          Row(
            children: [
              IconConstants.instance
                  .getMaterialIcon(Icons.location_on_outlined),
              SizedBox(
                width: context.lowValue,
              ),
              Expanded(
                child: Text(
                  location,
                  style: context.responsiveBodySmall,
                  maxLines: 2,
                  overflow: TextOverflow.ellipsis,
                  softWrap: true,
                  textAlign: TextAlign.start,
                ),
              ),
            ],
          ),
          SizedBox(height: context.lowValue),
          Row(
            children: [
              IconConstants.instance.getMaterialIcon(Icons.schedule),
              SizedBox(
                width: context.lowValue,
              ),
              Expanded(
                child: Text(
                  time.toString(),
                  style: context.responsiveBodySmall,
                  maxLines: 2,
                  overflow: TextOverflow.ellipsis,
                  softWrap: true,
                  textAlign: TextAlign.start,
                ),
              ),
            ],
          ),
          Align(
            alignment: Alignment.centerRight,
            child: OutlinedButton(
              style: const ButtonStyle(
                backgroundColor: WidgetStatePropertyAll(Colors.blueAccent),
              ),
              onPressed: () {
                context.pushNamed(AppRoutes.DEVICE_DETAIL.name,
                    pathParameters: {'thingID': device.thingId!});
              },
              child: Text(
                appLocalization(context).detail_message,
                style: const TextStyle(
                  color: Colors.white,
                ),
              ),
            ),
          ),
        ],
      ),
    ),
  );
}
