package com.example.quiz;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
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 com.example.quiz.data.adapter.AgentItemAdapter;
import com.example.quiz.data.model.dtos.auth.User;
import com.example.quiz.databinding.FragmentAgentManagementBinding;
import com.example.quiz.utils.AuthNavigator;
import com.example.quiz.utils.LoaderDialog;
import com.example.quiz.utils.MessageDialog;
import com.example.quiz.utils.Result;
import com.example.quiz.utils.SessionManager;
import com.example.quiz.utils.SharedPrefsHelper;
import com.example.quiz.viewModel.AgentViewModel;
import com.example.quiz.viewModel.LoginViewModel;
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 AgentManagement#newInstance} factory method to
 * create an instance of this fragment.
 */
@AndroidEntryPoint
public class AgentManagement extends Fragment {

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

    private FragmentAgentManagementBinding binding;
    private LoaderDialog loaderDialog;

    private AgentViewModel agentViewModel;
    SharedPrefsHelper prefsHelper;

    AgentItemAdapter agentItemAdapter;

    AuthNavigator authNavigator;

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

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        authNavigator = new AuthNavigator(getContext(), getParentFragmentManager(), SessionManager.newInstance(getContext()),new ViewModelProvider(this).get(LoginViewModel.class),new ViewModelProvider(this).get(LogsViewModel.class), this);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        binding = FragmentAgentManagementBinding.inflate(inflater, container, false);
        agentViewModel = new ViewModelProvider(this).get(AgentViewModel.class);
        loaderDialog = new LoaderDialog(getContext());
        prefsHelper = SharedPrefsHelper.getInstance(getContext());
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        AppCompatActivity activity = (AppCompatActivity) getActivity();
        if(activity != null){
            MaterialToolbar toolbar = activity.findViewById(R.id.toolbar);
            activity.setSupportActionBar(toolbar);

            if (activity.getSupportActionBar() != null) {
                activity.getSupportActionBar().show();
                activity.getSupportActionBar().setTitle("Gestion aides");
            }
            toolbar.setBackgroundColor(getResources().getColor(R.color.primary_green, null));
            toolbar.setTitleTextColor(getResources().getColor(R.color.white, null));
        }

        String agentId = prefsHelper.get("id");
        if(agentId == null){
            return;
        }
        agentViewModel.getAgents(agentId).observe(getViewLifecycleOwner(), new Observer<Result<List<User>>>() {
            @Override
            public void onChanged(Result<List<User>> listResult) {
                switch (listResult.status){
                    case LOADING:{
                        loaderDialog.show("Chargement des agents");
                        break;
                    }
                    case ERROR:{
                        loaderDialog.dismiss();
                        MessageDialog.showError(getContext(), listResult.message);
                    }
                    case SUCCESS:{
                        loaderDialog.dismiss();
                        agentItemAdapter = new AgentItemAdapter(listResult.data);
                        binding.agentList.setLayoutManager(new LinearLayoutManager(getContext()));
                        binding.agentList.setAdapter(agentItemAdapter);
                        agentItemAdapter.setOnItemClickListener(new AgentItemAdapter.onItemClickListener() {
                            @Override
                            public void onItemClick(User agent) {
                                AgentDetails agentDetails = AgentDetails.newInstance(agent);
                                authNavigator.navigate(agentDetails);
                            }});
                    }
                }
            }
        });
    }
}