/// This widget represents a switch for a device on the room page.
///
/// This widget displays the icon and name of the device, along with a switch
/// to control its state. Tapping on the widget opens the device interface.
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart';
import 'package:syncrow_app/features/devices/bloc/devices_cubit.dart';
import 'package:syncrow_app/features/devices/model/device_model.dart';
import 'package:syncrow_app/features/devices/view/widgets/6_scene_switch/six_scene_screen.dart';
import 'package:syncrow_app/features/devices/view/widgets/ACs/acs_view.dart';
import 'package:syncrow_app/features/devices/view/widgets/ceiling_sensor/ceiling_sensor_interface.dart';
import 'package:syncrow_app/features/devices/view/widgets/curtains/curtain_view.dart';
import 'package:syncrow_app/features/devices/view/widgets/door_sensor/door_sensor_screen.dart';
import 'package:syncrow_app/features/devices/view/widgets/flush_sensor/flush_sensor_interface.dart';
import 'package:syncrow_app/features/devices/view/widgets/four_scene_switch/four_scene_screen.dart';
import 'package:syncrow_app/features/devices/view/widgets/garage_door/garage_door_screen.dart';
import 'package:syncrow_app/features/devices/view/widgets/gateway/gateway_view.dart';
import 'package:syncrow_app/features/devices/view/widgets/lights/light_interface.dart';
import 'package:syncrow_app/features/devices/view/widgets/one_gang/one_gang_Interface.dart';
import 'package:syncrow_app/features/devices/view/widgets/one_touch/one_touch_screen.dart';
import 'package:syncrow_app/features/devices/view/widgets/power_clamp/power_clamp_page.dart';
import 'package:syncrow_app/features/devices/view/widgets/smart_door/door_interface.dart';
import 'package:syncrow_app/features/devices/view/widgets/sos/sos_screen.dart';
import 'package:syncrow_app/features/devices/view/widgets/three_gang/three_gang_interface.dart';
import 'package:syncrow_app/features/devices/view/widgets/three_touch/three_touch_interface.dart';
import 'package:syncrow_app/features/devices/view/widgets/two_gang/two_gang_Interface.dart';
import 'package:syncrow_app/features/devices/view/widgets/two_touch/two_touch_Interface.dart';
import 'package:syncrow_app/features/devices/view/widgets/wall_sensor/wall_sensor_interface.dart';
import 'package:syncrow_app/features/devices/view/widgets/water_heater/water_heater_page.dart';
import 'package:syncrow_app/features/devices/view/widgets/water_leak/water_leak_screen.dart';
import 'package:syncrow_app/features/shared_widgets/custom_switch.dart';
import 'package:syncrow_app/features/shared_widgets/default_container.dart';
import 'package:syncrow_app/utils/context_extension.dart';
import 'package:syncrow_app/utils/helpers/custom_page_route.dart';
import 'package:syncrow_app/utils/resource_manager/constants.dart';

class RoomPageSwitch extends StatelessWidget {
  const RoomPageSwitch(
      {super.key,
      required this.device,
      this.isAllDevices = false,
      this.allDevices});

