import 'dart:convert';

import 'package:http/http.dart';
import 'package:logger/logger.dart' as L;
import 'package:mohem_flutter_app/api/api_client.dart';
import 'package:mohem_flutter_app/app_state/app_state.dart';
import 'package:mohem_flutter_app/classes/consts.dart';
import 'package:mohem_flutter_app/models/marathon/marathon_generic_model.dart';
import 'package:mohem_flutter_app/models/marathon/marathon_model.dart';
import 'package:mohem_flutter_app/models/marathon/question_model.dart';
import 'package:mohem_flutter_app/models/marathon/winner_model.dart';
import 'package:signalr_netcore/hub_connection.dart';

class MarathonApiClient {
  late HubConnection hubConnection;
  L.Logger logger = L.Logger();

  Future<String> getMarathonToken() async {
    String employeeUserName = AppState().getUserName ?? "";
    String employeeSession = AppState().postParamsObject?.pSessionId.toString() ?? "";

    Map<String, String> jsonObject = <String, String>{"userName": employeeUserName, "password": employeeSession};

    Response response = await ApiClient().postJsonForResponse(ApiConsts.marathonParticipantLoginUrl, jsonObject);

    var json = jsonDecode(response.body);

    MarathonGenericModel marathonModel = MarathonGenericModel.fromJson(json);

    if (marathonModel.statusCode == 200) {
      if (marathonModel.data != null && marathonModel.isSuccessful == true) {
        AppState().setMarathonToken = marathonModel.data["token"] ?? "";
        return marathonModel.data["token"] ?? "";
      } else {
        //TODO : DO ERROR HANDLING HERE
        return "";
      }
    } else {
      //TODO : DO ERROR HANDLING HERE
      return "";
    }
  }

  Future<String> getProjectId() async {
    Response response = await ApiClient().postJsonForResponse(ApiConsts.marathonProjectGetUrl, <String, dynamic>{}, token: AppState().getMarathonToken ?? await getMarathonToken());

    var json = jsonDecode(response.body);
    MarathonGenericModel marathonModel = MarathonGenericModel.fromJson(json);

    if (marathonModel.statusCode == 200) {
      if (marathonModel.data != null && marathonModel.isSuccessful == true) {
        logger.i("message: ${marathonModel.data[0]["id"]}");
        AppState().setMarathonProjectId = marathonModel.data[0]["id"] ?? "";
        return marathonModel.data[0]["id"] ?? "";
      } else {
        return "";
      }
    } else {
      return "";
    }
  }

  Future<MarathonDetailModel> getMarathonDetails() async {
    String payrollString = AppState().postParamsObject?.payrollCodeStr.toString() ?? "CS";

    Response response = await ApiClient().getJsonForResponse(ApiConsts.marathonUpcomingUrl + payrollString, token: AppState().getMarathonToken ?? await getMarathonToken());

    var json = jsonDecode(response.body);
    logger.i("json in getMarathonDetails: $json");

    MarathonGenericModel marathonGenericModel = MarathonGenericModel.fromJson(json);

    if (marathonGenericModel.data == null) {
      return MarathonDetailModel();
    }

    MarathonDetailModel marathonDetailModel = MarathonDetailModel.fromJson(marathonGenericModel.data);

    AppState().setMarathonProjectId = marathonDetailModel.id!;

    return marathonDetailModel;
  }

  Future<int?> joinMarathonAsParticipant() async {
    Map<String, String> jsonObject = <String, String>{
      "employeeNumber": AppState().memberInformationList!.eMPLOYEENUMBER ?? "",
      "employeeNameAr": AppState().memberInformationList!.eMPLOYEEDISPLAYNAMEAr ?? "",
      "employeeNameEn": AppState().memberInformationList!.eMPLOYEEDISPLAYNAMEEn ?? "",
      "marathonId": AppState().getMarathonProjectId!,
    };

    Response response = await ApiClient().postJsonForResponse(ApiConsts.marathonJoinParticipantUrl, jsonObject, token: AppState().getMarathonToken ?? await getMarathonToken());

    var json = jsonDecode(response.body);

    MarathonGenericModel marathonModel = MarathonGenericModel.fromJson(json);

    if (marathonModel.statusCode == 208) {
      // means participant is already in the marathon i.e already joined
      //TODO:  NEED TO LOOK UPON THIS
      return marathonModel.data["remainingTime"];
    }

    if (marathonModel.statusCode == 200) {
      if (marathonModel.data != null && marathonModel.isSuccessful == true) {
        logger.i("joinMarathonAsParticipant: ${marathonModel.data}");
        return marathonModel.data["remainingTime"];
      } else {
        return null;
      }
    } else {
      return null;
    }
  }

  Future<QuestionModel?> getNextQuestion({required String? questionId, required String marathonId}) async {
    Map<String, String?> jsonObject = <String, String?>{
      "previousQuestionId": questionId,
      "marathonId": marathonId,
    };

    Response response = await ApiClient().postJsonForResponse(ApiConsts.marathonNextQuestionUrl, jsonObject, token: AppState().getMarathonToken ?? await getMarathonToken());

    var json = jsonDecode(response.body);


    logger.i("json in NextQuestion: $json");
    var data = json["data"];

    if (data != null) {
      QuestionModel newQuestion = QuestionModel.fromJson(data);
      return newQuestion;
    } else {
      return null;
    }
  }

