package com.rugvedsarika.deviceinfo

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.google.android.material.button.MaterialButton
import android.view.HapticFeedbackConstants
import android.transition.TransitionManager
import android.provider.Settings
import android.content.Intent
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout

class HardwareFragment : Fragment() {

    private lateinit var swipeRefresh: SwipeRefreshLayout
    private lateinit var displayCard: View
    private lateinit var cpuCard: View
    private lateinit var layoutDisplayMore: View
    private lateinit var layoutCpuMore: View
    private lateinit var ivExpandDisplay: android.widget.ImageView
    private lateinit var ivExpandCpu: android.widget.ImageView

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_hardware, container, false)
        
        val tvDisplayInfo = view.findViewById<TextView>(R.id.tvDisplayInfo)
        val tvCpuInfo = view.findViewById<TextView>(R.id.tvCpuInfo)

        swipeRefresh = view.findViewById(R.id.swipeRefresh)
        displayCard = view.findViewById(R.id.displayCard)
        cpuCard = view.findViewById(R.id.cpuCard)
        layoutDisplayMore = view.findViewById(R.id.layoutDisplayMore)
        layoutCpuMore = view.findViewById(R.id.layoutCpuMore)
        ivExpandDisplay = view.findViewById(R.id.ivExpandDisplay)
        ivExpandCpu = view.findViewById(R.id.ivExpandCpu)

        context?.let { ctx ->
            val display = DeviceInfoProvider.getDisplayInfo(ctx)
            val displayText = """
                Resolution: ${display["Resolution"]}
                Refresh Rate: ${display["Refresh Rate"]}
                Density: ${display["Density"]}
                Screen Size: ${display["Size"]}
            """.trimIndent()
            tvDisplayInfo.text = displayText
        }

        val cpuText = """
            Architecture: ${System.getProperty("os.arch")}
            Supported ABIs: ${android.os.Build.SUPPORTED_ABIS.joinToString(", ")}
            Cores: ${Runtime.getRuntime().availableProcessors()}
        """.trimIndent()
        tvCpuInfo.text = cpuText

        setupInteractions(view, cpuText)

        view.findViewById<View>(R.id.btnDisplaySettings).setOnClickListener {
            it.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY)
            startActivity(Intent(Settings.ACTION_DISPLAY_SETTINGS))
        }

        return view
    }

    private fun setupInteractions(view: View, cpuText: String) {
        swipeRefresh.setOnRefreshListener {
            view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS)
            view.postDelayed({ swipeRefresh.isRefreshing = false }, 1000)
        }

        displayCard.setOnClickListener {
            toggleExpansion(layoutDisplayMore, ivExpandDisplay)
        }

        cpuCard.setOnClickListener {
            toggleExpansion(layoutCpuMore, ivExpandCpu)
        }

        view.findViewById<View>(R.id.btnCopyDisplay).setOnClickListener {
            it.performHapticFeedback(HapticFeedbackConstants.CONFIRM)
            val info = view.findViewById<TextView>(R.id.tvDisplayInfo).text.toString()
            copyToClipboard("Display Specs", info)
        }

        view.findViewById<View>(R.id.btnCopyCpu).setOnClickListener {
            it.performHapticFeedback(HapticFeedbackConstants.CONFIRM)
            copyToClipboard("CPU Info", cpuText)
        }
    }

    private fun toggleExpansion(layout: View, icon: android.widget.ImageView) {
        val container = layout.parent.parent as ViewGroup
        TransitionManager.beginDelayedTransition(container)
        if (layout.visibility == View.GONE) {
            layout.visibility = View.VISIBLE
            icon.animate().rotation(180f).start()
        } else {
            layout.visibility = View.GONE
            icon.animate().rotation(0f).start()
        }
    }

    private fun copyToClipboard(label: String, text: String) {
        val ctx = context ?: return
        val clipboard = ctx.getSystemService(android.content.Context.CLIPBOARD_SERVICE) as android.content.ClipboardManager
        val clip = android.content.ClipData.newPlainText(label, text)
        clipboard.setPrimaryClip(clip)
        val v = view ?: return
        com.google.android.material.snackbar.Snackbar.make(v, "$label copied!", com.google.android.material.snackbar.Snackbar.LENGTH_SHORT).show()
    }
}
