package com.xyzshell.myphoneinfo.dashboard

import android.content.Intent
import android.os.Bundle
import android.text.format.Formatter
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.andinfo.libs.CpuInfo
import com.xyzshell.myphoneinfo.R
import com.xyzshell.myphoneinfo.databinding.FragmentHardWareBinding
import com.xyzshell.myphoneinfo.dialog.DialogBlueTooth
import com.xyzshell.myphoneinfo.dialog.DialogDiskPart
import com.xyzshell.myphoneinfo.dialog.DialogExtension
import com.xyzshell.myphoneinfo.dialog.DialogInput
import kotlin.math.roundToInt

class HardWareFragment : Fragment() {
 private lateinit var binding: FragmentHardWareBinding
 private var dialogBlueTooth: DialogBlueTooth? = null
 private var dialogInput: DialogInput? = null
 private var dialogExtension: DialogExtension? = null
 private var dialogDiskPart: DialogDiskPart? = null

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

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentHardWareBinding.inflate(inflater, container, false)
        dialogExtension=dialogExtension?: DialogExtension()
        initText()
        binding.cpuBtn.setOnClickListener {
            var intent=Intent(requireContext(), AnalysisActivity::class.java)
            startActivity(intent)
        }
        binding.bluetoothLayout.pair1.setOnClickListener {
            dialogBlueTooth= dialogBlueTooth?: DialogBlueTooth()
            println("showBluetoothDialog"+AndInfo.instance.bluetooth.bondedDevices.size)
            dialogBlueTooth?.show(childFragmentManager, "BlueTooth1")
        }
//        binding.pair2.setOnClickListener {
//            dialogBlueTooth= dialogBlueTooth?: DialogBlueTooth()
//            dialogBlueTooth?.show(childFragmentManager, "BlueTooth2")
//        }
        binding.othertext.setOnClickListener {
            dialogInput=dialogInput?: DialogInput()
            dialogInput?.show(childFragmentManager, "Input")
        }
        binding.extensionShow.setOnClickListener {
            dialogExtension?.show(childFragmentManager, "Extention")
        }
        binding.memoryLayout.disktext.setOnClickListener {
            dialogDiskPart=dialogDiskPart?: DialogDiskPart()
            dialogDiskPart?.show(childFragmentManager, "DiskPart")
        }

