import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:syncrow_app/features/devices/model/device_model.dart';
import 'package:syncrow_app/features/scene/widgets/scene_list_tile.dart';
import 'package:syncrow_app/features/shared_widgets/default_container.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart';
import 'package:syncrow_app/navigation/routing_constants.dart';
import 'package:syncrow_app/utils/context_extension.dart';
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';

class SceneDevicesList extends StatelessWidget {
  const SceneDevicesList({
    required this.isAutomationDeviceStatus,
    required this.devices,
    super.key,
  });
  final List<DeviceModel> devices;
  final bool isAutomationDeviceStatus;

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: devices.length,
      itemBuilder: (context, index) => _buildDeviceTile(context, devices[index]),
    );
  }

  Widget _buildDeviceTile(
    BuildContext context,
    DeviceModel device,
  ) {
    return DefaultContainer(
      child: SceneListTile(
        minLeadingWidth: 40,
        leadingWidget: SvgPicture.asset(device.icon ?? ''),
        titleWidget: BodyMedium(
          text: device.name ?? '',
          style: context.titleSmall.copyWith(
            color: ColorsManager.secondaryTextColor,
            fontWeight: FontWeight.w400,
            fontSize: 20,
          ),
        ),
        trailingWidget: const Icon(
          Icons.arrow_forward_ios_rounded,
          color: ColorsManager.greyColor,
          size: 16,
        ),
        onPressed: () => _navigateToDeviceFunctions(context, device),
      ),
    );
  }

  void _navigateToDeviceFunctions(
    BuildContext context,
    DeviceModel device,
  ) {
    Navigator.of(context).pushNamed(
      Routes.deviceFunctionsRoute,
      arguments: {
        "device": device,
        "isAutomationDeviceStatus": isAutomationDeviceStatus,
      },
    );
  }
}
