import 'dart:convert';

import 'package:flyinsono/lab/manager/interfaces/login.dart';
import 'package:flyinsono/rpc.dart';
import 'package:fis_jsonrpc/rpc.dart';

import 'package:fis_common/logger/logger.dart';
import 'package:fis_i18n/i18n.dart';

class LogInManager extends ILogInManager {
  @override
  Future<LoginResult?> commonLoginAsync({
    required String username,
    required String password,
    required Platform platform, // 平台
    required LoginSource loginSource,
  }) async {
    try {
      LoginResult result = await rpc.login.commonLoginAsync(
        CommonLoginRequest(
          anyAccount: username,
          password: password,
          platform: platform,
          loginSource: loginSource,
          languageCode: i18nBook.locale.languageCode,
        ),
      );
      return result;
    } catch (e) {
      logger.e('LogIn commonLoginAsync ex:', e);

      return null;
    }
  }

  /// dongle登录
  Future<LoginResult?> dongleCommonLoginAsync({
    required String anyAccount,
    required String labDongle,
    required String anyCode, // 平台
    required LoginSource loginSource,
  }) async {
    try {
      var request = CommonLoginRequest(
        anyAccount: anyAccount,
        labDongle: labDongle,
        anyCode: anyCode,
        loginSource: loginSource,
        languageCode: i18nBook.locale.languageCode,
      );
      logger
          .i('LogIn commonLoginAsync request:${jsonEncode(request.toJson())}:');
      print(jsonEncode(request.toJson()));
      LoginResult result = await rpc.login.commonLoginAsync(request);
      return result;
    } catch (e) {
      logger.e('LogIn commonLoginAsync ex:', e);

      return null;
    }
  }
}
