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 android.widget.Toast;

import com.example.quiz.databinding.FragmentLoginBinding;
import com.example.quiz.utils.AuthNavigator;
import com.example.quiz.utils.MessageDialog;
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 dagger.hilt.android.AndroidEntryPoint;

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


    private AuthNavigator authNavigator;
    private int clickCount = 0;
    private long lastClickTime = 0;
    private static final int CLICKS_REQUIRED = 5;
    private static final long TIME_WINDOW = 2000;
    private SharedPrefsHelper prefsHelper;
    public Login() {
        // Required empty public constructor
    }
    public static Login newInstance() {
        Login fragment = new Login();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    private FragmentLoginBinding binding;



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

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentLoginBinding.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(activity != null){
            activity.getSupportActionBar().hide();
        }

        binding.icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                long currentTime = System.currentTimeMillis();
                if (currentTime - lastClickTime > TIME_WINDOW) {
                    clickCount = 0;
                } else {
                    clickCount++;
                }
                lastClickTime = currentTime;
                if(clickCount == CLICKS_REQUIRED){
                    clickCount = 0;
                    ApiUrlConfiguration apiUrlConfiguration = ApiUrlConfiguration.newInstance();
                    FragmentManager fragmentManager = getParentFragmentManager();
                    fragmentManager.beginTransaction()
                            .replace(R.id.nav_host_fragment_content_main, apiUrlConfiguration)
                            .addToBackStack(null)
                            .commit();
                }
            }
        });

    }

   public void onStart(){
       super.onStart();
       binding.loginButton.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               prefsHelper = SharedPrefsHelper.getInstance(getContext());
               ListOFBets bets = ListOFBets.newInstance();
               authNavigator.navigate(bets);
           }
       });

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

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

   }

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