package partisan.weforge.xyz.pulse

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import partisan.weforge.xyz.pulse.databinding.FragmentAboutBinding

class AboutFragment : Fragment() {

    private var _binding: FragmentAboutBinding? = null
    private val binding get() = _binding!!

    private var tapCount = 0

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentAboutBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onResume() {
        super.onResume()
        (requireActivity() as? MainActivity)?.setAppBarTitle(
            getString(R.string.about_name)
        )
    }

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

        binding.sourceButton.setOnClickListener {
            openUrl("https://weforge.xyz/partisan/Pulse")
        }

        binding.licenseButton.setOnClickListener {
            openUrl("https://www.gnu.org/licenses/gpl-3.0.en.html")
        }

        binding.appIcon.setOnClickListener {
            tapCount++
            if (tapCount >= 5) {
                binding.secretButton.visibility = View.VISIBLE
            }
        }

        binding.secretButton.setOnClickListener {
            requireActivity().supportFragmentManager.beginTransaction()
                .replace(R.id.fragmentContainer, SecretFragment())
                .addToBackStack(null)
                .commit()
        }
    }

    private fun openUrl(url: String) {
        val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        startActivity(intent)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}