  Future<bool> submitSelectedOption({required String marathonId, required String? questionId, required String? selectedAnswerId}) async {
    Map<String, String?> jsonObject = <String, String?>{"marathonId": marathonId, "questionId": questionId, "selectedOptionId" : selectedAnswerId};

    Response response = await ApiClient().postJsonForResponse(ApiConsts.marathonSubmitAnswerUrl, jsonObject, token: AppState().getMarathonToken ?? await getMarathonToken());

    var json = jsonDecode(response.body);
    logger.i("json in submitSelectedOption : $json");

    MarathonGenericModel marathonModel = MarathonGenericModel.fromJson(json);

    if (marathonModel.isSuccessful == null) {
      return false;
    }

    return marathonModel.isSuccessful!;
  }

  Future<int?> getQualifiers({required String marathonId}) async {
    Map<String, String> params = <String, String>{"marathonId": marathonId};
    Response response = await ApiClient().getJsonForResponse(ApiConsts.marathonQualifiersUrl, queryParameters: params, token: AppState().getMarathonToken ?? await getMarathonToken());

    var json = jsonDecode(response.body);
    logger.i("json in getQualifiers: $json");

    MarathonGenericModel marathonGenericModel = MarathonGenericModel.fromJson(json);

    if (marathonGenericModel.isSuccessful == true && marathonGenericModel.statusCode == 200 && marathonGenericModel.data != null) {
      return marathonGenericModel.data["winnerCount"];
    }
    return null;
  }

  Future<List<WinnerModel>?> getSelectedWinner({required String marathonId}) async {
    Map<String, String> params = <String, String>{"marathonId": marathonId};
    Response response = await ApiClient().getJsonForResponse(ApiConsts.marathonSelectedWinner, queryParameters: params, token: AppState().getMarathonToken ?? await getMarathonToken());

    var json = jsonDecode(response.body);
    logger.i("json in getSelectedWinner: $json");

    MarathonGenericModel marathonGenericModel = MarathonGenericModel.fromJson(json);

    if (marathonGenericModel.isSuccessful == true && marathonGenericModel.statusCode == 200 && marathonGenericModel.data != null) {
      List<WinnerModel> winners = <WinnerModel>[];
      List data = marathonGenericModel.data as List;

      for (Map<String, dynamic> winner in data) {
        winners.add(WinnerModel.fromJson(winner));
      }
      return winners;
    }
    return null;
  }

// Future<void> buildHubConnection(BuildContext context, String prizeId) async {
//   HttpConnectionOptions httpOptions = HttpConnectionOptions(skipNegotiation: false, logMessageContent: true);
//   hubConnection = HubConnectionBuilder()
//       .withUrl(
//         ApiConsts.marathonHubConnectionUrl + "?employeeNumber=${AppState().memberInformationList!.eMPLOYEENUMBER ?? ""}&employeeName=${AppState().memberInformationList!.eMPLOYEENAME ?? ""}",
//         options: httpOptions,
//       )
//       .withAutomaticReconnect(
//         retryDelays: <int>[2000, 5000, 10000, 20000],
//       )
//       .configureLogging(
//         Logger("Logging"),
//       )
//       .build();
//   hubConnection.onclose(
//     ({Exception? error}) {
//       logger.i("onclose");
//     },
//   );
//   hubConnection.onreconnecting(
//     ({Exception? error}) {
//       logger.i("onreconnecting");
//     },
//   );
//   hubConnection.onreconnected(
//     ({String? connectionId}) {
//       logger.i("onreconnected");
//     },
//   );
//   if (hubConnection.state != HubConnectionState.Connected) {
//     await hubConnection.start();
//     logger.i("Started HubConnection");
//
//     await hubConnection.invoke(
//       "AddParticipant",
//       args: <Object>[
//         <String, dynamic>{
//           "employeeNumber": AppState().memberInformationList!.eMPLOYEENUMBER ?? "",
//           "employeeName": AppState().memberInformationList!.eMPLOYEENAME ?? "",
//           "marathonId": AppState().getMarathonProjectId,
//           "prizeId": "8577B2E8-5DD7-43F0-10DD-08DACB0AC064",
//         }
//       ],
//     ).catchError((e) {
//       logger.i("Error in AddParticipant: $e");
//     });
//
//     context.read<MarathonProvider>().addItemToList(ApiConsts.dummyQuestion);
//
//     await hubConnection.invoke(
//       "SendQuestionToParticipant",
//       args: <Object>[
//         <String, dynamic>{
//           "marathonId": "${AppState().getMarathonProjectId}",
//         }
//       ],
//     ).catchError((e) {
//       Utils.confirmDialog(context, e.toString());
//       logger.i("Error in SendQuestionToParticipant: $e");
//     });
//
//     try {
//       hubConnection.on("OnSendQuestionToParticipant", (List<Object?>? arguments) {
//         onSendQuestionToParticipant(arguments, context);
//       });
//     } catch (e, s) {
//       logger.i("Error in OnSendQuestionToParticipant");
//     }
//
//     try {
//       hubConnection.on("OnParticipantJoin", (List<Object?>? arguments) {
//         onParticipantJoin(arguments, context);
//       });
//     } catch (e, s) {
//       logger.i("Error in OnParticipantJoin");
//     }
//   }
// }
//
// Future<void> onSendQuestionToParticipant(List<Object?>? arguments, BuildContext context) async {
//   logger.i("onSendQuestionToParticipant arguments: $arguments");
//
//   if (arguments != null) {
//     Map<dynamic, dynamic> data = arguments.first! as Map<dynamic, dynamic>;
//     var json = data["data"];
//     QuestionModel newQuestion = QuestionModel.fromJson(json);
//     AppRoutes.navigatorKey.currentContext!.read<MarathonProvider>().onNewQuestionReceived(newQuestion);
//   }
// }
//
// Future<void> onParticipantJoin(List<Object?>? arguments, BuildContext context) async {
//   logger.i("OnParticipantJoin arguments: $arguments");
//   context.watch<MarathonProvider>().totalMarathoners++;
// }
}
