package pt.whatsms.gateway.ui.settings

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.text.InputType
import android.view.View
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import androidx.preference.EditTextPreference
import androidx.preference.Preference
import kotlinx.coroutines.launch
import pt.whatsms.gateway.R
import pt.whatsms.gateway.modules.gateway.GatewayService
import pt.whatsms.gateway.modules.gateway.GatewaySettings
import pt.whatsms.gateway.ui.ScanPairingActivity
import org.koin.android.ext.android.inject
import java.text.DateFormat

class WhatSmsServerSettingsFragment : BasePreferenceFragment() {

    private val settings: GatewaySettings by inject()
    private val service: GatewayService by inject()

    @SuppressLint("NotifyDataSetChanged")
    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.whatsms_server_preferences, rootKey)

        findPreference<Preference>("transient.server_url")?.summary =
            GatewaySettings.PUBLIC_URL

        findPreference<Preference>("transient.device_id")?.summary =
            settings.deviceId ?: getString(R.string.n_a)

        findPreference<EditTextPreference>("gateway.username")?.setSummaryProvider {
            settings.username ?: getString(R.string.not_set)
        }

        findPreference<EditTextPreference>("gateway.password")?.apply {
            isEnabled = settings.username != null && settings.password != null

            setSummaryProvider {
                when {
                    settings.username == null -> getString(R.string.not_registered)
                    settings.username != null && settings.password == null -> "Can't be changed"
                    else -> settings.password
                }
            }

            setOnPreferenceChangeListener { _, newValue ->
                val value = newValue as? String
                if (value == null || value.length < 14) {
                    showToast(getString(R.string.password_must_be_at_least_14_characters))
                    return@setOnPreferenceChangeListener false
                }

                this@WhatSmsServerSettingsFragment.lifecycleScope.launch {
                    try {
                        requireActivity().findViewById<View>(R.id.progressBar).isVisible = true
                        service.changePassword(settings.password ?: "", value)
                        listView.adapter?.notifyDataSetChanged()
                        showToast(getString(R.string.password_changed_successfully))
                    } catch (e: Exception) {
                        showToast(getString(R.string.failed_to_change_password, e.message))
                    } finally {
                        requireActivity().findViewById<View>(R.id.progressBar).isVisible = false
                    }
                }

                true
            }
        }

        findPreference<Preference>("action.pair_device")?.setOnPreferenceClickListener {
            startActivityForResult(
                Intent(requireContext(), ScanPairingActivity::class.java),
                REQ_PAIR
            )
            true
        }

        findPreference<Preference>("gateway.login_code")?.apply {
            isVisible = settings.username != null && settings.password != null

            onPreferenceClickListener = Preference.OnPreferenceClickListener {
                this@WhatSmsServerSettingsFragment.lifecycleScope.launch {
                    try {
                        requireActivity().findViewById<View>(R.id.progressBar).isVisible = true

                        val loginCode = service.getLoginCode()
                        title = getString(
                            R.string.login_code_expires,
                            DateFormat.getDateTimeInstance().format(loginCode.validUntil)
                        )
                        summary = loginCode.code

                        listView.adapter?.notifyDataSetChanged()
                        showToast(getString(R.string.success_long_press_to_copy))
                    } catch (e: Exception) {
                        showToast(getString(R.string.failed_to_get_login_code, e.message))
                    } finally {
                        requireActivity().findViewById<View>(R.id.progressBar).isVisible = false
                    }
                }

                true
            }
        }
    }

    companion object {
        private const val REQ_PAIR = 1001
    }

    override fun onDisplayPreferenceDialog(preference: Preference) {
        if (preference.key == "gateway.password") {
            (preference as EditTextPreference).setOnBindEditTextListener {
                it.inputType = InputType.TYPE_CLASS_TEXT
                it.text = null
            }
        }

        super.onDisplayPreferenceDialog(preference)
    }
}
