import 'package:fis_jsonrpc/rpc.dart';
import 'package:get/get.dart';
import 'package:vnoteapp/architecture/defines.dart';
import 'package:vnoteapp/managers/interfaces/patient.dart';
import 'package:vnoteapp/managers/interfaces/service_pack.dart';
import 'package:vnoteapp/pages/controllers/crowd_labels.dart';
import 'state.dart';

class ServicePackageContractController extends FControllerBase {
  final state = ServicePackageContractState();
  final _patientManager = Get.find<IPatientManager>();
  final _servicePackManager = Get.find<IServicePackManager>();
  List<LabelDTO> normalOptions = [];
  PatientDTO patient = PatientDTO();
  @override
  void onInit() {
    final p = Get.parameters;
    state.patientCode = p["patientCode"]!;
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
    _loadData();
  }

  Future<void> _loadData() async {
    final dto = await _patientManager.getDetail(state.patientCode);

    /// server 暂时没根据人群获取服务包列表这边先用全部服务包
    final result = await _servicePackManager.getServicePackList(
      ServicePackPageRequest(
        pageIndex: 1,
        pageSize: 20,
      ),
    );
    normalOptions = Get.find<CrowdLabelsController>().state.normalOptions;
    state.servicePackageItems = result?.pageData ?? [];

    final getServiceItemResult = await _servicePackManager.getServiceItemList(
      PageRequest(
        pageIndex: 1,
        pageSize: 100,
      ),
    );
    state.serviceItems = getServiceItemResult?.pageData ?? [];
    if (dto != null) {
      dto.birthday = dto.birthday!.toLocal();
      state.updateDto(dto);
      patient = dto;
    }
  }

  String setNormalLabels(List<String> normalValueList) {
    List<String> normalLabels = [];
    for (LabelDTO i in normalOptions) {
      if (normalValueList.contains(i.code)) {
        normalLabels.add(i.labelName ?? '');
      }
    }
    String labels = normalLabels.join('、');
    if (labels.isEmpty) {
      return '暂无';
    }
    return labels;
  }

  String getServiceItemsName(List<String> serviceItems) {
    List<String> serviceItemNames = [];
    for (var element in state.serviceItems) {
      if (serviceItems.contains(element.code)) {
        serviceItemNames.add(element.name ?? '');
      }
    }
    if (serviceItemNames.isEmpty) {
      return '暂无';
    }
    return serviceItemNames.join('、');
  }

  // 切换选择的服务包
  void changeServicePackage(ServicePackDTO servicePackDTO) {
    List<ServicePackDTO> selectedServicePackage = state.selectedServicePackage;
    if (state.selectedServicePackage.contains(servicePackDTO)) {
      selectedServicePackage.remove(servicePackDTO);
    } else {
      selectedServicePackage.add(servicePackDTO);
    }
    state.selectedServicePackage = selectedServicePackage;
    setSelectedServicePackages();
  }

  void setSelectedServicePackages() {
    List<String> selectedServicePackageNameList = [];
    List<String> selectedServicePackageCodeList = [];
    for (var element in state.selectedServicePackage) {
      selectedServicePackageNameList.add(element.name ?? "");
      selectedServicePackageCodeList.add(element.code ?? "");
    }
    state.selectedServicePackageName = selectedServicePackageNameList.join('、');
    state.selectedServicePackageCode = selectedServicePackageCodeList.join('、');
  }
}
