package com.dlc.chemical.ui.settings

import android.content.Context
import android.graphics.Point
import android.hardware.Camera
import android.os.Bundle
import android.view.ViewTreeObserver
import android.view.WindowManager
import com.dlc.chemical.arcsoft.util.camera.CameraHelper
import com.dlc.chemical.databinding.ActivitySettingsCameraBinding
import com.dlc.chemical.sp.CameraSp
import com.dlc.chemical.sp.CameraType
import com.rc.core.log.RcLog
import com.rc.core.ui.activity.RcBaseActivity

/**
 * 摄像头配置
 *
 * @author ReiChin_
 */
class CameraSettingsActivity : RcBaseActivity<ActivitySettingsCameraBinding>() {

    override fun createViewBinding() = ActivitySettingsCameraBinding.inflate(layoutInflater)

    private lateinit var mCameraType: CameraType

    private val mCameraId by lazy {
        CameraSp.loadConfig(this, mCameraType)
    }

    private var mCameraHelper: CameraHelper? = null

    override fun initViews(savedInstanceState: Bundle?) {
        mCameraType = intent.getSerializableExtra("cameraType") as CameraType

        viewBinding.back.setOnClickListener {
            finish()
        }

        // 确定
        viewBinding.confirm.setOnClickListener {
            val cameraId = mCameraHelper?.currentCameraId() ?: mCameraId

            CameraSp.saveConfig(context = this, cameraId = cameraId, mCameraType)
            viewBinding.confirm.postDelayed({
                setResult(RESULT_OK)
                finish()
            }, 50)
        }

        // 在布局结束后才做初始化操作
        viewBinding.texturePreview.viewTreeObserver.addOnGlobalLayoutListener(
            object : ViewTreeObserver.OnGlobalLayoutListener {
                override fun onGlobalLayout() {
                    viewBinding.texturePreview.viewTreeObserver.removeOnGlobalLayoutListener(this)
                    initCamera()
                }
            })
        viewBinding.changeCamera.setOnClickListener {
            mCameraHelper?.switchCamera()
        }

        viewBinding.title.text = when (mCameraType) {
            CameraType.FACE -> "人脸识别摄像头配置"
            CameraType.VIDEO -> "称重录像摄像头配置"
        }
    }

    private fun initCamera() {
        val windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager? ?: return

        val numberOfCameras = Camera.getNumberOfCameras()
        RcLog.info("[CameraSettingsActivity] numberOfCameras:${numberOfCameras}, mCameraId:${mCameraId}")

        mCameraHelper = CameraHelper.Builder()
            .previewViewSize(
                Point(
                    viewBinding.texturePreview.measuredWidth,
                    viewBinding.texturePreview.measuredHeight
                )
            )
            .rotation(windowManager.defaultDisplay.rotation)
            .specificCameraId(mCameraId)
            .isMirror(false)
            .previewOn(viewBinding.texturePreview)
//            .cameraListener()
            .build().apply {
                init()
                start()
            }
    }

    override fun onDestroy() {
        super.onDestroy()
        mCameraHelper?.release()
    }

}