        return binding.root
    }

    private fun initText() {
        val cpu = AndInfo.instance.cpu//cpu信息
        val device = AndInfo.instance.device//设备信息
        val gpu=AndInfo.instance.gpu//gpu信息


        //processor
        //处理器
        binding.text0.textTitle.text = getString(R.string.processor)
        binding.text0.textContent.text= cpu.getProcessorName()
        //制造商
        binding.text1.textTitle.text = getString(R.string.vendor)
        binding.text1.textContent.text=cpu.getVendor()
        //硬件
        binding.text2.textTitle.text = getString(R.string.hardware)
        binding.text2.textContent.text=cpu.getProcessorName()
        binding.text3.textTitle.text = getString(R.string.cores)
        binding.text3.textContent.text=cpu.cores.size.toString()
        //cpu内核详情
        binding.text4.textTitle.text = getString(R.string.CPU)
        binding.text4.textContent.text=cpu.getCpuText()
        //制程工艺
        binding.text5.textTitle.text = getString(R.string.process)
        binding.text5.textContent.text=cpu.getProcessInfo().process
        //架构
        binding.text6.textTitle.text = getString(R.string.architecture)
        binding.text6.textContent.text=cpu.getArchitecture()
        //ABI
        binding.text7.textTitle.text = getString(R.string.ABI)
        binding.text7.textContent.text=cpu.getAbi()
        //supported_ABls
        binding.text8.textTitle.text = getString(R.string.supported_ABls)
        binding.text8.textContent.text=cpu.getSupportedAbis().joinToString()
        //频率
        binding.text9.textTitle.text = getString(R.string.frequencies)
        binding.text9.textContent.text=cpu.getFrequencyText()
        println(cpu.getFrequencyText())



        //gpu
        binding.gpuText1.textTitle.text = getString(R.string.vendor)
        binding.gpuText1.textContent.text=gpu.getVendorName()
        binding.gpuText2.textTitle.text = getString(R.string.gpu)

        gpu.getGpuInformation().vkPhysicalDevices?.let { devices ->
            if (devices.isNotEmpty()) {
                devices.forEachIndexed { index, vkPhysicalDevice ->
                    binding.gpuText2.textContent.text=vkPhysicalDevice.deviceName


                }
            }
        }
        binding.gpuText3.textTitle.text=getString(R.string.max_frequency)
        binding.gpuText4.textTitle.text=getString(R.string.architecture)
        binding.gpuText5.textTitle.text=getString(R.string.cores)
        binding.gpuText6.textTitle.text=getString(R.string.total_l2)
        binding.gpuText7.textTitle.text=getString(R.string.bus_width)
        binding.gpuText8.textTitle.text=getString(R.string.vulkan_support)
        binding.gpuText9.textTitle.text=getString(R.string.vulkan_API)
        binding.open1.text=getString(R.string.opengl)
        gpuInfo.eglInformation?.let { eglInfo ->
            eglInfo.eglExtensions?.let {
                dialogExtension?.setContent(it.joinToString())
            }
            eglInfo.glInformation?.let{glInfo ->
                binding.openItem1.text=glInfo.glVendor
                binding.openItem2.text=glInfo.glRenderer
                binding.openItem3.text=glInfo.glVersion
            }
        }
        //display
        val defaultDisplayInfo = AndInfo.instance.display.getDefaultDisplayInfo()
        if(defaultDisplayInfo!=null){
            binding.disText1.textContent.text=defaultDisplayInfo.widthPixels.toString()+"x"+defaultDisplayInfo.heightPixels.toString()
        }
        binding.disText1.textTitle.text=getString(R.string.resolution)

        binding.disText2.textTitle.text=getString(R.string.screen_density)//这个是px
        binding.disText3.textTitle.text=getString(R.string.screen_density_d)
        val dpiStr=defaultDisplayInfo?.densityDpi.toString()+"(xxhdpi)\n"+defaultDisplayInfo?.xdpi?.roundToInt()+"dp"+defaultDisplayInfo?.ydpi?.roundToInt()+"dp"
        binding.disText3.textContent.text=dpiStr
        binding.disText4.textTitle.text=getString(R.string.screen_size_e)
        binding.disText5.textTitle.text=getString(R.string.aspect_ratio)
        binding.disText6.textTitle.text=getString(R.string.refresh_rate)
        binding.disText6.textContent.text=defaultDisplayInfo?.refreshRate.toString()+"HZ"
        binding.disText7.textTitle.text=getString(R.string.wide_color_gamut)
        binding.disText7.textContent.text=defaultDisplayInfo?.isWideColorGamut.toString()
        binding.disText8.textTitle.text=getString(R.string.hdr_support)
        binding.disText8.textContent.text=defaultDisplayInfo?.isHdr.toString()

        //memory
        val storageInfo = AndInfo.instance.storage//存储信息
        val used=Formatter.formatFileSize(AndInfo.instance.context, storageInfo.internalStorageUsedSpace)
        val free=Formatter.formatFileSize(AndInfo.instance.context, storageInfo.internalStorageAvailableSpace)
        val total=Formatter.formatFileSize(AndInfo.instance.context, storageInfo.internalStorageTotalSpace)
        binding.memoryLayout.memText1.textTitle.text=getString(R.string.ram_size)
        binding.memoryLayout.memText1.textContent.text=total
        binding.memoryLayout.ram1.text=used+" used"
        binding.memoryLayout.ram2.text=free+" free"
        binding.memoryLayout.seekbar.progress=(used.substring(0,used.indexOf(" ")).toDouble() / total.substring(0,total.indexOf(" ")).toDouble() *100).toInt()

        //storage
        binding.memoryLayout.memText3.textTitle.text=getString(R.string.zram)
        binding.memoryLayout.memText3.textContent.text=Formatter.formatFileSize(AndInfo.instance.context, storageInfo.internalStorageTotalSpace)
        binding.memoryLayout.ram3.text=used+" used"
        binding.memoryLayout.ram4.text=free+" free"
        binding.memoryLayout.seekbar2.progress=(used.substring(0,used.indexOf(" ")).toDouble() / total.substring(0,total.indexOf(" ")).toDouble() *100).toInt()

        //fileSystem
        binding.memoryLayout.storText1.textTitle.text=getString(R.string.filesystem)
        binding.memoryLayout.storText1.textContent.text=total
        binding.memoryLayout.stor1.text=used+" used"
        binding.memoryLayout.stor2.text=free+" free"
        binding.memoryLayout.radius1.text=used
        binding.memoryLayout.radius2.text=used
        binding.memoryLayout.radius3.text=free
        binding.memoryLayout.seekbar3.progress=(used.substring(0,used.indexOf(" ")).toDouble() / total.substring(0,total.indexOf(" ")).toDouble() *100).toInt()

        //internalStorage
        binding.memoryLayout.interText1.textTitle.text=getString(R.string.filesystem)
        binding.memoryLayout.interText1.textContent.text=storageInfo.internalStorageEncryptionType.toString()
        binding.memoryLayout.interText2.textTitle.text=getString(R.string.block_size)
        binding.memoryLayout.interText2.textContent.text=total
        binding.memoryLayout.interText3.textTitle.text="/data"
        binding.memoryLayout.interText3.textContent.text=total
        binding.memoryLayout.stor3.text=used+" used"
        binding.memoryLayout.stor4.text=free+" free"
        binding.memoryLayout.seekbar2.progress=(used.substring(0,used.indexOf(" ")).toDouble() / total.substring(0,total.indexOf(" ")).toDouble() *100).toInt()





        //Audio
        binding.check1.content.text=getString(R.string.low_audio)
        binding.check2.content.text=getString(R.string.pro_audio)
        binding.check3.content.text=getString(R.string.midl)



    }

    companion object {
        @JvmStatic
        fun newInstance() =
            HardWareFragment().apply {
                arguments = Bundle().apply {

                }
            }
    }
}