package com.xyzshell.myphoneinfo.dashboard

import android.hardware.camera2.CameraCharacteristics
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.xyzshell.andinfo.AndInfo
import com.xyzshell.myphoneinfo.R
import com.xyzshell.myphoneinfo.custom.SetNumberOrWordUtils.setDecimal0
import com.xyzshell.myphoneinfo.custom.SetNumberOrWordUtils.setDecimal1
import com.xyzshell.myphoneinfo.databinding.FragmentCameraBinding
import com.xyzshell.myphoneinfo.databinding.ItemCameraTypeBinding
import com.xyzshell.myphoneinfo.dialog.DialogCameraMore

class CameraFragment : Fragment() {
    private lateinit var binding:FragmentCameraBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {

        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        binding = FragmentCameraBinding.inflate(inflater, container, false)
        initText()

//        binding.frontMore.setOnClickListener {
//            dialogCamera1= dialogCamera1?:DialogCameraMore(1)
//            dialogCamera1?.show(childFragmentManager, "DialogCameraMore1")
//        }
        return binding.root
    }

    private fun initText() {
        val cameraInfo = AndInfo.instance.camera
        val cameraIds = cameraInfo.cameraIds
        if(!cameraIds.isEmpty()){
            cameraIds.forEachIndexed { index, cameraId ->
                val characteristics = cameraInfo.getCameraCharacteristics(cameraId)
                characteristics?.let {

                    val cameraCardView = LayoutInflater.from(context).inflate(
                        R.layout.item_camera_type,
                        binding.llCamera,
                        false
                    )
                    val itemBinding = ItemCameraTypeBinding.bind(cameraCardView)

                    // 2. 设置标题（区分前后摄像头）
                    val facing = cameraInfo.getCameraFacing(it).toString()
                    val cameraName = when (facing) {
                        "FRONT" -> getString(R.string.front_camera_1) + "${index + 1}"
                        "BACK" -> getString(R.string.rear_camera_1)+"${index + 1}"

                        else -> "External Camera ${index + 1}"
                    }
                    itemBinding.titleCon.text = cameraName

                    //取第一个相机的视频帧率
                    if(index==0){
                        cameraInfo.getMaxFrameRate(it).let { maxFrameRate->
                            binding.video.textTitle.text=getString(R.string.max_frequency)
                            binding.video.textContent.text=maxFrameRate.toString()+"HZ"
                         }
                        cameraInfo.supportsHighSpeedVideo(it).let { hasHighSpeedVideo ->
                            binding.videoCheck1.image.isSelected=hasHighSpeedVideo
                        }
                        cameraInfo.hasVideoStabilization(it).let { hasVideoStabilization ->
                            binding.videoCheck2.image.isSelected=hasVideoStabilization
                        }
                    }


                    // 4. 设置详细信息（使用 include 的文本）

                    itemBinding.camText1.textTitle.text=getString(R.string.resolution)
                    itemBinding.camText2.textTitle.text=getString(R.string.sensor_size)
                    itemBinding.camText3.textTitle.text=getString(R.string.pixel_size)
                    itemBinding.camText4.textTitle.text=getString(R.string._35mm_equivalent_focal_length)
                    itemBinding.camText5.textTitle.text=getString(R.string.shutter_speed)
                    itemBinding.camText6.textTitle.text=getString(R.string.iso_sensitivity_range)
                    itemBinding.rearCheck2.content.text=getString(R.string.flash)
                    itemBinding.rearCheck1.content.text=getString(R.string.optical_image_stabilization)

                    // 设置百万像素

                    cameraInfo.getMegaPixels(it)?.let { size ->
                        itemBinding.conText1.text = "${setDecimal1(size)} MP"
                    }
                    //光圈值
                    cameraInfo.getAvailableApertures(it)?.let { apertures ->
                        if (apertures.isNotEmpty()) {
                            itemBinding.conText2.text = "f/${apertures[0]}" // 取第一个光圈值
                        }
                    }
                    //像素阵列大小
                    cameraInfo.getPixelArraySize(it)?.let { size ->
                        itemBinding.camText1.textContent.text = size

                    }
                    // 传感器物理尺寸
                    cameraInfo.getSensorPhysicalSize(it)?.let { size ->
                        itemBinding.camText2.textContent.text=size
                    }
                    // 传感器物理尺寸
                    cameraInfo.getPixelSize(it)?.let { size ->
                        itemBinding.camText3.textContent.text="${setDecimal0(size)}μm"
                    }

                    // 35mm等效焦距
                    cameraInfo.getFocal35mmEquivalent(it)?.let { size ->
                        itemBinding.camText4.textContent.text="${size.toInt()}mm"
                    }
                    // 快门速度
                    cameraInfo.getShutterSpeedRange(it)?.let { size ->
                        itemBinding.camText5.textContent.text=size
                    }

                    // ISO感光范围
                    cameraInfo.getIsoRange(it)?.let { size ->
                        itemBinding.camText6.textContent.text=size.lower.toString() + " - " + size.upper.toString()
                    }
                    // 光学防抖
                    cameraInfo.hasOpticalStabilization(it).let { hasOpticalStabilization ->
                        itemBinding.rearCheck1.image.isSelected=hasOpticalStabilization
                    }
                    // 闪光灯
                    cameraInfo.hasFlashUnit(it)?.let { hasFlash ->
                        itemBinding.rearCheck2.image.isSelected=hasFlash
                    }


                    // 6. 设置"更多"按钮点击事件
                    itemBinding.rearMore.setOnClickListener {
                        var dialogCamera0: DialogCameraMore? = null
                        dialogCamera0= null ?:DialogCameraMore(cameraName,cameraId)
                        dialogCamera0.show(childFragmentManager, "DialogCameraMore")
                    }

                    // 7. 添加到父容器
                    binding.llCamera.addView(cameraCardView)

                }
            }
        }

        binding.videoCheck1.content.text=getString(R.string.hight_speed_video)
        binding.videoCheck2.content.text=getString(R.string.video_stabilization)


    }

    companion object {

        @JvmStatic
        fun newInstance() =
            CameraFragment().apply {
                arguments = Bundle().apply {

                }
            }
    }
}