/*
 * TimeLimit Copyright <C> 2019 - 2023 Jonas Lochmann
 *
 * 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 version 3 of the License.
 *
 * 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 <https://www.gnu.org/licenses/>.
 */
package io.timelimit.android.ui.setup


import android.os.Bundle
import android.text.method.LinkMovementMethod
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import io.timelimit.android.R
import io.timelimit.android.databinding.FragmentSetupTermsBinding
import io.timelimit.android.logic.DefaultAppLogic
import io.timelimit.android.ui.model.UpdateStateCommand
import io.timelimit.android.ui.model.execute
import io.timelimit.android.ui.obsolete.ObsoleteDialogFragment
import io.timelimit.android.ui.setup.customserver.SelectCustomServerDialogFragment

class SetupTermsFragment : Fragment() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if (savedInstanceState == null) {
            ObsoleteDialogFragment.show(activity!!, true)
        }
    }

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

        binding.btnAccept.setOnClickListener {
            acceptTerms()
        }

        binding.customServerButton.setOnClickListener {
            SelectCustomServerDialogFragment().show(fragmentManager!!)
        }

        binding.termsText1.movementMethod = LinkMovementMethod.getInstance()
        binding.termsText2.movementMethod = LinkMovementMethod.getInstance()

        DefaultAppLogic.with(context!!).database.config().getCustomServerUrlAsync().observe(viewLifecycleOwner, Observer {
            if (it.isEmpty()) {
                binding.customServerStatus.setText(R.string.custom_server_status_disabled)
            } else {
                binding.customServerStatus.setText(getString(R.string.custom_server_status_enabled, it))
            }
        })

        return binding.root
    }

    private fun acceptTerms() {
        requireActivity().execute(UpdateStateCommand.Setup.Help)
    }
}
