/*
 * 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.content.DialogInterface
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.widget.doOnTextChanged
import bums.lunatic.launcher.databinding.SettingsWeatherBinding
import bums.lunatic.launcher.helpers.Constants.Companion.KEY_CITY_NAME
import bums.lunatic.launcher.helpers.Constants.Companion.KEY_OWM_API
import bums.lunatic.launcher.helpers.Constants.Companion.KEY_SHOW_CITY
import bums.lunatic.launcher.helpers.Constants.Companion.KEY_TEMP_UNIT
import bums.lunatic.launcher.helpers.PrefBoolean
import bums.lunatic.launcher.helpers.PrefString
import bums.lunatic.launcher.settings.SettingsActivity.Companion.settingsPrefs
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import java.util.Objects


internal class WeatherSettings : BottomSheetDialogFragment() {

    private lateinit var binding : SettingsWeatherBinding

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        binding = SettingsWeatherBinding.inflate(inflater, container, false)

        /* initialize views according to the saved values */

        binding.inputOwm.setText(PrefString.weatherApiKey.get(""))
        binding.inputOwm.doOnTextChanged { text, start, before, count ->
            PrefString.weatherApiKey.set(text.toString())
        }
        binding.dress.isChecked = PrefBoolean.weatherDress.get(false)
        binding.weather.isChecked = PrefBoolean.weatherState.get(false)
        binding.weather.setOnCheckedChangeListener { buttonView, isChecked ->  PrefBoolean.weatherState.set(isChecked)}
        binding.dress.setOnCheckedChangeListener { buttonView, isChecked ->  PrefBoolean.weatherDress.set(isChecked)}
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        (requireDialog() as BottomSheetDialog).dismissWithAnimation = true

    }

    /* save input field values while closing the dialog */
    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)

    }

}
