package com.example.tis_app.screen

import android.webkit.WebView
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import kotlinx.coroutines.launch
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.hilt.navigation.compose.hiltViewModel
import com.example.core.getAndroidId
import com.example.core.getDeviceName
import com.example.design_system.PspWebView
import com.example.design_system.util.WebViewManager
import com.example.tis_app.viewmodel.WebViewModel

private const val tisWebUrl = "https://tis.shift-am.ir"

@Composable
fun TisWebViewScreen(
    modifier: Modifier = Modifier,
    viewModel: WebViewModel = hiltViewModel()
) {

    val context = LocalContext.current
    var webView: WebView by remember { mutableStateOf(WebViewManager.getWebView(context)) }
    var currentPaymentId by rememberSaveable { mutableStateOf<String?>(null) }
    var isPaymentProcessing by rememberSaveable { mutableStateOf(false) }
    var isPrinting by rememberSaveable { mutableStateOf(false) }
    val coroutineScope = rememberCoroutineScope()
    val deviceInfo by remember {
        mutableStateOf(
            viewModel.deviceInfoJson(
                androidId = getAndroidId(context),
                getDeviceName()
            )
        )
    }

    // launcher to handle POS app result
    val posLauncher = rememberLauncherForActivityResult(
        contract = ActivityResultContracts.StartActivityForResult()
    ) { result ->
        val paymentResultEntity = viewModel.paymentResult(
            id = currentPaymentId,
            activityResult = result

        )

        webView.post {
            webView.evaluateJavascript(
                viewModel.postPaymentResult(paymentResultEntity = paymentResultEntity),
                null
            )
        }
        isPaymentProcessing = false

    }

    PspWebView(
        url = tisWebUrl,
        modifier = modifier,
        onPayClick = { amount: Long, id: String? ->
            if (isPaymentProcessing) return@PspWebView
            isPaymentProcessing = true
            currentPaymentId = id
            viewModel.pay(amount)
                .also(posLauncher::launch)
        },
        onPrintClick = { printEntities ->
            if (isPrinting) return@PspWebView
            isPrinting = true
            coroutineScope.launch {
                try {
                    viewModel.print(printEntities)
                } finally {
                    isPrinting = false
                }
            }
        },
        onDeviceInfo = {
            deviceInfo
        }
    )
}