package com.example.p3

import android.content.Intent
import androidx.activity.result.ActivityResult
import com.example.core.domain.PspService
import com.example.core.model.PaymentResultEntity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import javax.inject.Inject

class P3Service @Inject constructor(
    private val iapPosService: IapPosService,
    private val printerHelper: PrinterHelper
) : PspService {

    private val scope = CoroutineScope(Dispatchers.IO)

    override fun pay(amount: Long): Intent {
        return iapPosService.startPurchase(amount)

    }

    override fun print(lines: List<String>, onMessage: (String) -> Unit) {
        printerHelper.printLines(lines) { status ->
            onMessage("P3 Printer: $status")
        }
    }


    override fun decodePosResponse(
        id: String?,
        activityResult: ActivityResult
    ): PaymentResultEntity? {
        return try {
            iapPosService.decodeResponse(
                id = id,
                activityResult = activityResult
            )
        } catch (e: Exception) {
            null
        }
    }
}
