/*
 * 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.SettingsPrivitServiceBinding
import bums.lunatic.launcher.helpers.Constants.Companion.KEY_RSS_URL
import bums.lunatic.launcher.helpers.Constants.Companion.KEY_RSS_URL2
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 Misc : BottomSheetDialogFragment() {

    private lateinit var binding : SettingsPrivitServiceBinding

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

        /* initialize views according to the saved values */



        binding.inputFeedUrl.setText(PrefString.telegramBotApi.get(""))
        binding.inputFeedUrl.doOnTextChanged { t,s,b,l ->  PrefString.telegramBotApi.set(t.toString())

        }
        binding.inputFeedUrl2.setText(PrefString.telegramMyId.get(""))
        binding.inputFeedUrl2.doOnTextChanged { t,s,b,l ->  PrefString.telegramMyId.set(t.toString())

        }
        binding.inputFeedUrl3.setText(PrefString.telegramSendTarget.get(""))
        binding.inputFeedUrl3.doOnTextChanged { t,s,b,l ->  PrefString.telegramSendTarget.set(t.toString())

        }
        binding.inputFeedUrl4.setText(PrefString.locationApi.get(""))
        binding.inputFeedUrl4.doOnTextChanged {  t,s,b,l -> PrefString.locationApi.set(t.toString())}

        binding.inputFeedUrl5.setText(PrefString.carName.get(""))
        binding.inputFeedUrl5.doOnTextChanged {  t,s,b,l -> PrefString.carName.set(t.toString())}

        return binding.root
    }

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

    }

    /* save input field value while closing the dialog */
    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        settingsPrefs!!.edit().putString(KEY_RSS_URL,
            Objects.requireNonNull(binding.inputFeedUrl.text).toString().trim { it <= ' ' }).apply()
        settingsPrefs!!.edit().putString(KEY_RSS_URL2,
            Objects.requireNonNull(binding.inputFeedUrl2.text).toString().trim { it <= ' ' }).apply()
    }

}
