import 'dart:async';

import 'package:application/service/map_watch.dart';
import 'package:common_pub/ui/map_view/view_map_cp.dart';
import 'package:get/get.dart';
import '../home_controller.dart';
export 'package:application/service/map_watch.dart';

class FieldControlController extends GetxController {


  @override
  void onInit() {
    super.onInit();
    final map = MapWatchService.instance;
    if (map != null) {
      activeList.bindStream(map.activeList.stream);
    }

    _subActive = activeList.listen((p0) {
      final user  = focusUser;
      if(user != null){
        viewCpController.setCPList(user.cpList);
      }
    });

  }


  @override
  void onClose() {
    super.onClose();
    _subActive?.cancel();
  }
  final viewCpController = ViewMapCPController();
  StreamSubscription<List<ActiveInfo>>? _subActive;
  HomeController get _home => Get.find();
  MapWatchService? get mapWatch => MapWatchService.instance;
  final activeList = <ActiveInfo>[].obs;
  final Rx<int?> focusUserId = Rx(null);

  UserInfo? findFocusUser( List<ActiveInfo> list){
    if(focusUserId.value== null){
      return null;
    }

    for(final act in list){
      for(final user in act.userList){
        if(user.id == focusUserId.value){
          return user;
        }
      }
    }
    return null;
  }
  UserInfo? get focusUser{
    return findFocusUser(activeList);
  }
}
