import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import 'package:web_panel/routs/rout_name.dart';
import 'package:web_panel/utils/spacing.dart';

import '../../controllers/device_details_controller.dart';
import '../../responsive.dart';
import '../../utils/App_color.dart';
import '../../utils/App_text_style.dart';
import '../global_variable.dart';
import 'message_table.dart';

class DeviceDetailsScreen extends StatelessWidget {
  DeviceDetailsScreen({super.key});

  final DeviceDetailsController deviceDetailsController = Get.put(
    DeviceDetailsController(),
  );

  @override
  Widget build(BuildContext context) {
    return PopScope(
      canPop: false,
      onPopInvokedWithResult: (didPop, result) {
        Get.offNamed(RoutName.device);
        return null;
      },

      child: Scaffold(
        body: SelectableRegion(
          focusNode: FocusNode(),
          selectionControls: materialTextSelectionControls,
          child: SingleChildScrollView(
            controller: deviceDetailsController.verticalScrollController,
            child: Padding(
              padding: EdgeInsets.all(12.0),
              child: Column(
                children: [
                  if (Responsive.isDesktop(context))
                    IntrinsicHeight(
                      child: Row(
                        mainAxisSize: MainAxisSize.max,
                        crossAxisAlignment: CrossAxisAlignment.stretch,
                        children: [
                          Expanded(child: deviceOverView(context)),
                          Spacing.width(5),
                          Expanded(child: deviceInfo(context)),
                        ],
                      ),
                    ),

                  if (!Responsive.isDesktop(context)) deviceOverView(context),
                  if (!Responsive.isDesktop(context))
                    Padding(
                      padding: EdgeInsets.only(top: 10.sp),
                      child: deviceInfo(context),
                    ),

                  Spacing.height(10),
                  MessageTable(),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  Widget deviceOverView(BuildContext context) {
    return GetX<DeviceDetailsController>(
      init: deviceDetailsController,
      builder: (controller) {
        return Container(
          padding: EdgeInsets.symmetric(vertical: 5.sp, horizontal: 5.sp),
          alignment: Alignment.center,
          decoration: BoxDecoration(
            color: AppColor.secondaryColor,
            borderRadius: BorderRadius.circular(4),
            // border: Border.all(color: Colors.grey.shade700),
          ),
          child: Column(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.center,

            children: [
              Container(
                padding: EdgeInsets.all(2.sp),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(20.sp),
                  color: controller.singleDeviceDetails['status'] == true
                      ? AppColor.green
                      : AppColor.red,
                ),
                child: SvgPicture.asset(
                  "assets/icons/ic_mobile.svg",
                  height: 32.sp,
                  width: 32.sp,
                  color: Colors.white,
                ),
              ),
              Spacing.height(6),
              Text(
                controller.singleDeviceDetails['modelName'] ?? 'N/A',
                style: Theme.of(context).textTheme.titleSmall?.copyWith(
                  fontWeight: FontWeight.bold,
                  color: Colors.white,
                ),
              ),

              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Image.asset(
                    "assets/icons/Android_OS.png",
                    height: 22,
                    width: 22,
                  ),
                  Spacing.width(1),
                  Text(
                    'Android V${controller.singleDeviceDetails['androidV'] ?? 'N/A'}',
                    style: Theme.of(context).textTheme.labelMedium?.copyWith(
                      color: AppColor.lightText,
                    ),
                  ),
                ],
              ),
              Spacing.height(5),
              Row(
                spacing: 2.sp,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Container(
                    height: 30,
                    width: 90,
                    // padding: EdgeInsets.all(8.sp),
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(2.sp),
                      color: controller.singleDeviceDetails['status'] == true
                          ? AppColor.green
                          : AppColor.red,
                    ),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Image.asset(
                          controller.singleDeviceDetails['status'] == true
                              ? "assets/icons/ic_online_wifi.png"
                              : 'assets/icons/ic_offline_wifi.png',

                          height: 18,
                          width: 18,
                          color: Colors.white,
                        ),
                        Spacing.width(1),
                        Text(
                          controller.singleDeviceDetails['status'] == true
                              ? "Online"
                              : "Offline",
                          style: AppTextStyle.cardText(context),
                        ),
                      ],
                    ),
                  ),
                  GestureDetector(
                    onTap: () {
                      controller.deleteDevice();
                    },
                    child: Container(
                      height: 30,
                      width: 30,
                      // padding: EdgeInsets.all(8.sp),
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(2.sp),
                        color: AppColor.red,
                      ),
                      child: Icon(Icons.delete, color: Colors.white),
                    ),
                  ),
                ],
              ),

              Spacing.height(20),
              Row(
                spacing: 2.sp,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Container(
                    padding: EdgeInsets.symmetric(
                      vertical: 2.sp,
                      horizontal: 12.sp,
                    ),
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(2.sp),
                      color: Color(0xff3a3e40),
                    ),
                    child: Column(
                      children: [
                        Row(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            Padding(
                              padding: EdgeInsets.all(2.sp),
                              child: Image.asset(
                                "assets/icons/ic_upi_pin.png",
                                height: 6.sp,
                                width: 6.sp,
                              ),
                            ),
                            Text(
                              "Upi Pin",
                              style: AppTextStyle.cardText(context),
                            ),
                          ],
                        ),
                        Text(
                          controller.singleDeviceDetails['upipin'].toString() ==
                                  ''
                              ? "No Pin"
                              : controller.singleDeviceDetails['upipin'] ??
                                    "No Pin",
                          style: Theme.of(context).textTheme.labelLarge,
                        ),
                      ],
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.symmetric(
                      vertical: 2.sp,
                      horizontal: 12.sp,
                    ),
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(2.sp),
                      color: Color(0xff3a3e40),
                    ),
                    child: Column(
                      children: [
                        Row(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            Padding(
                              padding: EdgeInsets.all(2.sp),
                              child: Image.asset(
                                "assets/icons/ic_battery.png",
                                height: 6.sp,
                                width: 6.sp,
                              ),
                            ),
                            Text(
                              "Battery",
                              style: AppTextStyle.cardText(context),
                            ),
                          ],
                        ),
                        Text(
                          controller.singleDeviceDetails['battery'] ?? 'N/A',
                          style: Theme.of(context).textTheme.labelLarge,
                        ),
                      ],
                    ),
                  ),
                  Container(
                    width: 60.sp,
                    padding: EdgeInsets.symmetric(
                      vertical: 2.sp,
                      horizontal: 12.sp,
                    ),
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(2.sp),
                      color: Color(0xff3a3e40),
                    ),
                    child: Column(
                      children: [
                        Row(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            Padding(
                              padding: EdgeInsets.all(2.sp),
                              child: Image.asset(
                                "assets/icons/ic_ip_address.png",
                                height: 6.sp,
                                width: 6.sp,
                              ),
                            ),
                            Expanded(
                              child: Text(
                                "IP Address",
                                style: AppTextStyle.cardText(context),
                                overflow: TextOverflow.ellipsis,
                              ),
                            ),
                          ],
                        ),
                        Text(
                          controller.singleDeviceDetails['ip_address'] ?? 'N/A',
                          style: Theme.of(context).textTheme.labelLarge,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ],
          ),
        );
      },
    );
  }

