/*
 * Lunar Launcher
 * Copyright (C) 2022 Md Rasel Hossain
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package bums.lunatic.launcher.settings.childs

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TableRow
import androidx.core.view.children
import bums.lunatic.launcher.R
import bums.lunatic.launcher.databinding.SettingsTodoBinding
import bums.lunatic.launcher.helpers.PrefBoolean
import bums.lunatic.launcher.helpers.PrefHelper
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.switchmaterial.SwitchMaterial
import kotlin.system.exitProcess


internal class HomeSettings : BottomSheetDialogFragment() {

    private lateinit var binding : SettingsTodoBinding
    private var settingsChanged: Boolean = false
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        binding = SettingsTodoBinding.inflate(inflater, container, false)


        setTableItem(binding.admin02)
        setTableItem(binding.admin01)
        setTableItem(binding.normal01)
        setTableItem(binding.normal02)
        setTableItem(binding.normal03)
        setTableItem(binding.normal04)


        binding.callInfo.isChecked = PrefBoolean.showCallHistory.get(false)
        binding.callInfo.setOnCheckedChangeListener {  buttonView, isChecked ->  PrefBoolean.showCallHistory.set(isChecked)
            settingsChanged = true}
        binding.smsInfos.isChecked = PrefBoolean.showSMSHistory.get(false)
        binding.smsInfos.setOnCheckedChangeListener {  buttonView, isChecked ->  PrefBoolean.showSMSHistory.set(isChecked)
            settingsChanged = true}
        binding.notificationInfos.isChecked = PrefBoolean.showNotificationHistory.get(false)
        binding.notificationInfos.setOnCheckedChangeListener {  buttonView, isChecked ->  PrefBoolean.showNotificationHistory.set(isChecked)
            settingsChanged = true}
        binding.nowPlaying.isChecked = PrefBoolean.showNowPlaying.get(false)
        binding.nowPlaying.setOnCheckedChangeListener { buttonView, isChecked ->  PrefBoolean.showNowPlaying.set(isChecked)
            settingsChanged = true}



        if(!PrefHelper.getBoolean("rootPermisssion",false)) {
            binding.admin01.visibility = View.GONE
            binding.admin02.visibility = View.GONE
        }

        return binding.root
    }

    var checkdCount = 0
        set(value) {
            field = value
            PrefBoolean.showNewsHistory.set(field > 0)
        }


    fun setTableItem(tableRow : TableRow) {
        tableRow.children.forEach { (it as? SwitchMaterial)?.let {
            it.text?.toString()?.toUpperCase()?.let { key ->
                it.isChecked = PrefHelper.getBoolean(key, false)
                checkdCount += if (it.isChecked) 1 else -1
                it.setOnCheckedChangeListener { v , isBool ->
                    PrefHelper.putBoolean(key, isBool)
                    checkdCount += if (isBool) 1 else -1
                    settingsChanged = true
                }
            } }
        }
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        (requireDialog() as BottomSheetDialog).dismissWithAnimation = true


    }

    override fun onDestroyView() {
        super.onDestroyView()
        if (settingsChanged) {
            MaterialAlertDialogBuilder(requireActivity())
                .setTitle(R.string.restart_now)
                .setMessage(R.string.restart_message)
                .setPositiveButton(R.string.restart) { _, _ ->
                    exitProcess(0)
                }
                .setNeutralButton(R.string.later, null)
                .show()
        }
    }


}
