import 'package:fis_common/helpers/color.dart';
import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/services/index.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/rpc.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:flyinsono/pages/user/role_authentication/types.dart';

import 'index.dart';

class RoleAuthenticationController extends FControllerBase {
  final state = RoleAuthenticationState();

  @override
  void onInit() {
    super.onInit();
  }

  /// 加载角色集合
  Future<void> loadRoles() async {
    final roles = await rpc.role.findAuthenticationRolesAsync(
      FindAuthenticationRolesRequest(
        token: Store.user.token,
        language: i18nBook.locale.toCodeString('-'),
      ),
    );
    state.roles = roles.map(
      (x) {
        final descList = x.description!.split('\n');
        return UserRole(
          roleCode: x.roleCode!,
          startColor: FColorHelper.hex2Color(x.colorStart!, hasTag: false)!,
          endColor: FColorHelper.hex2Color(x.colorEnd!, hasTag: false)!,
          icon: FIcon(FIcons.account, color: Color(0xffc3a873), size: 44),
          iconUrl: x.iConUrl!,
          roleName: x.roleName!,
          descriptionLines: descList,
        );
      },
    ).toList();
  }
}
