import 'package:ef/ef.dart';
import 'package:flutter/material.dart';
import 'package:flutterflow_ui/flutterflow_ui.dart';
import 'package:vbvs_app/common/color/appConstants.dart';
import 'package:vbvs_app/common/color/app_uri_status.dart';
import 'package:vbvs_app/common/util/FitTool.dart';
import 'package:vbvs_app/component/tool/ClickableContainer.dart';
import 'package:vbvs_app/component/tool/CustomCard.dart';
import 'package:vbvs_app/component/tool/TopSlideNotification.dart';
import 'package:vbvs_app/controller/device/device_share_controller.dart';
import 'package:vbvs_app/controller/message/message_controller.dart';
import 'package:vbvs_app/controller/theme_controller/ThemeController.dart';
import 'package:vbvs_app/enum/MessageStatus.dart';
import 'package:vbvs_app/model/api_response.dart';
import 'package:vbvs_app/pages/device_bind/componnet/bind_dialog.dart';

class MessageWidgetWidget extends StatefulWidget {
  final data;

  const MessageWidgetWidget({super.key, required this.data});

  @override
  State<MessageWidgetWidget> createState() => _MessageWidgetWidgetState();
}

class _MessageWidgetWidgetState extends State<MessageWidgetWidget> {
  ThemeController themeController = Get.find();
  MessageController messageController = Get.find();
  DeviceShareController deviceShareController = Get.find();

  @override
  Widget build(BuildContext context) {
    var messageInfo = widget.data;
    print(messageInfo);
    return Stack(
      children: [
        ClickableContainer(
          backgroundColor: themeController.currentColor.sc5,
          highlightColor: themeController.currentColor.sc21,
          borderRadius: 20.rpx,
          padding:
              EdgeInsetsDirectional.fromSTEB(31.rpx, 33.rpx, 0.rpx, 33.rpx),
          onTap: () {},
          child: Column(
            mainAxisSize: MainAxisSize.max,
            children: [
              Container(
                width: double.infinity,
                constraints: BoxConstraints(
                  minHeight: 66.rpx,
                ),
                child: Align(
                  alignment: AlignmentDirectional(-1, 0),
                  child: Text(
                    // '实时监测结果通知'.tr,
                    "${messageInfo['data']['title']}",
                    style: TextStyle(
                      fontFamily: 'Inter',
                      fontSize: 30.rpx,
                      letterSpacing: 0.0,
                      color: themeController.currentColor.sc3,
                    ),
                  ),
                ),
              ),
              Row(
                mainAxisSize: MainAxisSize.max,
                children: [
                  Container(
                    constraints: BoxConstraints(
                      minWidth: 30.rpx,
                      maxWidth: 140.rpx,
                    ),
                    child: Column(
                      mainAxisSize: MainAxisSize.max,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: messageInfo['data']['val']
                          .map<Widget>((mapItem) =>
                              _buildInfoItem(context, mapItem['k'] ?? ''))
                          .toList(),
                    ),
                  ),
                  Container(
                    constraints: BoxConstraints(
                      minWidth: 30.rpx,
                    ),
                    child: Column(
                      mainAxisSize: MainAxisSize.max,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: messageInfo['data']['val']
                          .map<Widget>((mapItem) =>
                              _buildValueItem(context, mapItem['v'] ?? ''))
                          .toList(),
                    ),
                  ),
                ].divide(SizedBox(width: 30.rpx)),
              ),
            ],
          ),
        ),
        if (messageInfo['type'] == 'app_system')
          Positioned(
            bottom: 46.rpx,
            right: 20.rpx,
            child: Container(
              width: 123.rpx,
              height: 47.rpx,
              child: CustomCard(
                borderRadius: AppConstants().button_container_radius, // 直角
                colors: messageInfo['status'] == 1
                    ? [
                        themeController.currentColor.sc1,
                        themeController.currentColor.sc2
                      ]
                    : [themeController.currentColor.sc4], // 单色背景
                enableAnimation: true, // 有点击缩放动画
                enableGradient: false, // 不用渐变
                onTap: () {
                  if (messageInfo['status'] == 1) {
                    showConfirmDialog(context, Container(), "是否确认接受该设备".tr,
                        onConfirm: () async {
                      ApiResponse apiResponse = await deviceShareController
                          .confirmShare(messageInfo['data']['shareCode']);
                      if (apiResponse.code == HttpStatusCodes.ok) {
                        TopSlideNotification.show(
                          context,
                          text: apiResponse.msg!,
                          textColor: themeController.currentColor.sc2,
                        );
                        messageController.getMessageList();
                        messageController.updateAll();
                      } else {
                        TopSlideNotification.show(
                          context,
                          text: apiResponse.msg!,
                          textColor: themeController.currentColor.sc9,
                        );
                        messageController.getMessageList();
                        messageController.updateAll();
                      }
                    }, onCancel: () {});
                  }
                },
                child: Center(
                  child: Text(
                    getMessageStatus(messageInfo['status']),
                    style: TextStyle(
                      fontFamily: 'Inter',
                      fontSize: 26.rpx,
                      letterSpacing: 0.0,
                      color: Colors.white,
                    ),
                  ),
                ),
              ),
            ),
          ),
      ],
    );
  }

  Widget _buildInfoItem(BuildContext context, String label) {
    return Container(
      constraints: BoxConstraints(
        minHeight: 62.rpx,
      ),
      child: Align(
        alignment: AlignmentDirectional(-1, 0),
        child: Text(
          overflow: TextOverflow.ellipsis,
          maxLines: 1,
          label.tr,
          style: TextStyle(
            fontFamily: 'Inter',
            fontSize: 26.rpx,
            letterSpacing: 0.0,
            color: themeController.currentColor.sc4,
          ),
        ),
      ),
    );
  }

  Widget _buildValueItem(BuildContext context, value) {
    return Container(
      constraints: BoxConstraints(
        minHeight: 62.rpx,
      ),
      child: Align(
        alignment: AlignmentDirectional(-1, 0),
        child: Text(
          overflow: TextOverflow.ellipsis,
          maxLines: 1,
          "${value}",
          style: TextStyle(
            fontFamily: 'Inter',
            fontSize: 26.rpx,
            letterSpacing: 0.0,
            color: themeController.currentColor.sc3,
          ),
        ),
      ),
    );
  }

  String getMessageStatus(int status) {
    if (status == null) {
      return '未知数据'.tr;
    }
    if (status == MessageStatus.noNeed.code) {
      return '无需处理'.tr;
    }
    if (status == MessageStatus.pending.code) {
      return "待处理".tr;
    }
    if (status == MessageStatus.completed.code) {
      return "已处理".tr;
    }
    if (status == MessageStatus.timeout.code) {
      return "已超时".tr;
    }
    return '未知数据'.tr;
  }
}
