import 'package:flutter/foundation.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:flyinsono/rpc.dart';
import 'package:fis_i18n/i18n.dart';
import 'package:flyinsono/views/desktop/real_time_consultation/widgets/simple_Info.dart';
import 'base_manager.dart';
import 'interfaces/group.dart';
import 'package:fis_common/index.dart';
import 'package:fis_jsonrpc/rpc.dart';

class GroupManager extends BaseManager implements IGroupManager {
  ///获取所有群组信息
  @override
  Future<List<UserGroupDTO>> getGroups({
    String? keyword,
    int pageIndex = 1,
    int pageSize = 20,
  }) async {
    final PageResult<UserGroupDTO> userGroupPageInfos =
        await rpc.user.getUserGroupPagesAsync(GetUserGroupPagesRequest(
      token: token,
      pageIndex: pageIndex,
      pageSize: pageSize,
      keyword: keyword,
    ));
    final List<UserGroupDTO> userGroupDatas = userGroupPageInfos.pageData ?? [];

    return userGroupDatas;
  }

  ///查询群组内用户集合
  @override
  Future<PageResult<UserExtendDTO>> getGroupUserInfos({
    required String userGroupCode,
    int pageIndex = 1,
    int pageSize = 20,
    String? keyword,
  }) async {
    PageResult<UserExtendDTO> members = await rpc.user.getGroupUserInfosAsync(
      GetGroupUserInfosRequest(
        languageCode: i18nBook.locale.toCodeString('-'),
        userGroupCode: userGroupCode,
        token: Store.user.token,
        pageIndex: pageIndex,
        pageSize: pageSize,
        keyword: keyword,
      ),
    );
    return members;
  }

  @override
  Future<List<SimpleInfoItem>> getGroupUserSimpleItems({
    required String userGroupCode,
    int pageIndex = 1,
    int pageSize = 20,
    String? keyword,
  }) async {
    List<SimpleInfoItem> result = [];
    PageResult<UserExtendDTO> members = await getGroupUserInfos(
      userGroupCode: userGroupCode,
      pageIndex: pageIndex,
      pageSize: pageSize,
      keyword: keyword,
    );
    for (UserExtendDTO m in members.pageData ?? []) {
      result.add(SimpleInfoItem(
        itemCode: m.userCode!,
        name: m.displayName,
        role: m.roleName,
        image: m.headImageUrl,
      ));
    }
    return result;
  }

  ///查询群组内设备集合
  @override
  Future<PageResult<DeviceInfoDTO>> getGroupDeviceInfos({
    required String userGroupCode,
    int pageIndex = 1,
    int pageSize = 20,
    String? keyword,
  }) async {
    PageResult<DeviceInfoDTO> devices = await rpc.user.getGroupDeviceInfosAsync(
      GetGroupDeviceInfosRequest(
        languageCode: i18nBook.locale.toCodeString('-'),
        userGroupCode: userGroupCode,
        token: Store.user.token,
        pageIndex: pageIndex,
        keyword: keyword,
        pageSize: pageSize,
      ),
    );
    return devices;
  }

  ///查询群组内设备集合
  @override
  Future<List<SimpleInfoItem>> getGroupDeviceSimpleItems({
    required String userGroupCode,
    int pageIndex = 1,
    int pageSize = 20,
    String? keyword,
  }) async {
    PageResult<DeviceInfoDTO> devices = await getGroupDeviceInfos(
      userGroupCode: userGroupCode,
      pageIndex: pageIndex,
      pageSize: pageSize,
      keyword: keyword,
    );
    List<SimpleInfoItem> result = [];
    for (DeviceInfoDTO m in devices.pageData ?? []) {
      result.add(SimpleInfoItem(
        itemCode: m.deviceCode!,
        name: m.displayName,
        role: m.shortCode,
        image: m.headPicUrl,
      ));
    }
    return result;
  }
}
