package com.rugvedsarika.deviceinfo

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.google.android.material.appbar.MaterialToolbar
import com.google.android.material.bottomnavigation.BottomNavigationView

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        setupToolbar()
        setupNavigation()

        // Load default fragment
        if (savedInstanceState == null) {
            loadFragment(HomeFragment())
        }
    }

    private fun setupToolbar() {
        val toolbar = findViewById<MaterialToolbar>(R.id.toolbar)
        toolbar.setOnMenuItemClickListener { item ->
            if (item.itemId == R.id.action_export) {
                exportReport()
                true
            } else false
        }
    }

    private fun setupNavigation() {
        val bottomNav = findViewById<BottomNavigationView>(R.id.bottom_navigation)
        bottomNav.setOnItemSelectedListener { item ->
            val fragment: Fragment = when (item.itemId) {
                R.id.nav_dashboard -> HomeFragment()
                R.id.nav_system -> SystemFragment()
                R.id.nav_hardware -> HardwareFragment()
                R.id.nav_battery -> BatteryFragment()
                R.id.nav_sensors -> SensorFragment()
                else -> HomeFragment()
            }
            loadFragment(fragment)
            true
        }
    }

    private fun loadFragment(fragment: Fragment) {
        supportFragmentManager.beginTransaction()
            .setCustomAnimations(R.anim.fade_in, R.anim.fade_out)
            .replace(R.id.nav_host_fragment, fragment)
            .commit()
    }

    private fun exportReport() {
        val report = """
            📱 DEVICE INFO REPORT
            --------------------
            BRAND: ${DeviceInfoProvider.getBrand()}
            MODEL: ${DeviceInfoProvider.getModel()}
            VERSION: ${DeviceInfoProvider.getAndroidVersion()}
            BUILD: ${DeviceInfoProvider.getBuildNumber()}
            
            🖥 DISPLAY
            RESOLUTION: ${DeviceInfoProvider.getDisplayInfo(this)["Resolution"]}
            REFRESH: ${DeviceInfoProvider.getDisplayInfo(this)["Refresh Rate"]}
            
            🔋 BATTERY
            HEALTH: ${HardwareInfoProvider.getBatteryInfo(this)["Health"]}
            TECH: ${HardwareInfoProvider.getBatteryInfo(this)["Technology"]}
            
            📡 NETWORK
            IP: ${HardwareInfoProvider.getIPAddress()}
        """.trimIndent()

        val shareIntent = Intent(Intent.ACTION_SEND).apply {
            type = "text/plain"
            putExtra(Intent.EXTRA_TITLE, "Device Info Report")
            putExtra(Intent.EXTRA_TEXT, report)
        }
        startActivity(Intent.createChooser(shareIntent, "Share Report via"))
    }
}
