package com.xyzshell.myphoneinfo.dialog

import android.os.Bundle
import android.view.View
import com.xyzshell.andinfo.AndInfo
import com.xyzshell.myphoneinfo.R
import com.xyzshell.myphoneinfo.base.BaseDialogFragment
import com.xyzshell.myphoneinfo.custom.SetNumberOrWordUtils.setDecimal0
import com.xyzshell.myphoneinfo.custom.SetNumberOrWordUtils.setDecimal1
import com.xyzshell.myphoneinfo.custom.SetNumberOrWordUtils.toTitleCase
import com.xyzshell.myphoneinfo.databinding.DialogCameraMoreBinding

class DialogCameraMore(private val name:String,private val id:String) :BaseDialogFragment<DialogCameraMoreBinding>(DialogCameraMoreBinding::inflate){
    override fun getTitle(): String  = name

    override fun getIconRes(): Int? =9

    override fun getDoubleBtn(): Boolean? = false

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val cameraInfo = AndInfo.instance.camera
        if(!id.isEmpty()){
            id.forEachIndexed { index, cameraId ->
                val characteristics = cameraInfo.getCameraCharacteristics(id)
                characteristics?.let {
                    // 设置详细信息（使用 include 的文本）
                    // 设置百万像素

                    cameraInfo.getMegaPixels(it)?.let { size ->
                        binding.tv1.setValue("${setDecimal1(size)} MP")
                    }
                    //像素阵列大小
                    cameraInfo.getPixelArraySize(it)?.let { size ->
                        binding.tv2.setValue(size)

                    }
                    // 传感器物理尺寸
                    cameraInfo.getSensorPhysicalSize(it)?.let { size ->
                       binding.tv3.setValue(size)
                    }
                    // 传感器px尺寸
                    cameraInfo.getPixelSize(it)?.let { size ->
                        binding.tv4.setValue("${setDecimal0(size)}μm")
                    }
                    // 颜色排列
                    cameraInfo.getColorFilterArrangement(it)?.let { size ->
                       binding.tv5.setValue(size)
                    }
                    //光圈值
                    cameraInfo.getAvailableApertures(it)?.let { apertures ->
                        if (apertures.isNotEmpty()) {
                            binding.tv6.setValue("f/${apertures[0]}")
                        }
                    }
                    //焦距
                    cameraInfo.getFocalLength(it)?.let { apertures ->
                        if (apertures.isNotEmpty()) {
                            binding.tv7.setValue(apertures[0].toString())
                        }
                    }

                    // 35mm等效焦距
                    cameraInfo.getFocal35mmEquivalent(it)?.let { size ->
                        binding.tv8.setValue("${size.toInt()}mm")
                    }
                    // 裁切系数
                    cameraInfo.getFocal35mmEquivalent(it)?.let { size ->
                        binding.tv9.setValue("${size.toInt()}x")
                    }
                    // 快门速度
                    cameraInfo.getShutterSpeedRange(it)?.let { size ->
                        binding.tv11.setValue(size)
                    }

                    // ISO感光范围
                    cameraInfo.getIsoRange(it)?.let { size ->
                        binding.tv12.setValue(size.lower.toString() + " - " + size.upper.toString())
                    }

                    // 闪光灯
                    binding.check1.content.text=getString(R.string.flash)
                    cameraInfo.hasFlashUnit(it)?.let { hasFlash ->
                        binding.check1.image.isSelected=hasFlash
                    }
                    // 视频防抖
                    binding.check2.content.text=getString(R.string.video_stabilization)
                    cameraInfo.hasVideoStabilization(it)?.let { hasVideoStabilization ->
                        binding.check2.image.isSelected=hasVideoStabilization
                    }
                    // 光学防抖
                    binding.check3.content.text=getString(R.string.optical_image_stabilization)
                    cameraInfo.hasOpticalStabilization(it).let { hasOpticalStabilization ->
                        binding.check3.image.isSelected = hasOpticalStabilization
                    }
                    // 曝光锁定
                    // 曝光模式
                    binding.check4.content.text=getString(R.string.ae_lock)
                    cameraInfo.getAutoExposureModes(it)?.let { list ->
                        val allInfo=list.joinToString(" , ") { mode -> mode}
                        binding.check4.image.isSelected=list.size>2
                        binding.more2.text=allInfo
                    }
                    // 白平衡锁定
                    // 白平衡模式
                    binding.check5.content.text=getString(R.string.wb_lock)
                    cameraInfo.getWhiteBalanceModes(it)?.let { list ->
                        binding.check5.image.isSelected=list.size>2
                        val allInfo=list.joinToString(" , ") { mode -> mode}
                        binding.more4.text=allInfo
                    }


                    //功能
                    cameraInfo.getAvailableCapabilities(it).let{ capabilities ->
                        val allCapabilities = capabilities.joinToString(",") { capability ->
                            toTitleCase(capability.name.replace("_", " "))
                        }
                        binding.more1.text=allCapabilities
                    }
                    //自动对焦模式
                    cameraInfo.getAvailableCapabilities(it).let{ capabilities ->
                        val allInfo=capabilities.joinToString(" , ") { capability ->
                            toTitleCase(capability.name.replace("_", " "))
                        }
                        binding.more3.text=allInfo
                    }

                //场景模式
                    cameraInfo.getSceneModes(it).let{ capabilities ->
                        val allInfo=capabilities?.joinToString(" , ") { capability ->
                            toTitleCase(capability.replace("_", " "))
                        }
                        binding.more5.text=allInfo
                        binding.tv10.setValue(capabilities?.get(0) ?: "")
                    }




                }
            }
        }
    }

    override fun onNegativeClick() {
        super.onNegativeClick()

    }

    override fun onPositiveClick() {
        super.onPositiveClick()
    }
}