  final DeviceModel device;
  final List<DeviceModel>? allDevices;
  final bool isAllDevices;
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        showDeviceInterface(
            device: device,
            context: context,
            isAllDevices: isAllDevices,
            allDevices: allDevices);
      },
      child: DefaultContainer(
        padding: const EdgeInsets.all(15),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                SvgPicture.asset(
                  device.icon!,
                  fit: BoxFit.contain,
                ),
                isAllDevices
                    ? CustomSwitch(
                        device: device,
                      )
                    : const SizedBox.shrink(),
              ],
            ),
            Text(
              device.name ?? '',
              textScaler: TextScaler.linear(1),
              style: context.bodyLarge.copyWith(
                fontWeight: FontWeight.bold,
                color: Colors.grey,
                fontSize: 14,
              ),
              overflow: TextOverflow.ellipsis,
              maxLines: 1,
            ),
            FittedBox(
              fit: BoxFit.scaleDown,
              child: Text(
                device.subspace!.subspaceName ?? '',
                overflow: TextOverflow.ellipsis,
                textScaler: TextScaler.linear(1),
                style: context.bodySmall.copyWith(
                  fontWeight: FontWeight.w400,
                  fontSize: 9,
                  color: Colors.grey,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

/// Shows the device interface based on the product type of the device.
///
/// The [device] parameter represents the device model.
Future<void> showDeviceInterface(
    {required DeviceModel device,
    required BuildContext context,
    required isAllDevices,
    List<DeviceModel>? allDevices}) async {
  final devicesCubit = context.read<DevicesCubit>();

  switch (device.productType) {
    case DeviceType.AC:
      var value = await Navigator.push(
          context,
          PageRouteBuilder(
              pageBuilder: (context, animation1, animation2) =>
                  ACsView(deviceModel: device)));
      if (value && isAllDevices) {
        devicesCubit.fetchAllDevices(HomeCubit.getInstance().selectedSpace);
      }
      // navigateToInterface(ACsView(deviceModel: device), context);
      break;
    case DeviceType.WallSensor:
      // navigateToInterface(WallMountedInterface(wallSensor: device), context);
      Navigator.push(
          context,
          PageRouteBuilder(
              pageBuilder: (context, animation1, animation2) =>
                  WallMountedInterface(deviceModel: device)));
      break;
    case DeviceType.CeilingSensor:
      Navigator.push(
          context,
          PageRouteBuilder(
              pageBuilder: (context, animation1, animation2) =>
                  CeilingSensorInterface(ceilingSensor: device)));
      // navigateToInterface(CeilingSensorInterface(ceilingSensor: device), context);
      break;
    case DeviceType.Curtain:
      var value = await Navigator.push(
          context,
          PageRouteBuilder(
              pageBuilder: (context, animation1, animation2) => CurtainView(
                    curtain: device,
                  )));
      if (value && isAllDevices) {
        devicesCubit.fetchAllDevices(HomeCubit.getInstance().selectedSpace);
      }
      break;
    case DeviceType.Blind:
      break;
    case DeviceType.DoorLock:
      Navigator.push(
          context,
          PageRouteBuilder(
              pageBuilder: (context, animation1, animation2) =>
                  DoorInterface(doorLock: device)));
      // navigateToInterface(DoorInterface(doorlock: device), context);
      break;
    case DeviceType.Gateway:
      Navigator.push(
          context,
          PageRouteBuilder(
              pageBuilder: (context, animation1, animation2) =>
                  GateWayView(gatewayObj: device)));
      break;
    case DeviceType.LightBulb:
      navigateToInterface(LightInterface(light: device), context);
    case DeviceType.OneGang:
      var value = await Navigator.push(
          context,
          PageRouteBuilder(
              pageBuilder: (context, animation1, animation2) =>
                  OneGangInterface(gangSwitch: device)));

      if (value && isAllDevices) {
        devicesCubit.fetchAllDevices(HomeCubit.getInstance().selectedSpace);
      }
    case DeviceType.TwoGang:
      var value = await Navigator.push(
          context,
          PageRouteBuilder(
              pageBuilder: (context, animation1, animation2) =>
                  TwoGangInterface(gangSwitch: device)));
      if (value && isAllDevices) {
        devicesCubit.fetchAllDevices(HomeCubit.getInstance().selectedSpace);
      }
    case DeviceType.ThreeGang:
      var value = await Navigator.push(
          context,
          PageRouteBuilder(
              pageBuilder: (context, animation1, animation2) =>
                  ThreeGangInterface(gangSwitch: device)));
      if (value && isAllDevices) {
        devicesCubit.fetchAllDevices(HomeCubit.getInstance().selectedSpace);
      }

    case DeviceType.WH:
      var value = await Navigator.push(
          context,
          PageRouteBuilder(
              pageBuilder: (context, animation1, animation2) =>
                  WaterHeaterPage(device: device)));
      if (value && isAllDevices) {
        devicesCubit.fetchAllDevices(HomeCubit.getInstance().selectedSpace);
      }
    case DeviceType.DS:
      Navigator.push(
          context,
          PageRouteBuilder(
              pageBuilder: (context, animation1, animation2) =>
                  DoorSensorScreen(device: device)));

    case DeviceType.PC:
      Navigator.push(
          context,
          PageRouteBuilder(
              pageBuilder: (context, animation1, animation2) =>
                  PowerClampPage(device: device)));

    case DeviceType.OneTouch:
      var value = await Navigator.push(
          context,
          PageRouteBuilder(
              pageBuilder: (context, animation1, animation2) =>
                  OneTouchScreen(device: device)));
      if (value && isAllDevices) {
        devicesCubit.fetchAllDevices(HomeCubit.getInstance().selectedSpace);
      }

    case DeviceType.TowTouch:
      var value = await Navigator.push(
          context,
          PageRouteBuilder(
              pageBuilder: (context, animation1, animation2) =>
                  TwoTouchInterface(touchSwitch: device)));
      if (value && isAllDevices) {
        devicesCubit.fetchAllDevices(HomeCubit.getInstance().selectedSpace);
      }
    case DeviceType.ThreeTouch:
      var value = await Navigator.push(
          context,
          PageRouteBuilder(
              pageBuilder: (context, animation1, animation2) =>
                  ThreeTouchInterface(touchSwitch: device)));
      if (value && isAllDevices) {
        devicesCubit.fetchAllDevices(HomeCubit.getInstance().selectedSpace);
      }

    case DeviceType.GarageDoor:
      var value = await Navigator.push(
          context,
          PageRouteBuilder(
              pageBuilder: (context, animation1, animation2) =>
                  GarageDoorScreen(device: device)));
      if (value && isAllDevices) {
        devicesCubit.fetchAllDevices(HomeCubit.getInstance().selectedSpace);
      }

    case DeviceType.WaterLeak:
      Navigator.push(
          context,
          PageRouteBuilder(
              pageBuilder: (context, animation1, animation2) =>
                  WaterLeakScreen(device: device)));

    case DeviceType.SixScene:
      var value = await Navigator.push(
          context,
          PageRouteBuilder(
              pageBuilder: (context, animation1, animation2) =>
                  SixSceneScreen(device: device)));
      if (value && isAllDevices) {
        devicesCubit.fetchAllDevices(HomeCubit.getInstance().selectedSpace);
      }

    case DeviceType.FourScene:
      var value = await Navigator.push(
          context,
          PageRouteBuilder(
              pageBuilder: (context, animation1, animation2) =>
                  FourSceneScreen(device: device)));
      if (value && isAllDevices) {
        devicesCubit.fetchAllDevices(HomeCubit.getInstance().selectedSpace);
      }

    case DeviceType.SOS:
      Navigator.push(
          context,
          PageRouteBuilder(
              pageBuilder: (context, animation1, animation2) =>
                  SosScreen(device: device)));

    case DeviceType.FlushMountedSensor:
      Navigator.push(
          context,
          PageRouteBuilder(
              pageBuilder: (context, animation1, animation2) =>
                  FlushMountedInterface(deviceModel: device)));

      break;
    default:
  }
}

/// Navigates to the specified device interface.
///
/// The [interface] parameter represents the widget interface.
void navigateToInterface(Widget interface, context) {
  Navigator.push(
    context,
    CustomPageRoute(
      builder: (context) => BlocProvider(
        create: (context) => DevicesCubit.getInstance(),
        child: interface,
      ),
    ),
  );
}
