import 'package:dio/dio.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:hoshan/core/services/api/dio_service.dart';
import 'package:hoshan/data/model/ai/bots_model.dart';
import 'package:hoshan/data/model/assistant_comments_model.dart';
import 'package:hoshan/data/model/assistant_personal_info_model.dart';
import 'package:hoshan/data/model/create_assistant_request_model.dart';
import 'package:hoshan/data/model/effects_model.dart';
import 'package:hoshan/data/model/event_model.dart';
import 'package:hoshan/data/model/global_assistant_bots_model.dart';
import 'package:hoshan/data/model/media_model.dart';
import 'package:hoshan/data/model/personal_assistants_bots.dart';
import 'package:hoshan/data/model/photo_gen_model.dart';
import 'package:hoshan/data/model/tools_categories_model.dart';

class BotRepository {
  static final DioService _dioService = DioService();

  static Future<BotsModel> getBots({final String? search}) async {
    try {
      Map<String, dynamic>? queryParameters = {};
      if (search != null) {
        queryParameters['query'] = search;
      }
      Response response = await _dioService
          .sendRequest()
          .get(DioService.getAllBots, queryParameters: queryParameters);
      return BotsModel.fromJson(response.data);
    } catch (ex) {
      rethrow;
    }
  }

  static Future<Bots> getSingleBot({required final int id}) async {
    try {
      Response response =
          await _dioService.sendRequest().get(DioService.getSingleBot(id));
      return Bots.fromJson(response.data['bot']);
    } catch (ex) {
      rethrow;
    }
  }

  static Future<GlobalAssistantBotsModel> getGlobalAssistant(
      {final bool? marked, final int? categorieId}) async {
    try {
      Map<String, dynamic>? queryParameters = {};
      if (categorieId != null) {
        queryParameters['category'] = categorieId;
      }
      if (marked != null) {
        queryParameters['marked'] = marked;
      }

      Response response = await _dioService.sendRequest().get(
          DioService.getGlobalAssistants,
          queryParameters: queryParameters);
      return GlobalAssistantBotsModel.fromJson(response.data);
    } catch (ex) {
      rethrow;
    }
  }

  static Future<PersonalAssistantBotsModel> getPersonalAssistant() async {
    try {
      Response response =
          await _dioService.sendRequest().get(DioService.getPersonalAssistants);
      return PersonalAssistantBotsModel.fromJson(response.data);
    } catch (ex) {
      rethrow;
    }
  }

  static Future<ToolsCategoriesModel> getAllCategories() async {
    try {
      Response response =
          await _dioService.sendRequest().get(DioService.allCategories);
      return ToolsCategoriesModel.fromJson(response.data);
    } catch (ex) {
      rethrow;
    }
  }

  static Future<ToolsCategoriesModel> getToolsCategories() async {
    try {
      Response response =
          await _dioService.sendRequest().get(DioService.toolsCategories);
      return ToolsCategoriesModel.fromJson(response.data);
    } catch (ex) {
      rethrow;
    }
  }

  static Future<Response> botMark(
      {required final int id, required final bool marked}) async {
    try {
      Response response = await _dioService
          .sendRequest()
          .put(DioService.markedBot(id), data: {'marked': marked});
      return response;
    } catch (ex) {
      rethrow;
    }
  }

  static Future<Response> createBot(
      {required final CreateAssistantRequestModel model}) async {
    try {
      FormData formDatBody = await model.toFormData();
      final response = await _dioService.sendRequest().post(
          DioService.getAllBots,
          data: formDatBody,
          options: Options(sendTimeout: 5.minutes));
      return response;
    } catch (e) {
      rethrow;
    }
  }

  static Future<Response> editBot(
      {required final int id,
      required final CreateAssistantRequestModel model}) async {
    try {
      FormData formDatBody = await model.toFormData();
      final response = await _dioService.sendRequest().put(
          DioService.getPersonalAssistant(id),
          data: formDatBody,
          options: Options(sendTimeout: 5.minutes));
      return response;
    } catch (e) {
      rethrow;
    }
  }

  static Future<Response> deleteBot({required final int id}) async {
    try {
      final response = await _dioService
          .sendRequest()
          .delete(DioService.getPersonalAssistant(id));
      return response;
    } catch (e) {
      rethrow;
    }
  }

  static Future<Bots> getAssistantGlobalInfo(int id) async {
    try {
      final response = await _dioService
          .sendRequest()
          .get(DioService.getGlobalAssistant(id));
      return Bots.fromJson(response.data['bot']);
    } catch (e) {
      rethrow;
    }
  }

  static Future<AssistantPersonalInfoModel> getAssistantPersonalInfo(
      int id) async {
    try {
      final response = await _dioService
          .sendRequest()
          .get(DioService.getPersonalAssistant(id));
      return AssistantPersonalInfoModel.fromJson(response.data);
    } catch (e) {
      rethrow;
    }
  }

  static Future<AssistantCommentsModel> getAssistantComments(
      int id, int page) async {
    try {
      final response = await _dioService.sendRequest().get(
          DioService.getAssistantComments(id),
          queryParameters: {'page': page});
      return AssistantCommentsModel.fromJson(response.data);
    } catch (e) {
      rethrow;
    }
  }

  static Future<Response> addCommentInAssistant(
      int id, AssistantComments comment) async {
    try {
      final response = await _dioService.sendRequest().put(
          DioService.getAssistantComments(id),
          data: {'text': comment.text, 'score': comment.score});
      return response;
    } catch (e) {
      rethrow;
    }
  }

  static Future<MediasModel> getMedias() async {
    try {
      final response = await _dioService.sendRequest().get(
            DioService.media,
          );
      return MediasModel.fromJson(response.data);
    } catch (e) {
      rethrow;
    }
  }

  static Future<GensModel> getSingleMedia(int id) async {
    try {
      final response = await _dioService.sendRequest().get(
            DioService.singleMedia(id),
          );
      return GensModel.fromJson(response.data);
    } catch (e) {
      rethrow;
    }
  }

  static Future<EffectsModel> getAllEffects() async {
    try {
      final response = await _dioService.sendRequest().get(
            DioService.effects,
          );
      return EffectsModel.fromJson(response.data);
    } catch (e) {
      rethrow;
    }
  }

  static Future<GensModel> getAllCharachters() async {
    try {
      final response = await _dioService.sendRequest().get(
            DioService.characters,
          );
      return GensModel.fromJson(response.data);
    } catch (e) {
      rethrow;
    }
  }

  static Future<EventModel> getAllEvents() async {
    try {
      final response = await _dioService.sendRequest().get(
            DioService.events,
          );
      return EventModel.fromJson(response.data);
    } catch (e) {
      rethrow;
    }
  }
}
