import 'package:get/get.dart';
import 'package:injectable/injectable.dart';
import 'package:keyboard/base/app_base_request.dart';
import 'package:keyboard/data/api/request/keyboard_character_update_request.dart';
import 'package:keyboard/data/api/request/keyboard_list_request.dart';
import 'package:keyboard/data/api/response/keyboard_prologue_list_response.dart';
import 'package:keyboard/utils/atmob_log.dart';
import 'dart:convert';
import '../../di/get_it.dart';
import '../../utils/http_handler.dart';
import '../api/atmob_api.dart';
import '../api/request/keyboard_character_list_request.dart';
import '../api/request/keyboard_update_request.dart';
import '../api/response/keyboard_character_list_response.dart';
import '../api/response/keyboard_list_response.dart';
import '../bean/keyboard_info.dart';

@lazySingleton
class KeyboardRepository {
  final tag = "KeyboardRepository";
  final AtmobApi atmobApi;

  final RxList<KeyboardInfo> _keyboardInfoList = RxList();

  RxList<KeyboardInfo> get keyboardInfoList => _keyboardInfoList;

  KeyboardRepository(this.atmobApi) {
    print('$tag....init');
    refreshKeyboardList();
  }

  Future refreshKeyboardList() async {

    return getKeyboardList().then((response) {
      _keyboardInfoList.value = response.keyboardInfos;
      print('$tag refreshKeyboardList: ${response.keyboardInfos.first.id}');
    });
  }

  // 获取键盘列表
  Future<KeyboardListResponse> getKeyboardList({String? type}) {
    return atmobApi
        .getKeyboardList(KeyboardListRequest(type: type))
        .then(HttpHandler.handle(true));
  }

  // 获取键盘人设列表
  Future<KeyboardCharacterListResponse> getKeyboardCharacterList({
    required String keyboardId,
  }) {
    return atmobApi
        .getKeyboardCharacterList(
          KeyboardCharacterListRequest(keyboardId: keyboardId),
        )
        .then(HttpHandler.handle(true));
  }

  //更新键盘人设
  Future<void> keyboardCharacterUpdate({
    required List<String> characterIds,
    required String keyboardId,
  }) {
    return atmobApi
        .keyboardCharacterUpdate(
          KeyboardCharacterUpdateRequest(
            keyboardId: keyboardId,
            characterIds: characterIds,
          ),
        )
        .then(HttpHandler.handle(true));
  }

  // 更新键盘信息
  Future<void> updateKeyboardInfo({
    required String keyboardId,
    String? name,
    String? imageUrl,
    String? birthday,
    int? intimacy,
    int? gender,
  }) {
    return atmobApi
        .keyboardUpdate(
          KeyboardUpdateRequest(
            keyboardId: keyboardId,
            name: name,
            imageUrl: imageUrl,
            birthday: birthday,
            intimacy: intimacy,
            gender: gender,
          ),
        )
        .then(HttpHandler.handle(true));
  }

  // 获取开场白列表
  Future<KeyboardPrologueListResponse> getPrologueList() {
    return atmobApi
        .getPrologueList(AppBaseRequest())
        .then(HttpHandler.handle(true));
  }

  static KeyboardRepository getInstance() => getIt.get<KeyboardRepository>();
}
