package com.example.quiz;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.quiz.data.adapter.LogsAdapter;
import com.example.quiz.data.dao.UserActivityLogDao;
import com.example.quiz.data.database.DatabaseClient;
import com.example.quiz.databinding.FragmentLogsBinding;
import com.example.quiz.utils.LoaderDialog;
import com.example.quiz.viewModel.LogsViewModel;
import com.google.android.material.appbar.MaterialToolbar;

import java.util.List;

import dagger.hilt.android.AndroidEntryPoint;

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

    FragmentLogsBinding binding;

    LogsViewModel viewModel;

    private LogsAdapter adapter;

    LoaderDialog loader;


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

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentLogsBinding.inflate(inflater, container, false);
        loader = new LoaderDialog(getContext());
        adapter = new LogsAdapter();
        binding.recyclerLogs.setLayoutManager(new LinearLayoutManager(getContext()));
        binding.recyclerLogs.setAdapter(adapter);
        // Inflate the layout for this fragment
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        AppCompatActivity activity = (AppCompatActivity) getActivity();
        if (activity != null) {
            if(activity.getSupportActionBar() != null){
                activity.getSupportActionBar().setTitle("Historique");
                MaterialToolbar toolbar = activity.findViewById(R.id.toolbar);
                toolbar.setBackgroundColor(getResources().getColor(R.color.primary_green));
                toolbar.setTitleTextColor(getResources().getColor(R.color.white));
            }
        }
        viewModel = new ViewModelProvider(this).get(LogsViewModel.class);

        // Observer les logs
        viewModel.getTodayLogs().observe(getViewLifecycleOwner(), new Observer<List<com.example.quiz.data.entity.Logs>>() {
            @Override
            public void onChanged(List<com.example.quiz.data.entity.Logs> logs) {
                loader.show("Chargement des logs");
                if(logs != null){
                    loader.dismiss();
                    adapter.setLogs(logs);
                }else{
                    loader.dismiss();
                    showErrorAlert("Erreur", "Impossible de charger les logs");
                }
            }
        });
    }

    private void showErrorAlert(String title, String message) {
        new AlertDialog.Builder(requireContext())
                .setTitle(title)
                .setMessage(message)
                .setCancelable(true)
                .setPositiveButton("OK", (dialog, which) -> dialog.dismiss())
                .show();
    }

}