package com.example.stage_app.viewmodel

import android.content.Intent
import androidx.activity.result.ActivityResult
import androidx.lifecycle.ViewModel
import com.example.core.domain.PaymentResult
import com.example.core.domain.Printer
import com.example.core.domain.PspService
import com.example.core.model.DeviceInfo
import com.example.core.model.PaymentResultEntity
import com.example.core.model.PrintEntity
import com.google.gson.Gson
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class StageViewModel @Inject constructor(
    private val pspService: PspService,
    private val paymentResult: PaymentResult,
    private val printer: Printer,
    private val gson: Gson
) : ViewModel() {


    fun postPaymentResult(paymentResultEntity: PaymentResultEntity?): String {
        return paymentResult.post(paymentResultEntity)
    }

    fun pay(amount: Long): Intent = pspService.pay(amount)

    fun paymentResult(
        id: String?,
        activityResult: ActivityResult
    ): PaymentResultEntity {
        return PaymentResultEntity(
            id = id,
            status = com.example.core.model.PaymentResultStatus.ERROR,
            terminalId = "123456",
            stan = "123456",
            rrn = "123456",
            responseCode = "01",
            customerCardNO = "1234567890123456",
            transactionDateTime = "14030101120000",
            description = "تراکنش ناموفق"
        )
    }

    private val mock = listOf<PrintEntity>(

        PrintEntity(
            title = "اطلاعات مشتری",
            items = listOf(
                PrintEntity.ItemEntity("نام مشتری", "علی رضایی"),
                PrintEntity.ItemEntity("شماره تماس", "09123456789"),
                PrintEntity.ItemEntity("آدرس", "تهران، خیابان ولیعصر، پلاک ۱۲۳")
            ),
            type = PrintEntity.Type.CARD
        ),
        PrintEntity(
            title = "اطلاعات پرداخت",
            items = listOf(
                PrintEntity.ItemEntity("روش پرداخت", "کارت به کارت"),
                PrintEntity.ItemEntity("شماره پیگیری", "7845129630"),
                PrintEntity.ItemEntity("تاریخ", "1405/02/14")
            ),
            type = PrintEntity.Type.CARD
        ),
    )

    fun print(printEntities: List<PrintEntity>) {
        printer.print(
            printEntities = printEntities,
            icon = android.R.drawable.btn_star
        )
    }

    fun deviceInfoJson(androidId: String, deviceName: String): String {
        return gson.toJson(
            DeviceInfo(
                androidId = androidId,
                deviceName = deviceName
            )
        )
    }
}
