package com.xyzshell.myphoneinfo.dashboard

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import com.xyzshell.myphoneinfo.base.BaseFragment
import com.xyzshell.myphoneinfo.databinding.FragmentDashboardBinding
import com.xyzshell.myphoneinfo.dialog.DialogAppInstall
import com.xyzshell.myphoneinfo.dialog.DialogBattery
import com.xyzshell.myphoneinfo.dialog.DialogDisplay
import com.xyzshell.myphoneinfo.dialog.DialogMemory
import com.xyzshell.myphoneinfo.dialog.DialogNetwork
import com.xyzshell.myphoneinfo.dialog.DialogOperating
import com.xyzshell.myphoneinfo.dialog.DialogStorage


private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

class DashboardFragment : BaseFragment<FragmentDashboardBinding>() {

    private var param1: String? = null
    private var param2: String? = null

    private var dialogBattery: DialogBattery? = null
    private var dialogNetwork: DialogNetwork? = null
    private var dialogDisplay: DialogDisplay? = null
    private var dialogOperating: DialogOperating? = null
    private var dialogAppInstall: DialogAppInstall? = null
    private var dialogMemory: DialogMemory? = null
    private var dialogStorage: DialogStorage? = null


    companion object {
        @JvmStatic
        fun newInstance() =
            DashboardFragment().apply {
                arguments = Bundle().apply {
//                    putString(ARG_PARAM1, param1)
//                    putString(ARG_PARAM2, param2)
                }
            }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
//            param1 = it.getString(ARG_PARAM1)
//            param2 = it.getString(ARG_PARAM2)
        }
    }


    override fun getViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentDashboardBinding {
        return FragmentDashboardBinding.inflate(inflater, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        initCpu()
        initClick()


    }

    private fun initCpu() {
        binding.layoutCpu.run {
            recyclerCpu.run {
                adapter = CpuAdapter(requireContext()).apply {
                    setData(
                        listOf(
                            "1804 MHz",
                            "1804 MHz",
                            "1804 MHz",
                            "1804 MHz",
                            "1804 MHz",
                            "1804 MHz",
                            "1804 MHz",
                            "1804 MHz"
                        )
                    )
                }
                layoutManager = GridLayoutManager(requireContext(), 2)

            }
        }

    }

    private fun initClick() {
        binding.layoutCpu.showCpu.setOnClickListener {
            startActivity(Intent(requireContext(), CpuStatusActivity::class.java))
        }
        binding.layoutCpu.moduleCpu.setOnClickListener {
            startActivity(Intent(requireContext(), TemperatureActivity::class.java))
        }
        binding.layoutCenter.run {
            relayoutBattery.setOnClickListener {
                dialogBattery = dialogBattery ?: DialogBattery()
                dialogBattery?.show(parentFragmentManager, "")
            }
            relayoutNetwork.setOnClickListener {
                dialogNetwork = dialogNetwork ?: DialogNetwork()
                dialogNetwork?.show(parentFragmentManager, "")
            }
            relayoutDisplay.setOnClickListener {
                dialogDisplay = dialogDisplay?: DialogDisplay()
                dialogDisplay?.show(parentFragmentManager, "")
            }
            relayoutApps.setOnClickListener {
                dialogAppInstall = dialogAppInstall ?: DialogAppInstall()
                dialogAppInstall?.show(parentFragmentManager, "")
            }
            relayoutRam.setOnClickListener {
                dialogMemory = dialogMemory ?: DialogMemory()
                dialogMemory?.show(parentFragmentManager, "")
            }
            relayoutStorage.setOnClickListener {
                dialogStorage = dialogStorage ?: DialogStorage()
                dialogStorage?.show(parentFragmentManager, "")
            }
        }
        binding.layoutTop.root.setOnClickListener {
            dialogOperating = dialogOperating ?: DialogOperating()
            dialogOperating?.show(parentFragmentManager, "")
        }
        binding.layoutBottom.run {
            relayoutTools.setOnClickListener {
                startActivity(Intent(requireContext(), ToolsActivity::class.java))
            }
            relayoutTests.setOnClickListener {
                startActivity(Intent(requireContext(), TestActivity::class.java))
            }
        }

    }

}