import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/svg.dart';
import 'package:syncrow_web/pages/device_managment/all_devices/bloc/device_mgmt_bloc/device_managment_bloc.dart';
import 'package:syncrow_web/pages/device_managment/all_devices/models/devices_model.dart';
import 'package:syncrow_web/pages/device_managment/device_setting/bloc/setting_bloc_bloc.dart';
import 'package:syncrow_web/pages/device_managment/device_setting/settings_model/device_info_model.dart';
import 'package:syncrow_web/pages/device_managment/device_setting/settings_model/sub_space_model.dart';
import 'package:syncrow_web/pages/device_managment/device_setting/sub_space_dialog.dart';
import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/constants/assets.dart';
import 'package:syncrow_web/utils/extension/build_context_x.dart';
import 'package:syncrow_web/web_layout/default_container.dart';

class DeviceManagementContent extends StatelessWidget {
  const DeviceManagementContent({
    super.key,
    required this.device,
    required this.subSpaces,
    required this.deviceInfo,
    required this.deviceManagementBloc,
  });

  final AllDevicesModel device;
  final List<SubSpaceModel> subSpaces;
  final DeviceInfoModel deviceInfo;
  final DeviceManagementBloc deviceManagementBloc;


  @override
  Widget build(BuildContext context) {
    Widget infoRow(
        {required String label,
        required String value,
        Widget? trailing,
        required Color? valueColor}) {
      return Padding(
        padding: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 10),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              label,
              style: context.theme.textTheme.bodyMedium!.copyWith(
                fontSize: 14,
                color: ColorsManager.grayColor,
              ),
            ),
            const SizedBox(width: 15),
            Expanded(
              child: Text(
                value,
                textAlign: TextAlign.end,
                style: context.theme.textTheme.bodyMedium!
                    .copyWith(fontSize: 14, color: valueColor),
                overflow: TextOverflow.ellipsis,
              ),
            ),
            const SizedBox(width: 12),
            trailing ?? const SizedBox.shrink(),
          ],
        ),
      );
    }

    return DefaultContainer(
      padding: EdgeInsets.zero,
      child: Column(
        children: [
          const SizedBox(height: 5),
          Padding(
            padding: const EdgeInsets.all(10.0),
            child: InkWell(
              onTap: () async {
                final selectedSubSpace = await showSubSpaceDialog(
                  context,
                  communityUuid: device.community!.uuid!,
                  spaceUuid: device.spaces!.first.uuid!,
                  subSpaces: subSpaces,
                  selected: deviceInfo.subspace.uuid,
                );
                if (selectedSubSpace != null) {
                  Future.delayed(const Duration(milliseconds: 500), () {
                    context.read<SettingDeviceBloc>().add(
                          SettingBlocAssignRoom(
                            communityUuid: device.community!.uuid!,
                            spaceUuid: device.spaces!.first.uuid!,
                            subSpaceUuid: selectedSubSpace.id ?? '',
                          ),
                        );
                  });

                  deviceManagementBloc.add(UpdateSubSpaceName(
                      subspaceId: selectedSubSpace.id!,
                      deviceId: device.uuid!,
                      newSubSpaceName: selectedSubSpace.name ?? ''));
                }
              },
              child: infoRow(
                  label: 'Sub-Space:',
                  value: deviceInfo.subspace.subspaceName,
                  valueColor: ColorsManager.blackColor,
                  trailing: SvgPicture.asset(
                    Assets.arrowDown,
                    width: 10,
                    height: 10,
                    color: ColorsManager.greyColor,
                  )),
            ),
          ),
          const Divider(color: ColorsManager.dividerColor),
          Padding(
            padding: const EdgeInsets.all(10.0),
            child: infoRow(
              label: 'Virtual Address:',
              value: deviceInfo.productUuid,
              valueColor: ColorsManager.blackColor,
              trailing: InkWell(
                onTap: () {
                  Clipboard.setData(
                    ClipboardData(text: device.productUuid ?? ''),
                  );
                  ScaffoldMessenger.of(context).showSnackBar(
                    const SnackBar(
                      content: Text('Virtual Address copied to clipboard'),
                    ),
                  );
                },
                child: const Icon(
                  Icons.copy,
                  size: 15,
                  color: ColorsManager.greyColor,
                ),
              ),
            ),
          ),
          const Divider(color: ColorsManager.dividerColor),
          Padding(
            padding: const EdgeInsets.all(10.0),
            child: infoRow(
              label: 'MAC Address:',
              valueColor: ColorsManager.blackColor,
              value: deviceInfo.macAddress,
            ),
          ),
          const SizedBox(height: 5),
        ],
      ),
    );
  }
}
