import 'package:flutter/material.dart';
import 'package:syncrow_web/pages/device_managment/all_devices/helper/route_controls_based_code.dart';
import 'package:syncrow_web/pages/device_managment/all_devices/models/devices_model.dart';
import 'package:syncrow_web/pages/device_managment/shared/device_batch_control_dialog.dart';
import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/format_date_time.dart';

class DeviceControlDialog extends StatelessWidget with RouteControlsBasedCode {
  final AllDevicesModel device;

  const DeviceControlDialog({super.key, required this.device});

  @override
  Widget build(BuildContext context) {
    return Dialog(
      backgroundColor: Colors.white,
      insetPadding: const EdgeInsets.all(20),
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(20),
      ),
      child: SizedBox(
        width: 798,
        //height: context.screenHeight * 0.7,
        child: SingleChildScrollView(
          child: Padding(
            padding: const EdgeInsets.all(20.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    const SizedBox(),
                    Text(
                      getBatchDialogName(device),
                      style: TextStyle(
                        fontWeight: FontWeight.bold,
                        fontSize: 22,
                        color: ColorsManager.dialogBlueTitle,
                      ),
                    ),
                    Container(
                      width: 25,
                      decoration: BoxDecoration(
                        color: Colors.transparent,
                        shape: BoxShape.circle,
                        border: Border.all(
                          color: Colors.grey,
                          width: 1.0,
                        ),
                      ),
                      child: IconButton(
                        padding: EdgeInsets.all(1),
                        icon: const Icon(
                          Icons.close,
                          color: Colors.grey,
                          size: 18,
                        ),
                        onPressed: () {
                          Navigator.of(context).pop();
                        },
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 20),
                _buildDeviceInfoSection(),
                //const SizedBox(height: 20),
                //// BUILD DEVICE CONTROLS
                ///
                //// ROUTE TO SPECIFIC CONTROL VIEW BASED ON DEVICE CATEGORY
                routeControlsWidgets(device: device),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildDeviceInfoSection() {
    final isOnlineDevice = device.online != null && device.online!;
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 25, horizontal: 50),
      child: Table(
        children: [
          TableRow(
            children: [
              _buildInfoRow('Product Name:', device.productName ?? 'N/A'),
              _buildInfoRow('Device ID:', device.uuid ?? ''),
            ],
          ),
          TableRow(children: [
            _buildInfoRow('Virtual Address:', device.ip ?? '-'),
            const SizedBox.shrink(),
          ]),
          TableRow(
            children: [
              _buildInfoRow('Space Name:',
                  device.spaces?.firstOrNull?.spaceName ?? 'N/A'),
              _buildInfoRow(
                  'Sub space:', device.subspace?.subspaceName ?? 'N/A'),
            ],
          ),
          TableRow(
            children: [
              _buildInfoRow(
                'Installation Date and Time:',
                formatDateTime(
                  DateTime.fromMillisecondsSinceEpoch(
                    (device.createTime ?? 0) * 1000,
                  ),
                ),
              ),
              _buildInfoRow(
                'Battery Level:',
                device.batteryLevel != null
                    ? '${device.batteryLevel ?? 0}%'
                    : "-",
                statusColor: device.batteryLevel != null
                    ? (device.batteryLevel! < 20
                        ? ColorsManager.red
                        : ColorsManager.green)
                    : null,
              ),
            ],
          ),
          TableRow(
            children: [
              _buildInfoRow(
                'Status:',
                isOnlineDevice ? 'Online' : 'offline',
                statusColor: isOnlineDevice ? Colors.green : Colors.red,
              ),
              _buildInfoRow(
                'Last Offline Date and Time:',
                formatDateTime(
                  DateTime.fromMillisecondsSinceEpoch(
                    (device.updateTime ?? 0) * 1000,
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildInfoRow(String title, String value, {Color? statusColor}) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 5.0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            title,
            style: const TextStyle(
              fontWeight: FontWeight.normal,
              fontSize: 12,
              color: ColorsManager.lightGreyColor,
            ),
          ),
          const SizedBox(width: 10),
          SelectableText(
            value,
            style: TextStyle(
              fontSize: 16,
              color: statusColor ?? Colors.black,
            ),
          ),
        ],
      ),
    );
  }
}
