
import 'dart:convert';
import 'dart:developer';

import 'package:device_info_plus/device_info_plus.dart';
import 'package:http/http.dart' as http;
import 'package:package_info/package_info.dart';

import '../../../../../model/accountData.dart';
import '../../../mhslApi.dart';
import 'updateUserIndexParams.dart';

class UpdateUserIndex extends MhslApi<void> {
  UpdateUserIndexParams params;
  UpdateUserIndex(this.params) : super('server/userIndex/update');

  @override
  void assemble(String raw) {}

  @override
  Future<http.Response> request(Uri uri) {
    var data = jsonEncode(params.toJson());
    log('Updating userindex:\n $data');
    return http.post(uri, body: data);
  }

  static Future<void> index() async {
    UpdateUserIndex(
      UpdateUserIndexParams(
        username: AccountData().getUsername(),
        user: AccountData().getUserSecret(),
        device: await AccountData().getDeviceId(),
        appVersion: int.parse((await PackageInfo.fromPlatform()).buildNumber),
        deviceInfo: jsonEncode((await DeviceInfoPlugin().deviceInfo).data).toString(),
      ),
    ).run();
  }
}
