import 'package:hmg_qline/api/api_client.dart';
import 'package:hmg_qline/constants/app_constants.dart';
import 'package:hmg_qline/models/generic_response_model.dart';
import 'package:hmg_qline/models/global_config_model.dart';
import 'package:hmg_qline/models/kiosk_ticket_model.dart';
import 'package:hmg_qline/models/prayers_widget_model.dart';
import 'package:hmg_qline/models/rss_feed_model.dart';
import 'package:hmg_qline/models/weathers_widget_model.dart';
import 'package:hmg_qline/utilities/enums.dart';
import 'package:hmg_qline/utilities/extensions.dart';
import 'package:hmg_qline/views/view_helpers/info_components.dart';

abstract class ScreenDetailsRepo {
  Future<GlobalConfigurationsModel?> getGlobalScreenConfigurations({required String ipAddress});

  Future<GenericRespModel?> createTestTickets({required int ticketNumber});

  Future<GenericRespModel?> createTicketFromKiosk({required int projectId, required int queueId, int patientId = 0});

  // Future<WidgetsConfigModel?> getScreenConfigurationsByIP({required String ipAddress});

  Future<WeathersWidgetModel?> getWeatherDetailsByCity({required String cityId});

  Future<PrayersWidgetModel?> getPrayerDetailsByLatLong({required double latitude, required double longitude});

  Future<RssFeedModel?> getRssFeedDetailsByLanguageID({required int languageId});

  Future<GenericRespModel?> acknowledgeTicket({required String ipAddress, required int ticketQueueID, required QTypeEnum qTypeEnum});

  Future<GenericRespModel?> acknowledgeTicketForAppointment({required int ticketId, required String ipAddress, required CallTypeEnum callTypeEnum});
}

class ScreenDetailsRepoImp implements ScreenDetailsRepo {
  ApiClient apiClientInstance;

  ScreenDetailsRepoImp({required this.apiClientInstance});

  @override
  Future<GlobalConfigurationsModel?> getGlobalScreenConfigurations({required String ipAddress}) async {
    try {
      var params = {
        "ipAddress": ipAddress.toString(),
        "apiKey": AppConstants.apiKey.toString(),
      };
      GenericRespModel genericModel = await apiClientInstance.postJsonForObject(
        (json) => GenericRespModel.fromJson(json),
        ApiConstants.commonConfigGet,
        params,
      );
      List<GlobalConfigurationsModel> globalConfigurationsModel = List.generate(genericModel.data.length, (index) => GlobalConfigurationsModel.fromJson(json: genericModel.data[index]));
      if (globalConfigurationsModel.isNotEmpty) {
        return globalConfigurationsModel.first;
      }
      return null;
    } catch (e) {
      logger.e(e.toString());
      InfoComponents.showToast(e.toString());
      return null;
    }
  }

  @override
  Future<GenericRespModel?> createTestTickets({required int ticketNumber}) async {
    try {
      var params = {
        "projectID": "15",
        "patientID": ticketNumber.toString(),
        "isVip": false,
        "orderNo": "123",
        "isVidaPlus": false,
        "createdBy": "101",
        "apiKey": AppConstants.apiKey,
      };
      GenericRespModel genericModel = await apiClientInstance.postJsonForObject(
        (json) => GenericRespModel.fromJson(json),
        ApiConstants.createTicket,
        params,
      );
      return genericModel;
    } catch (e) {
      logger.e(e.toString());
      InfoComponents.showToast(e.toString());
      return null;
    }
  }

  @override
  Future<GenericRespModel?> createTicketFromKiosk({required int projectId, required int queueId, int patientId = 0}) async {
    try {
      var params = {
        "projectID": "$projectId",
        "queueID": "$queueId",
        "patientID": "$patientId",
        "isVidaPlus": false,
        "createdBy": "101",
        "apiKey": AppConstants.apiKey,
      };
      GenericRespModel genericRespModel = await apiClientInstance.postJsonForObject(
        (json) => GenericRespModel.fromJson(json),
        ApiConstants.createTicketForKiosk,
        params,
      );

      genericRespModel.data = KioskPatientTicket.fromJson(genericRespModel.data);
      return genericRespModel;
    } catch (e) {
      logger.e(e.toString());
      InfoComponents.showToast(e.toString());
      return null;
    }
  }

