import 'dart:convert';

import 'package:hl_lieferservice/services/erpframe.dart';
import 'package:docuframe/docuframe.dart' as df;
import 'package:flutter/cupertino.dart';

import '../dto/basic_response.dart';
import '../dto/scan.dart';
import '../dto/scan_response.dart';

class ScanService extends ErpFrameService {
  ScanService({required super.config});

  Future<void> scanArticle(String internalId) async {
    df.LoginSession? session;

    try {
      session = await getSession();
      df.DocuFrameMacroResponse response =
          await df.Macro(config: dfConfig, session: session).execute(
              "_web_scanArticle",
              parameter: ScanDTO(internalId: internalId).toJson()
                  as Map<String, dynamic>);

      Map<String, dynamic> responseJson = jsonDecode(response.body!);
      ScanResponseDTO responseDto = ScanResponseDTO.fromJson(responseJson);

      if (responseDto.succeeded == true) {
        return;
      } else {
        throw responseDto.message;
      }
    } on df.DocuFrameException catch (e, st) {
      debugPrint("ERROR WHILE SCANNING ARTICLE $internalId");
      debugPrint(e.toString());
      debugPrint(st.toString());

      rethrow;
    } finally {
      await logout(session);
    }
  }

  Future<String?> unscanArticle(
      String internalId, int amount, String reason) async {
    df.LoginSession? session;

    debugPrint("AMOUNT: $amount");
    debugPrint("ID: $internalId");

    try {
      session = await getSession();
      df.DocuFrameMacroResponse response =
          await df.Macro(config: dfConfig, session: session)
              .execute("_web_unscanArticle", parameter: {
        "article_id": internalId,
        "amount": amount.toString(),
        "reason": reason
      });

      Map<String, dynamic> responseJson = jsonDecode(response.body!);
      debugPrint(responseJson.toString());
      ScanResponseDTO responseDto = ScanResponseDTO.fromJson(responseJson);

      if (responseDto.succeeded == true) {
        return responseDto.noteId;
      } else {
        throw responseDto.message;
      }
    } catch (e, st) {
      debugPrint("ERROR WHILE REVERTING THE SCAN OF ARTICLE $internalId");
      debugPrint(e.toString());
      debugPrint(st.toString());

      rethrow;
    } finally {
      await logout(session);
    }
  }

  Future<void> resetScannedArticleAmount(String receiptRowId) async {
    df.LoginSession? session;

    try {
      session = await getSession();
      df.DocuFrameMacroResponse response =
          await df.Macro(config: dfConfig, session: session).execute(
              "_web_unscanArticleReset",
              parameter: {"receipt_row_id": receiptRowId});

      Map<String, dynamic> responseJson = jsonDecode(response.body!);
      BasicResponseDTO responseDto = BasicResponseDTO.fromJson(responseJson);

      if (responseDto.succeeded == true) {
        return;
      } else {
        throw responseDto.message;
      }
    } catch (e, st) {
      debugPrint("ERROR WHILE REVERTING THE UNSCAN OF ARTICLE $receiptRowId");
      debugPrint(e.toString());
      debugPrint(st.toString());

      rethrow;
    } finally {
      await logout(session);
    }
  }
}
