package com.example.quiz;

import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.quiz.databinding.FragmentApiUrlConfigurationBinding;
import com.example.quiz.utils.MessageDialog;
import com.example.quiz.utils.SharedPrefsHelper;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ApiUrlConfiguration#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ApiUrlConfiguration extends Fragment {

    private FragmentApiUrlConfigurationBinding binding;

    SharedPrefsHelper prefsHelper;

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER

    public ApiUrlConfiguration() {
        // Required empty public constructor
    }

    // TODO: Rename and change types and number of parameters
    public static ApiUrlConfiguration newInstance() {
        ApiUrlConfiguration fragment = new ApiUrlConfiguration();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        prefsHelper = SharedPrefsHelper.getInstance(getContext());
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        binding = FragmentApiUrlConfigurationBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        String serverLink = prefsHelper.get("MAIN_API_URL");
        String depLink = prefsHelper.get("REPORT_API_URL");
        if(serverLink!=null){
            binding.serverLink.setText(serverLink);
        }
        if(depLink!=null){
            binding.depLink.setText(depLink);
        }
        binding.btnValidate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(binding.serverLink.getText().toString().isEmpty()){
                    binding.serverLink.setError("Entrez l'URL du serveur");
                    return;
                }
                if(binding.depLink.getText().toString().isEmpty()){
                    binding.depLink.setError("Entrez l'URL de secours");
                    return;
                }
                saveServerLinks(binding.serverLink.getText().toString(), binding.depLink.getText().toString());
            }
        });
        super.onViewCreated(view, savedInstanceState);
    }

    private void saveServerLinks(String serverLink, String depLink){
        SharedPreferences.Editor editor = prefsHelper.edit();
        editor.putString("MAIN_API_URL", serverLink);
        editor.putString("REPORT_API_URL", depLink);
        editor.apply();
        MessageDialog.showSuccess(getContext(), "URL's enregistrée avec succès");
        requireActivity().finishAffinity();
    }
}