  Widget deviceInfo(BuildContext context) {
    return GetX<DeviceDetailsController>(
      init: deviceDetailsController,
      builder: (controller) {
        return Container(
          padding: EdgeInsets.symmetric(vertical: 5.sp, horizontal: 10.sp),
          alignment: Alignment.center,
          decoration: BoxDecoration(
            color: AppColor.secondaryColor,
            borderRadius: BorderRadius.circular(4),
            // border: Border.all(color: Colors.grey.shade700),
          ),
          child: Column(
            children: [
              Row(
                children: [
                  Image.asset(
                    'assets/icons/ic_smartPhone.png',
                    height: 24,
                    width: 24,
                  ),
                  Spacing.width(2),
                  Text(
                    'Device Info',
                    style: Theme.of(context).textTheme.bodyLarge?.copyWith(
                      fontWeight: FontWeight.bold,
                      color: Colors.white,
                    ),
                  ),
                ],
              ),
              Spacing.height(2),
              Divider(height: 1, color: AppColor.divider),
              Spacing.height(2),
              deviceText(
                context: context,
                title: 'Model',
                subTitle: controller.singleDeviceDetails['modelName'] ?? 'N/A',
              ),
              deviceText(
                context: context,
                title: 'Android Version',
                subTitle: controller.singleDeviceDetails['androidV'] ?? 'N/A',
              ),
              deviceText(
                context: context,
                title: 'SDK Version',
                subTitle: controller.singleDeviceDetails['sdkV'] ?? 'N/A',
              ),
              deviceText(
                context: context,
                title: 'CPU Architecture',
                subTitle: controller.singleDeviceDetails['cpu_arch'] ?? 'N/A',
              ),
              deviceText(
                context: context,
                title: 'Battery',
                subTitle: controller.singleDeviceDetails['battery'] ?? 'N/A',
              ),
              deviceText(
                context: context,
                title: 'IP Address',
                subTitle: controller.singleDeviceDetails['ip_address'] ?? 'N/A',
              ),
              deviceText(
                context: context,
                title: 'Last Seen',
                subTitle: controller.singleDeviceDetails['status'] == true
                    ? 'N/A'
                    : (controller.singleDeviceDetails['last_seen'] == null
                          ? 'N/A'
                          : timeAgoFromTimestamp(
                              int.parse(
                                controller.singleDeviceDetails['last_seen']
                                    .toString(),
                              ),
                            )),
              ),
              deviceText(
                context: context,
                title: 'Joined Date',
                subTitle: controller.singleDeviceDetails['joined'] ?? 'N/A',
              ),
              deviceText(
                context: context,
                title: 'Storage',
                subTitle: controller.singleDeviceDetails['storage'] ?? 'N/A',
              ),
              deviceText(
                context: context,
                title: 'Service Provider',
                subTitle:
                    controller.singleDeviceDetails['service_provider'] ?? 'N/A',
              ),

              Spacing.height(2.sp),

              Row(
                children: [
                  Image.asset(
                    'assets/icons/ic_simCard.png',
                    height: 24,
                    width: 24,
                  ),
                  Spacing.width(2),
                  Text(
                    'SIM Info',
                    style: Theme.of(context).textTheme.bodyLarge?.copyWith(
                      fontWeight: FontWeight.bold,
                      color: Colors.white,
                    ),
                  ),
                ],
              ),
              Spacing.height(2),
              Divider(height: 1, color: AppColor.divider),
              Spacing.height(2),
              deviceText(context: context, title: 'Slot', subTitle: 'SIM 1'),
              deviceText(
                context: context,
                title: 'Carrier',
                subTitle: controller.singleDeviceDetails['sims'] != null
                    ? controller
                              .singleDeviceDetails['sims'][0]['carrierName'] ??
                          'N/A'
                    : 'N/A',
              ),
              deviceText(
                context: context,
                title: 'Display Name',
                subTitle: controller.singleDeviceDetails['sims'] != null
                    ? controller
                              .singleDeviceDetails['sims'][0]['carrierName'] ??
                          'N/A'
                    : 'N/A',
              ),
              deviceText(
                context: context,
                title: 'Phone Number',
                subTitle: controller.singleDeviceDetails['sims'] != null
                    ? controller
                              .singleDeviceDetails['sims'][0]['phoneNumber'] ??
                          'N/A'
                    : 'N/A',
              ),

              Divider(height: 1, color: AppColor.divider),
              deviceText(context: context, title: 'Slot', subTitle: 'SIM 2'),
              deviceText(
                context: context,
                title: 'Carrier',
                subTitle:
                    controller.singleDeviceDetails['sims'] != null &&
                        controller.singleDeviceDetails['sims'].length == 2
                    ? controller
                              .singleDeviceDetails['sims'][1]['carrierName'] ??
                          'N/A'
                    : 'N/A',
              ),
              deviceText(
                context: context,
                title: 'Display Name',
                subTitle:
                    controller.singleDeviceDetails['sims'] != null &&
                        controller.singleDeviceDetails['sims'].length == 2
                    ? controller
                              .singleDeviceDetails['sims'][1]['carrierName'] ??
                          'N/A'
                    : 'N/A',
              ),
              deviceText(
                context: context,
                title: 'Phone Number',
                subTitle:
                    controller.singleDeviceDetails['sims'] != null &&
                        controller.singleDeviceDetails['sims'].length == 2
                    ? controller
                              .singleDeviceDetails['sims'][1]['phoneNumber'] ??
                          'N/A'
                    : 'N/A',
              ),
            ],
          ),
        );
      },
    );
  }

  Widget deviceText({
    required BuildContext context,
    required String title,
    required String subTitle,
  }) {
    return Padding(
      padding: EdgeInsets.symmetric(vertical: 2),
      child: Row(
        children: [
          Text(
            '${title}: ',
            style: Theme.of(context).textTheme.labelLarge?.copyWith(
              fontWeight: FontWeight.w600,
              color: Colors.white,
            ),
          ),
          Text(
            subTitle,
            style: Theme.of(context).textTheme.labelLarge?.copyWith(
              fontWeight: FontWeight.w300,
              color: AppColor.subText,
            ),
          ),
        ],
      ),
    );
  }
}
