package com.divyagkquiz.app.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.divyagkquiz.app.R

import android.widget.LinearLayout
import com.google.android.material.switchmaterial.SwitchMaterial
import android.widget.Toast

import android.content.Intent

class SettingsFragment : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_settings, container, false)
    }

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

        val switchVibration = view.findViewById<SwitchMaterial>(R.id.switch_vibration)
        val switchTimer = view.findViewById<SwitchMaterial>(R.id.switch_quick_timer)
        val btnShare = view.findViewById<LinearLayout>(R.id.btn_share_app)
        val btnAbout = view.findViewById<LinearLayout>(R.id.btn_about)

        switchVibration.setOnCheckedChangeListener { _, isChecked ->
            val msg = if (isChecked) "Vibration enabled" else "Vibration disabled"
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
        }

        switchTimer.setOnCheckedChangeListener { _, isChecked ->
            val msg = if (isChecked) "Quick Timer enabled" else "Quick Timer disabled"
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
        }

        btnShare.setOnClickListener {
            val appPackageName = context?.packageName
            val shareIntent = Intent(Intent.ACTION_SEND).apply {
                type = "text/plain"
                putExtra(Intent.EXTRA_SUBJECT, "Check out GK Master!")
                putExtra(Intent.EXTRA_TEXT, "I'm learning so much with GK Master! Download it now and master over 135+ trivia questions:\nhttps://play.google.com/store/apps/details?id=$appPackageName")
            }
            startActivity(Intent.createChooser(shareIntent, "Share GK Master via"))
        }

        btnAbout.setOnClickListener {
            Toast.makeText(context, "GK Master v1.0.0", Toast.LENGTH_SHORT).show()
        }
    }
}
