package com.example.quiz;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.ViewModelProvider;

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

import com.example.quiz.databinding.FragmentSettingsBinding;
import com.example.quiz.utils.AuthNavigator;
import com.example.quiz.utils.SessionManager;
import com.example.quiz.utils.SharedPrefsHelper;
import com.example.quiz.viewModel.LoginViewModel;
import com.example.quiz.viewModel.LogsViewModel;
import com.google.android.material.appbar.MaterialToolbar;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

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

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

    // TODO: Rename and change types of parameters

    FragmentSettingsBinding binding;

    AuthNavigator authNavigator;

    SharedPrefsHelper prefsHelper;

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

    // TODO: Rename and change types and number of parameters
    public static Settings newInstance() {
        Settings fragment = new Settings();
        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(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentSettingsBinding.inflate(inflater, container, false);
        authNavigator = new AuthNavigator(getContext(), getParentFragmentManager(), SessionManager.newInstance(getContext()),new ViewModelProvider(this).get(LoginViewModel.class),new ViewModelProvider(this).get(LogsViewModel.class), this);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState){
        super.onViewCreated(view, savedInstanceState);
        AppCompatActivity activity = (AppCompatActivity) getActivity();
        if(prefsHelper.get("isSupAgent") == null){
            binding.agentManagement.setVisibility(View.GONE);
        }else{
            String isSubAgent = prefsHelper.get("isSupAgent");
            if(isSubAgent.equals("true")){
                binding.agentManagement.setVisibility(View.GONE);
            }else{
                binding.agentManagement.setVisibility(View.VISIBLE);
            }
        }
        if(activity != null){
            MaterialToolbar toolbar = activity.findViewById(R.id.toolbar);
            activity.setSupportActionBar(toolbar);

            if (activity.getSupportActionBar() != null) {
                activity.getSupportActionBar().show();
                activity.getSupportActionBar().setTitle("Choisissez une action à effectuer");
            }
            toolbar.setBackgroundColor(getResources().getColor(R.color.primary_green, null));
            toolbar.setTitleTextColor(getResources().getColor(R.color.white, null));
        }

        binding.logs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Logs logs = Logs.newInstance();
                FragmentManager fragmentManager = getParentFragmentManager();
                fragmentManager.beginTransaction()
                        .replace(R.id.nav_host_fragment_content_main, logs)
                        .addToBackStack(null)
                        .commit();
            }
        });

        binding.serverConfig.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ServerConfig serverConfig = ServerConfig.newInstance();
                FragmentManager fragmentManager = getParentFragmentManager();
                fragmentManager.beginTransaction()
                        .replace(R.id.nav_host_fragment_content_main, serverConfig)
                        .addToBackStack(null)
                        .commit();
            }
        });
        binding.changePin.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                UpdatePin updatePin = UpdatePin.newInstance();
                authNavigator.navigate(updatePin);
            }
        });

        binding.agentManagement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AgentManagement agentManagement = AgentManagement.newInstance();
                authNavigator.navigate(agentManagement);
            }
        });

        binding.logout.setOnClickListener(v -> {
            authNavigator.logOut();
        });
    }


}