package com.narutohuo.xindazhou.qrcode.ui

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.huawei.hms.hmsscankit.ScanUtil
import com.huawei.hms.ml.scan.HmsScan
import com.huawei.hms.ml.scan.HmsScanAnalyzerOptions

/**
 * 二维码扫描 Activity
 * 
 * 使用华为 Default View Mode 实现扫码界面
 */
class QRCodeScanActivity : AppCompatActivity() {
    
    companion object {
        const val EXTRA_RESULT = "scan_result"
        const val REQUEST_CODE_SCAN = 1001
        const val REQUEST_CODE_CAMERA_PERMISSION = 1002
        
        /**
         * 启动扫码界面
         */
        fun start(activity: Activity, requestCode: Int = REQUEST_CODE_SCAN) {
            val intent = Intent(activity, QRCodeScanActivity::class.java)
            activity.startActivityForResult(intent, requestCode)
        }
    }
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        
        // 检查相机权限
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
            != PackageManager.PERMISSION_GRANTED
        ) {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.CAMERA),
                REQUEST_CODE_CAMERA_PERMISSION
            )
        } else {
            startScan()
        }
    }
    
    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == REQUEST_CODE_CAMERA_PERMISSION) {
            if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                startScan()
            } else {
                Toast.makeText(this, "需要相机权限才能扫码", Toast.LENGTH_SHORT).show()
                finish()
            }
        }
    }
    
    private fun startScan() {
        val options = HmsScanAnalyzerOptions.Creator()
            .setHmsScanTypes(HmsScan.QRCODE_SCAN_TYPE)
            .create()
        
        ScanUtil.startScan(this, REQUEST_CODE_SCAN, options)
    }
    
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_CODE_SCAN) {
            if (resultCode == Activity.RESULT_OK && data != null) {
                @Suppress("DEPRECATION")
                val hmsScan = data.getParcelableExtra<HmsScan>(ScanUtil.RESULT)
                if (hmsScan != null) {
                    val intent = Intent().apply {
                        putExtra(EXTRA_RESULT, hmsScan.originalValue)
                    }
                    setResult(Activity.RESULT_OK, intent)
                }
            }
            finish()
        }
    }
}
