package com.example.petstoremobile.fragments;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.fragment.NavHostFragment;

import android.view.LayoutInflater;

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

import com.example.petstoremobile.R;
import com.example.petstoremobile.api.auth.TokenManager;
import com.example.petstoremobile.databinding.FragmentListBinding;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

//The Fragment for the displaying the list of entities to be viewed
@AndroidEntryPoint
public class ListFragment extends Fragment {

    private FragmentListBinding binding;
    private NavController innerNavController;

    @Inject TokenManager tokenManager;

    /**
     * Inflates the fragment layout, initializes navigation drawers, and applies role-based access control.
     */
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentListBinding.inflate(inflater, container, false);

        // Check user role and restrict access for STAFF
        String role = tokenManager.getRole();
        if ("STAFF".equalsIgnoreCase(role)) {
            binding.sectionAdmin.setVisibility(View.GONE);
        } else if ("ADMIN".equalsIgnoreCase(role)) {
            binding.sectionAdmin.setVisibility(View.VISIBLE);
            binding.drawerStaff.setVisibility(View.VISIBLE);
        } else {
            // Default or other roles
            binding.sectionAdmin.setVisibility(View.GONE);
        }

        //add Listeners to the drawer so user won't be able to interact with the innerContainer (the list fragments)
        //while the drawer is open
        binding.drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {

            //When the drawer is opened, disable touches on the background
            @Override
            public void onDrawerOpened(View drawerView) {
                binding.touchBlocker.setVisibility(View.VISIBLE);
                binding.touchBlocker.setClickable(true);
            }

            //When the drawer is closed, enable touches again
            @Override
            public void onDrawerClosed(View drawerView) {
                binding.touchBlocker.setVisibility(View.GONE);
                binding.touchBlocker.setClickable(false);
            }

            //unused methods
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {}
            @Override
            public void onDrawerStateChanged(int newState) {}
        });

        // Click listeners for each drawer
        binding.drawerPets.setOnClickListener(v -> navigateTo(R.id.nav_pet));
        binding.drawerServices.setOnClickListener(v -> navigateTo(R.id.nav_service));
        binding.drawerSuppliers.setOnClickListener(v -> navigateTo(R.id.nav_supplier));
        binding.drawerAdoptions.setOnClickListener(v -> navigateTo(R.id.nav_adoption));
        binding.drawerAppointments.setOnClickListener(v -> navigateTo(R.id.nav_appointment));
        binding.drawerInventory.setOnClickListener(v -> navigateTo(R.id.nav_inventory));
        binding.drawerProducts.setOnClickListener(v -> navigateTo(R.id.nav_product));
        binding.drawerProductSupplier.setOnClickListener(v -> navigateTo(R.id.nav_product_supplier));
        binding.drawerPurchaseOrderView.setOnClickListener(v -> navigateTo(R.id.nav_purchase_order));
        binding.drawerSale.setOnClickListener(v -> navigateTo(R.id.nav_sale));
        binding.drawerStaff.setOnClickListener(v -> navigateTo(R.id.nav_staff));
        binding.drawerCustomers.setOnClickListener(v -> navigateTo(R.id.nav_customer));
        binding.drawerAnalytics.setOnClickListener(v -> navigateTo(R.id.nav_analytics));
        binding.drawerCoupons.setOnClickListener(v -> navigateTo(R.id.nav_coupon));
        binding.drawerActivityLogs.setOnClickListener(v -> navigateTo(R.id.nav_activity_log));

        return binding.getRoot();
    }

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

    /**
     * Initializes the NavController for the internal fragment container.
     */
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        NavHostFragment navHostFragment = (NavHostFragment) getChildFragmentManager()
                .findFragmentById(R.id.inner_nav_host_fragment);
        if (navHostFragment != null) {
            innerNavController = navHostFragment.getNavController();
        }
    }

    /**
     * Navigates to a specific inner destination and closes all drawers.
     */
    private void navigateTo(int destinationId) {
        if (innerNavController != null) {
            innerNavController.navigate(destinationId);
        }
        binding.drawerLayout.closeDrawers();
    }

    /**
     * Programmatically opens the navigation drawer.
     */
    public void openDrawer() {
        binding.drawerLayout.openDrawer(GravityCompat.START);
    }
}
