import 'mock/monitoring_mock_data.dart';
import 'package:laki_icu_app/models/bo/monitoring_bo.dart';
import 'package:laki_icu_app/utils/constants.dart';
import 'package:laki_icu_app/utils/http/dio_request.dart';
import 'package:laki_icu_app/utils/http/response_model.dart';

class MonitoringRepository {
  MonitoringRepository._();

  static final MonitoringRepository instance = MonitoringRepository._();

  Future<ResponseModel<List<MonitoringMetricBO>>> getMetrics() {
    if (Constants.useMockData) {
      return _mockMetrics();
    }
    return DioRequest.instance.get<List<MonitoringMetricBO>>(
      '/app/monitoring/metrics',
      fromJsonT: (data) {
        final list = data as List<dynamic>;
        return list
            .map((e) => MonitoringMetricBO.fromJson(e as Map<String, dynamic>))
            .toList();
      },
    );
  }

  Future<ResponseModel<PatientInfoBO>> getPatientInfo() {
    if (Constants.useMockData) {
      return _mockPatientInfo();
    }
    return DioRequest.instance.get<PatientInfoBO>(
      '/app/monitoring/patient-info',
      fromJsonT: (data) => PatientInfoBO.fromJson(data as Map<String, dynamic>),
    );
  }

  Future<ResponseModel<List<AlertInfoBO>>> getAlerts() {
    if (Constants.useMockData) {
      return _mockAlerts();
    }
    return DioRequest.instance.get<List<AlertInfoBO>>(
      '/app/monitoring/alerts',
      fromJsonT: (data) {
        final list = data as List<dynamic>;
        return list
            .map((e) => AlertInfoBO.fromJson(e as Map<String, dynamic>))
            .toList();
      },
    );
  }

  Future<ResponseModel<List<MonitoringMenuItemBO>>> getMenuItems() {
    if (Constants.useMockData) {
      return _mockMenuItems();
    }
    return DioRequest.instance.get<List<MonitoringMenuItemBO>>(
      '/app/monitoring/menu-items',
      fromJsonT: (data) {
        final list = data as List<dynamic>;
        return list
            .map(
                (e) => MonitoringMenuItemBO.fromJson(e as Map<String, dynamic>))
            .toList();
      },
    );
  }

  Future<ResponseModel<List<MonitoringMetricBO>>> _mockMetrics() async {
    await Future.delayed(const Duration(milliseconds: 300));
    return ResponseModel<List<MonitoringMetricBO>>(
      code: 200,
      msg: '获取监护指标成功（mock）',
      data: MonitoringMockData.getMockMetrics(),
      uuid: 'mock_uuid_${DateTime.now().millisecondsSinceEpoch}',
      success: true,
      timestamp: DateTime.now().millisecondsSinceEpoch,
    );
  }

  Future<ResponseModel<PatientInfoBO>> _mockPatientInfo() async {
    await Future.delayed(const Duration(milliseconds: 300));
    return ResponseModel<PatientInfoBO>(
      code: 200,
      msg: '获取患者信息成功（mock）',
      data: MonitoringMockData.getMockPatientInfo(),
      uuid: 'mock_uuid_${DateTime.now().millisecondsSinceEpoch}',
      success: true,
      timestamp: DateTime.now().millisecondsSinceEpoch,
    );
  }

  Future<ResponseModel<List<AlertInfoBO>>> _mockAlerts() async {
    await Future.delayed(const Duration(milliseconds: 300));
    return ResponseModel<List<AlertInfoBO>>(
      code: 200,
      msg: '获取告警信息成功（mock）',
      data: MonitoringMockData.getMockAlerts(),
      uuid: 'mock_uuid_${DateTime.now().millisecondsSinceEpoch}',
      success: true,
      timestamp: DateTime.now().millisecondsSinceEpoch,
    );
  }

  Future<ResponseModel<List<MonitoringMenuItemBO>>> _mockMenuItems() async {
    await Future.delayed(const Duration(milliseconds: 300));
    return ResponseModel<List<MonitoringMenuItemBO>>(
      code: 200,
      msg: '获取设备菜单成功（mock）',
      data: MonitoringMockData.getMockMenuItems(),
      uuid: 'mock_uuid_${DateTime.now().millisecondsSinceEpoch}',
      success: true,
      timestamp: DateTime.now().millisecondsSinceEpoch,
    );
  }
}
