package com.rugvedsarika.deviceinfo

import android.content.Intent
import android.os.Bundle
import android.provider.Settings
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 androidx.swiperefreshlayout.widget.SwipeRefreshLayout

class SystemFragment : Fragment() {

    private lateinit var swipeRefresh: SwipeRefreshLayout
    private lateinit var systemCard: View
    private lateinit var layoutSystemMore: View
    private lateinit var ivExpandSystem: android.widget.ImageView

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_system, container, false)
        
        val tvSystemInfo = view.findViewById<TextView>(R.id.tvSystemInfo)
        val btnDevOps = view.findViewById<MaterialButton>(R.id.btnDevOps)
        val btnSettings = view.findViewById<MaterialButton>(R.id.btnSettings)

        swipeRefresh = view.findViewById(R.id.swipeRefresh)
        systemCard = view.findViewById(R.id.systemCard)
        layoutSystemMore = view.findViewById(R.id.layoutSystemMore)
        ivExpandSystem = view.findViewById(R.id.ivExpandSystem)

        val info = """
            Brand: ${DeviceInfoProvider.getBrand()}
            Model: ${DeviceInfoProvider.getModel()}
            Manufacturer: ${DeviceInfoProvider.getManufacturer()}
            Android Version: ${DeviceInfoProvider.getAndroidVersion()}
            Build Number: ${DeviceInfoProvider.getBuildNumber()}
            SDK Level: ${android.os.Build.VERSION.SDK_INT}
        """.trimIndent()
        
        tvSystemInfo.text = info

        setupInteractions(view)

        btnDevOps.setOnClickListener {
            it.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY)
            try {
                startActivity(Intent(Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS))
            } catch (e: Exception) {
                startActivity(Intent(Settings.ACTION_SETTINGS))
            }
        }

        btnSettings.setOnClickListener {
            it.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY)
            startActivity(Intent(Settings.ACTION_SETTINGS))
        }

        return view
    }

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

        systemCard.setOnClickListener {
            toggleExpansion(layoutSystemMore, ivExpandSystem)
        }

        view.findViewById<View>(R.id.btnCopySystem).setOnClickListener {
            it.performHapticFeedback(HapticFeedbackConstants.CONFIRM)
            val info = view.findViewById<TextView>(R.id.tvSystemInfo).text.toString()
            copyToClipboard("System Info", info)
        }
    }

    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()
    }
}
