package com.za.ui.view

import android.app.Activity
import android.content.Intent
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import com.blankj.utilcode.util.ToastUtils
import com.za.common.log.LogUtil
import com.za.ext.noDoubleClick
import com.za.servicing.R
import com.za.signature.GridPaintActivity
import com.za.signature.config.PenConfig

@Composable
fun SignatureView(modifier: Modifier = Modifier, success: (String?) -> Unit, serverPath: String?) {
    val context = LocalContext.current
    val signatureLauncher = rememberLauncherForActivityResult(contract = ActivityResultContracts.StartActivityForResult()) { it ->
        if (it.resultCode == Activity.RESULT_OK) {
            val value = it.data?.getStringExtra(PenConfig.SAVE_PATH)
            LogUtil.print("pen save Path", "path==$value")
            if (value.isNullOrBlank()) {
                ToastUtils.showLong("照片路径为空，请重新拍摄！")
                return@rememberLauncherForActivityResult
            }
            success(value)
        }
    }

    AsyncImage(model = R.drawable.sv_sign.takeIf { serverPath.isNullOrBlank() } ?: serverPath,
        contentDescription = "",
        modifier = modifier
            .size(142.dp, 52.dp)
            .background(color = Color.White, shape = RoundedCornerShape(4.dp))
            .noDoubleClick {
                val intent = Intent(context, GridPaintActivity::class.java)
                intent.putExtra("background", android.graphics.Color.WHITE)
                intent.putExtra("crop", true)
                intent.putExtra("fontSize", 30) //手写字体大小
                intent.putExtra("format", PenConfig.FORMAT_PNG)
                intent.putExtra("lineLength", 10) //每行显示字数（超出屏幕支持横向滚动）
                signatureLauncher.launch(intent)
            },
        contentScale = ContentScale.Inside)
}