package com.offline.music.task.player.ui.fragment

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.gyf.immersionbar.ktx.immersionBar
import com.offline.music.task.player.R
import com.offline.music.task.player.databinding.FragmentSettingBinding
import com.offline.music.task.player.tools.util.EMAIL
import com.offline.music.task.player.tools.util.PRIVACY_POLICY_URL
import com.offline.music.task.player.tools.util.TERMS_OF_SERVICE_URL
import com.offline.music.task.player.tools.util.openPrivacyPolicy
import com.offline.music.task.player.tools.util.openTermsOfService
import com.offline.music.task.player.tools.util.sendFeedback
import com.offline.music.task.player.tools.util.shareApp
import com.offline.music.task.player.ui.activity.AboutActivity

class SettingsFragment : Fragment() {
    private lateinit var binding: FragmentSettingBinding

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

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

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (!hidden) {
            initImmersionBar()
        }
    }

    override fun onResume() {
        super.onResume()
        initImmersionBar()
    }

    private fun initImmersionBar() {
        immersionBar {
            statusBarDarkFont(false)
            statusBarView(binding.view)
        }
    }

    private fun initView() {
        binding.aboutBtn.setOnClickListener {
            startActivity(Intent(requireContext(), AboutActivity::class.java))
        }
        binding.feedbackBtn.setOnClickListener {
            sendFeedback(requireContext(), EMAIL, getString(R.string.app_name))
        }
        binding.shareBtn.setOnClickListener {
            shareApp(requireContext())
        }
        binding.ppBtn.setOnClickListener {
            openPrivacyPolicy(requireContext(), PRIVACY_POLICY_URL)
        }
        binding.tosBtn.setOnClickListener {
            openTermsOfService(requireContext(), TERMS_OF_SERVICE_URL)
        }
    }
}