package com.example.vdcscore.ui.definicoes;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

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

import com.example.vdcscore.databinding.FragmentDefinicoesBinding;

/**
 * Tela de definições com ações simples para o utilizador.
 */
public class DefinicoesFragment extends Fragment {

    private FragmentDefinicoesBinding binding;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
            @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {
        binding = FragmentDefinicoesBinding.inflate(inflater, container, false);
        setupUi();
        return binding.getRoot();
    }

    // teste
    private void setupUi() {
        binding.cardConta.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Criar Intent para abrir ContaActivity
                Intent intent = new Intent(getContext(), ContaActivity.class);
                startActivity(intent);

                // Adicionar animação (opcional)
                requireActivity().overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            }
        });

        binding.switchNotifications
                .setOnCheckedChangeListener((buttonView, isChecked) -> binding.textNotificationsStatus.setText(
                        isChecked ? "Ativadas" : "Desativadas"));

        // Obter SharedPreferences
        android.content.SharedPreferences prefs = requireActivity().getSharedPreferences("SettingsPrefs",
                android.content.Context.MODE_PRIVATE);
        boolean isDarkMode = prefs.getBoolean("dark_mode", false);

        binding.switchDarkMode.setChecked(isDarkMode);
        binding.textDarkModeStatus.setText(isDarkMode ? "Ativo" : "Inativo");

        binding.switchDarkMode.setOnCheckedChangeListener((buttonView, isChecked) -> {
            binding.textDarkModeStatus.setText(isChecked ? "Ativo" : "Inativo");

            // Guardar preferência
            android.content.SharedPreferences.Editor editor = prefs.edit();
            editor.putBoolean("dark_mode", isChecked);
            editor.apply();

            // Aplicar modo escuro
            if (isChecked) {
                androidx.appcompat.app.AppCompatDelegate
                        .setDefaultNightMode(androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES);
            } else {
                androidx.appcompat.app.AppCompatDelegate
                        .setDefaultNightMode(androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO);
            }
        });

        binding.btnOpenSystemSettings.setOnClickListener(v -> {
            Intent intent = new Intent(Settings.ACTION_SETTINGS);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}