  // @override
  // Future<WidgetsConfigModel?> getScreenConfigurationsByIP({required String ipAddress}) async {
  //   try {
  //     final body = {
  //       "ipAddress": ipAddress.toString(),
  //     };
  //
  //     GenericRespModel genericRespModel = await apiClientInstance.postJsonForObject(
  //       (json) => GenericRespModel.fromJson(json),
  //       ApiConstants.waitingAreaScreenConfigGet,
  //       body,
  //     );
  //     WidgetsConfigModel widgetsConfigModel = WidgetsConfigModel.fromJson(genericRespModel.data);
  //     return widgetsConfigModel;
  //   } catch (e) {
  //     logger.e(e.toString());
  //     InfoComponents.showToast(e.toString());
  //     return null;
  //   }
  // }

  @override
  Future<WeathersWidgetModel?> getWeatherDetailsByCity({required String cityId}) async {
    try {
      final body = {"cityID": cityId};
      GenericRespModel genericRespModel = await apiClientInstance.postJsonForObject(
        (json) => GenericRespModel.fromJson(json),
        ApiConstants.getWeatherForecastBy5Days,
        body,
      );

      List<WeathersWidgetModel> weathersWidgetModel = List.generate(genericRespModel.data.length, (index) => WeathersWidgetModel.fromJson(genericRespModel.data[index]));
      if (weathersWidgetModel.isNotEmpty) {
        return weathersWidgetModel.first;
      }
      return null;
    } catch (e) {
      logger.e(e.toString());
      InfoComponents.showToast(e.toString());
      return null;
    }
  }

  @override
  Future<PrayersWidgetModel?> getPrayerDetailsByLatLong({required double latitude, required double longitude}) async {
    try {
      final body = {"latitude": latitude, "longitude": longitude};
      GenericRespModel genericRespModel = await apiClientInstance.postJsonForObject(
        (json) => GenericRespModel.fromJson(json),
        ApiConstants.prayerTimeToday,
        body,
      );

      List<PrayersWidgetModel> prayersWidgetModel = List.generate(genericRespModel.data.length, (index) => PrayersWidgetModel.fromJson(genericRespModel.data[index]));
      if (prayersWidgetModel.isNotEmpty) {
        return prayersWidgetModel.first;
      }

      return null;
    } catch (e) {
      logger.e(e.toString());
      InfoComponents.showToast(e.toString());
      return null;
    }
  }

  @override
  Future<RssFeedModel?> getRssFeedDetailsByLanguageID({required int languageId}) async {
    try {
      final body = {"languageID": languageId};
      GenericRespModel genericRespModel = await apiClientInstance.postJsonForObject(
        (json) => GenericRespModel.fromJson(json),
        ApiConstants.rssFeedGet,
        body,
      );

      List<RssFeedModel> rssFeedModel = List.generate(genericRespModel.data.length, (index) => RssFeedModel.fromJson(genericRespModel.data[index]));
      if (rssFeedModel.isNotEmpty) {
        return rssFeedModel.first;
      }
      return null;
    } catch (e) {
      logger.e(e.toString());
      InfoComponents.showToast(e.toString());
      return null;
    }
  }

  @override
  Future<GenericRespModel?> acknowledgeTicket({required String ipAddress, required int ticketQueueID, required QTypeEnum qTypeEnum}) async {
    try {
      var params = {
        "ipAddress": ipAddress.toString(),
        "ticketQueueID": ticketQueueID.toString(),
        "qType": qTypeEnum.getQTypeIDFromEnum().toString(),
      };
      GenericRespModel genericModel = await apiClientInstance.postJsonForObject(
        (json) => GenericRespModel.fromJson(json),
        ApiConstants.ticketAcknowledgementInsert,
        params,
      );
      return genericModel;
    } catch (e) {
      logger.e(e.toString());
      InfoComponents.showToast(e.toString());
      return null;
    }
  }

  @override
  Future<GenericRespModel?> acknowledgeTicketForAppointment({required int ticketId, required String ipAddress, required CallTypeEnum callTypeEnum}) async {
    try {
      var params = {
        "id": ticketId.toString(),
        "apiKey": AppConstants.apiKey,
        "ipAddress": ipAddress,
        "callType": callTypeEnum.getIdFromCallTypeEnum().toString(),
      };

      GenericRespModel genericModel = await apiClientInstance.postJsonForObject(
        (json) => GenericRespModel.fromJson(json),
        ApiConstants.ticketCallRequestUpdate,
        params,
      );
      return genericModel;
    } catch (e) {
      logger.e(e.toString());
      InfoComponents.showToast(e.toString());
      return null;
    }
  }
}
