import 'package:injectable/injectable.dart';

import 'package:get/get.dart';
import 'package:keyboard/data/api/request/character_custom_update_request.dart';
import 'package:keyboard/data/api/response/character_add_response.dart';
import 'package:keyboard/data/api/response/character_custom_page_response.dart';
import 'package:keyboard/data/api/response/character_custom_update_response.dart';
import 'package:keyboard/data/api/response/character_unlock_response.dart';
import 'package:keyboard/data/api/response/new_user_get_character_response.dart';
import 'package:keyboard/data/repository/keyboard_repository.dart';
import '../../base/app_base_request.dart';
import '../../di/get_it.dart';
import '../../utils/atmob_log.dart';
import '../../utils/http_handler.dart';
import '../api/atmob_api.dart';
import '../api/request/character_add_request.dart';
import '../api/request/character_custom_add_request.dart';
import '../api/request/character_custom_delete_request.dart';
import '../api/request/character_custom_generate_request.dart';
import '../api/request/character_custom_page_request.dart';
import '../api/request/character_page_request.dart';
import '../api/request/character_unlock_request.dart';
import '../api/response/character_custom_generate_response.dart';
import '../api/response/character_group_response.dart';
import '../api/response/character_page_response.dart';
import '../bean/character_group_info.dart';

@lazySingleton
class CharactersRepository {
  final AtmobApi atmobApi;
  final String tag = "CharactersRepository";

  // 人设主题
  final RxList<CharacterGroupInfo> _characterGroupList =
      RxList<CharacterGroupInfo>();

  RxList<CharacterGroupInfo> get characterGroupList => _characterGroupList;

  CharactersRepository(this.atmobApi) {
    AtmobLog.d(tag, '$tag....init');
    getCharactersGroup();
  }

  // 获取主题
  Future<CharacterGroupResponse> getCharactersGroup() async {
    return atmobApi
        .getCharactersGroup(AppBaseRequest())
        .then(HttpHandler.handle(true))
        .then((response) {
          _characterGroupList.value = response.characterGroups;
          return response;
        });
  }

  //   获取人设列表
  Future<CharacterPageResponse> getCharactersPage({
    required String groupId,
    required String keyboardId,
    int page = 1,
    int pageSize = 10,
  }) {
    return atmobApi
        .getCharactersPage(
          CharacterPageRequest(
            keyboardId: keyboardId,
            groupId: groupId,
            page: page,
            pageSize: pageSize,
          ),
        )
        .then(HttpHandler.handle(true));
  }

  // 添加人设
  Future<CharacterAddResponse> characterAdd({
    required String characterId,
    required String keyboardId,
  }) {
    return atmobApi
        .addCharacter(
          CharacterAddRequest(characterId: characterId, keyboardId: keyboardId),
        )
        .then(HttpHandler.handle(false));
  }

  // 解锁人设
  Future<CharacterUnlockResponse> characterUnlock({
    required String characterId,
    required String keyboardId,
  }) {
    return atmobApi
        .unlockCharacter(
          CharacterUnlockRequest(
            characterId: characterId,
            keyboardId: keyboardId,
          ),
        )
        .then(HttpHandler.handle(true));
  }

  ///生成自定义人设
  Future<CharacterCustomGenerateResponse> generateCharacterCustom({
    String? name,
    String? birthday,
    String? imageUrl,
    int? gender,
    List<String>? hobbies,
    List<String>? characters,
  }) {
    return atmobApi
        .generateCharacterCustom(
          CharacterCustomGenerateRequest(
            name: name,
            birthday: birthday,
            imageUrl: imageUrl,
            gender: gender,
            hobbies: hobbies,
            characters: characters,
          ),
        )
        .then(HttpHandler.handle(true));
  }

  // 更新定制人设信息
  Future<CharacterCustomUpdateResponse> characterCustomUpdate({
    required String id,
    String? name,
    String? birthday,
    String? imgUrl,
    int? gender,
    List<String>? hobbies,
    List<String>? characters,
  }) {
    return atmobApi
        .updateCustomCharacter(
          CharacterCustomUpdateRequest(
            id: id,
            name: name,
            birthday: birthday,
            imageUrl: imgUrl,
            gender: gender,
            hobbies: hobbies,
            characters: characters,
          ),
        )
        .then(HttpHandler.handle(true));
  }

  // 添加定制人设到键盘
  Future<CharacterCustomUpdateResponse> addCustomCharacter({
    required String characterId,
    required String keyboardId,
  }) {
    return atmobApi
        .addCustomCharacter(
          CharacterCustomAddRequest(
            characterId: characterId,
            keyboardId: keyboardId,
          ),
        )
        .then(HttpHandler.handle(true));
  }

  // 分页查询定制人设列表
  Future<CharacterCustomPageResponse> getCustomCharactersPage({
    String? keyboardId,
    int page = 1,
    int pageSize = 10,
  }) {
    return atmobApi
        .getCustomCharactersPage(
          CharacterCustomPageRequest(
            keyboardId: keyboardId,
            page: page,
            pageSize: pageSize,
          ),
        )
        .then(HttpHandler.handle(true));
  }

  // 删除定制人设
  Future<void> deleteCustomCharacter({required String characterId}) {
    return atmobApi
        .deleteCustomCharacter(
          CharacterCustomDeleteRequest(characterId: characterId),
        )
        .then(HttpHandler.handle(true));
  }

  // 获取新人人设列表
  Future<NewUserGetCharacterResponse> getNewUserCharactersList() {
    return atmobApi
        .getNewUserCharactersList(AppBaseRequest())
        .then(HttpHandler.handle(true));
  }

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

class CharacterException implements Exception {
  final String msg;
  final int? code;

  CharacterException(this.msg, {this.code});

  @override
  String toString() => msg;
}
