import 'package:fis_common/logger/logger.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:flyinsono/managers/interfaces/entities/consultation_member.dart';
import 'package:flyinsono/rpc.dart';
import 'package:flyinsono/views/desktop/real_time_consultation/live_consultation/controller.dart';
import 'package:flyinsono/views/desktop/real_time_consultation/live_consultation/state.dart';
import 'package:get/get.dart';

class WhiteboardController {
  LiveConsultationController controller;
  LiveConsulationState get state => controller.state;
  WhiteboardController(this.controller);

  /// 通过位序获取颜色，如果无位序则返回透明色
  Color getColorByIndex(int? index) {
    if (index == null) {
      return Colors.transparent;
    }
    assert(index < _patientColors.length, 'only support 8 colors');
    return _patientColors[index];
  }

  static const List<Color> _patientColors = [
    Color.fromARGB(255, 145, 223, 18),
    Color.fromARGB(255, 255, 0, 0),
    Color.fromARGB(255, 252, 212, 82),
    Color.fromARGB(255, 18, 100, 223),
    Color.fromARGB(255, 2, 213, 250),
    Color.fromARGB(255, 190, 101, 101),
    Color.fromARGB(255, 255, 192, 241),
    Color.fromARGB(255, 119, 14, 247),
  ];

  /// 当前颜色
  Color currectColor() {
    try {
      final memberLiveDatas = state.memberStatus;
      List<ConsultationMember> consultationMembers = memberLiveDatas.values
          .toList()
          .where((element) => element.memberType == LiveMemberEnum.User)
          .toList();
      ConsultationMember? currectConsultationMember = consultationMembers
          .firstWhereOrNull((element) => element.id == Store.user.code);
      int currectIndex = 0;
      if (currectConsultationMember != null) {
        currectIndex = currectConsultationMember.index ?? 0;
      }
      return _patientColors[currectIndex];
    } catch (e) {
      logger.e('WhiteboardController currectColor', e);
      return Color(0xFF1283DF);
    }
  }

  /// 发送白板数据
  Future<void> sendInteractiveBoardData(String boardData,
      [bool isClear = false]) async {
    try {
      rpc.liveConsultation.sendInteractiveBoardDataAsync(
        SendInteractiveBoardDataRequest(
          consultationCode: state.liveConsultation.consultationCode,
          token: Store.user.token,
          boardData: boardData,
          isClear: isClear,
        ),
      );
    } catch (e) {
      print(e);
    }
  }
}
