// sos device control view

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/device_managment/all_devices/models/devices_model.dart';
import 'package:syncrow_web/pages/device_managment/shared/icon_name_status_container.dart';
import 'package:syncrow_web/pages/device_managment/shared/table/report_table.dart';
import 'package:syncrow_web/pages/device_managment/sos/bloc/sos_device_bloc.dart';
import 'package:syncrow_web/pages/device_managment/sos/widgets/sos_notification_dialog.dart';
import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/constants/assets.dart';
import 'package:syncrow_web/utils/helpers/responsice_layout_helper/responsive_layout_helper.dart';

import '../models/sos_status_model.dart';

class SosDeviceControlsView extends StatelessWidget
    with HelperResponsiveLayout {
  const SosDeviceControlsView({
    super.key,
    required this.device,
  });

  final AllDevicesModel device;

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
        create: (context) =>
            SosDeviceBloc()..add(GetDeviceStatus(device.uuid!)),
        child: BlocBuilder<SosDeviceBloc, SosDeviceState>(
          builder: (context, state) {
            if (state is SosDeviceLoadingState) {
              return const Center(child: CircularProgressIndicator());
            } else if (state is SosDeviceLoadedState) {
              return _buildStatusControls(context, state.sosStatusModel);
            } else if (state is SosReportLoadedState) {
              return ReportsTable(
                report: state.sosReport,
                hideValueShowDescription: true,
                garageDoorSensor: true,
                onRowTap: (index) {},
                onClose: () {
                  context.read<SosDeviceBloc>().add(BackToSosStatusView());
                },
              );
            } else if (state is SosAutomationReportLoadedState) {
              return ReportsTable(
                report: state.automationReport,
                hideValueShowDescription: true,
                garageDoorSensor: true,
                onRowTap: (index) {},
                onClose: () {
                  context.read<SosDeviceBloc>().add(BackToSosStatusView());
                },
              );
            } else if (state is SosDeviceErrorState) {
              return const Center(child: Text('Error fetching status'));
            } else if (state is SosAutomationReportErrorState) {
              return Center(child: Text('Error: ${state.message.toString()}'));
            } else if (state is SosReportErrorState) {
              return Center(child: Text('Error: ${state.message.toString()}'));
            }
            return const Center(child: CircularProgressIndicator());
          },
        ));
  }

  Widget _buildStatusControls(
      BuildContext context, SosStatusModel deviceStatus) {
    final isExtraLarge = isExtraLargeScreenSize(context);
    final isLarge = isLargeScreenSize(context);
    final isMedium = isMediumScreenSize(context);
    return GridView(
      padding: const EdgeInsets.symmetric(horizontal: 50),
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: isLarge || isExtraLarge
            ? 3
            : isMedium
                ? 2
                : 1,
        mainAxisExtent: 140,
        crossAxisSpacing: 12,
        mainAxisSpacing: 12,
      ),
      children: [
        IconNameStatusContainer(
          isFullIcon: false,
          name: deviceStatus.sosStatus == 'sos' ? 'SOS' : 'Normal',
          icon: deviceStatus.sosStatus == 'sos' ? Assets.sosNormal : Assets.sos,
          onTap: () {},
          status: false,
          textColor: ColorsManager.blackColor,
        ),
        IconNameStatusContainer(
          isFullIcon: false,
          name: 'SOS Records',
          icon: Assets.records,
          onTap: () {
            context.read<SosDeviceBloc>().add(
                  GetDeviceRecords(
                    device.uuid!,
                  ),
                );
          },
          status: false,
          textColor: ColorsManager.blackColor,
        ),
        IconNameStatusContainer(
          isFullIcon: false,
          name: 'Automation Record',
          icon: Assets.automationRecords,
          onTap: () {
            // context.read<SosDeviceBloc>().add(
            //       GetDeviceAutomationRecords(
            //         device.uuid!,
            //       ),
            //     );
          },
          status: false,
          textColor: ColorsManager.blackColor,
        ),
        IconNameStatusContainer(
          isFullIcon: false,
          name: 'Alarm Settings',
          icon: Assets.mainDoorNotifi,
          onTap: () {
            showDialog(
              context: context,
              builder: (context) => const SosNotificationDialog(),
            );
          },
          status: false,
          textColor: ColorsManager.blackColor,
          paddingAmount: 14,
        ),
      ],
    );
  }
}
