import 'dart:convert';
import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/pages/main/home/widgets/dialog_page.dart';
import 'package:get/get.dart';
import '../index.dart';

/// 消息列表
///
/// FOverlayContainer 用于显示在屏幕上的浮动窗口
class NotificationListConatiner extends GetView<NotificationListController> {
  NotificationListConatiner({Key? key, required this.onOpenNotification})
      : super(key: key);
  final ValueCallback<MessageExtendInfoDTO> onOpenNotification;

  @override
  Widget build(BuildContext context) {
    return FisBuilder<NotificationListController>(
      id: "notification_list",
      builder: (_) {
        return Obx(
          () {
            return FOverlayContainer(
              child: FContainer(
                margin: EdgeInsets.only(
                  left: 10,
                  right: 10,
                  bottom: 10,
                ),
                decoration: BoxDecoration(
                  color: Colors.grey[200],
                  borderRadius: BorderRadius.all(Radius.circular(5)),
                  border: Border.all(
                    color: Colors.grey.withOpacity(0.5),
                    width: 1,
                  ),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.black.withOpacity(0.2),
                      offset: Offset(0, 2),
                      blurRadius: 4,
                    ),
                  ],
                ),
                child: FColumn(
                  children: [
                    _buildHead(),
                    if (controller.state.notificationList.isEmpty)
                      FContainer(
                        padding: EdgeInsets.all(20),
                        child: FText(i18nBook.common.noData.t),
                      ),
                    if (controller.state.notificationList.isNotEmpty)
                      FExpanded(
                        child: FListView(
                          shrinkWrap: true,
                          padding: EdgeInsets.all(0),
                          controller:
                              controller.notificationListScrollController,
                          children: controller.state.notificationList
                              .map(
                                (e) => _buildNotificationCard(e),
                              )
                              .toList(),
                        ),
                      ),
                  ],
                ),
              ),
            );
          },
        );
      },
    );
  }

  /// 消息列表头部
  FWidget _buildHead() {
    return FContainer(
      padding: EdgeInsets.only(
        left: 10,
        right: 10,
        top: 10,
        bottom: 10,
      ),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(5),
      ),
      child: FRow(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.end,
        children: [
          FText(
            i18nBook.notification.notification.t,
            style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600),
          ),
          FInkWell(
            onTap: controller.setAllReaded,
            child: Text(
              i18nBook.notification.markAllAsRead.t,
              style: TextStyle(
                fontSize: 14,
                color: Colors.grey,
              ),
            ),
          ),
        ],
      ),
    );
  }

  /// 消息卡片
  FWidget _buildNotificationCard(MessageExtendInfoDTO message) {
    return FInkWell(
      onTap: message.relevanceCode == null
          ? null
          : () {
              onOpenNotification.call(message);
            },
      child: FContainer(
        margin: EdgeInsets.only(
          left: 10,
          right: 10,
          top: 5,
          bottom: 5,
        ),
        padding: EdgeInsets.only(
          left: 10,
          right: 10,
          top: 5,
          bottom: 5,
        ),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(5),
        ),
        child: FColumn(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            FRow(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                FText(
                  _notificationTypeMapping(message.notificationType),
                  style: TextStyle(
                    fontSize: 15,
                    fontWeight: FontWeight.w500,
                    color: Colors.black54,
                  ),
                ),
                FText(
                  '${message.isReaded ? i18nBook.notification.readed.t : i18nBook.notification.unread.t}',
                  style: TextStyle(
                    fontSize: 12,
                    color: message.isReaded ? Colors.grey : Colors.blue,
                  ),
                ),
              ],
            ),
            FSizedBox(height: 3),
            FText(
              _notificationContentMapping(
                  message.notificationType, message.content!),
              style: TextStyle(
                fontSize: 14,
                color: Colors.black87,
              ),
            ),
            FSizedBox(height: 5),
            FRow(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                FText(
                  '${message.notifyTime!.toLocal().toString().substring(0, 16)}',
                  style: TextStyle(
                    fontSize: 12,
                    color: Colors.grey,
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  /// 通知类型映射
  String _notificationTypeMapping(NotificationTypeEnum type) {
    switch (type) {
      case NotificationTypeEnum.RejectApplyConsultationNotification:
        return i18nBook.notification.consultationNotification.t;
      case NotificationTypeEnum.ApplyConsultationNotification:
        return i18nBook.notification.consultationNotification.t;
      case NotificationTypeEnum.InviteeConsultationNotification:
        return i18nBook.notification.consultationNotification.t;
      case NotificationTypeEnum.InviteeRejectApplyConsultationNotification:
        return i18nBook.notification.consultationNotification.t;
      default:
        return '${i18nBook.notification.unknownType.t} ${type.index}';
    }
  }

  /// 通知内容映射
  String _notificationContentMapping(
      NotificationTypeEnum type, String contentJson) {
    if (contentJson.isEmpty) {
      return '';
    }
    switch (type) {
      case NotificationTypeEnum
          .RejectApplyConsultationNotification: // 会诊申请被拒绝通知
        InviteeRejectApplyConsultationNotification message =
            InviteeRejectApplyConsultationNotification.fromJson(
                json.decode(contentJson));
        return i18nBook.notification.someoneRejectYourConsultation
            .translate(["${message.operatorName ?? ""}"]);
      case NotificationTypeEnum.InviteeConsultationNotification: // 会诊受邀通知
        InviteeConsultationNotification message =
            InviteeConsultationNotification.fromJson(json.decode(contentJson));
        return i18nBook.notification.receiveConsultationInvitation4Mobile
            .translate(["${message.operatorName ?? ""}"]);
      case NotificationTypeEnum
          .InviteeRejectApplyConsultationNotification: // 会诊邀请被拒
        InviteeRejectApplyConsultationNotification message =
            InviteeRejectApplyConsultationNotification.fromJson(
                json.decode(contentJson));
        return i18nBook.notification.someoneRejectYourInvitation
            .translate(["${message.operatorName ?? ""}"]);
      case NotificationTypeEnum.ApplyConsultationNotification: // 会诊申请通知
        ApplyConsultationNotification message =
            ApplyConsultationNotification.fromJson(json.decode(contentJson));
        return i18nBook.notification.receiveConsultationApply4Mobile
            .translate(["${message.operatorName ?? ""}"]);
      default:
        return '${i18nBook.notification.unknownType.t} ${type.index}';
    }
  }
}
