package com.example.petstoremobile.fragments.listfragments.detailfragments;

import android.os.Bundle;
import android.view.*;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.fragment.NavHostFragment;

import com.example.petstoremobile.databinding.FragmentPurchaseOrderDetailBinding;
import com.example.petstoremobile.dtos.PurchaseOrderDTO;
import com.example.petstoremobile.utils.Resource;
import com.example.petstoremobile.viewmodels.PurchaseOrderDetailViewModel;

import dagger.hilt.android.AndroidEntryPoint;

/**
 * Fragment for displaying the information of a purchase order.
 */
@AndroidEntryPoint
public class PurchaseOrderDetailFragment extends Fragment {

    private FragmentPurchaseOrderDetailBinding binding;
    private PurchaseOrderDetailViewModel viewModel;
    private long purchaseOrderId;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewModel = new ViewModelProvider(this).get(PurchaseOrderDetailViewModel.class);
    }

    /**
     * Inflates the layout.
     */
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentPurchaseOrderDetailBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    /**
     * Initializes views and populates order data from backend after the view is created.
     */
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        handleArguments();

        binding.btnPOBack.setOnClickListener(v -> {
            NavHostFragment.findNavController(this).popBackStack();
        });
    }

    private void handleArguments() {
        Bundle a = getArguments();
        if (a != null && a.containsKey("purchaseOrderId")) {
            purchaseOrderId = a.getLong("purchaseOrderId");
            loadPurchaseOrderData();
        }
    }

    private void setLoading(boolean loading) {
        if (binding != null && binding.progressBar != null) {
            binding.progressBar.setVisibility(loading ? View.VISIBLE : View.GONE);
        }
    }

    private void loadPurchaseOrderData() {
        viewModel.loadPurchaseOrder(purchaseOrderId).observe(getViewLifecycleOwner(), resource -> {
            if (resource == null) return;
            setLoading(resource.status == Resource.Status.LOADING);
            if (resource.status == Resource.Status.SUCCESS && resource.data != null) {
                PurchaseOrderDTO po = resource.data;
                binding.tvPODetailId.setText("PO #" + po.getPurchaseOrderId());
                binding.tvPODetailSupplier.setText(po.getSupplierName());
                binding.tvPODetailStore.setText(po.getStoreName() != null ? po.getStoreName() : "N/A");
                binding.tvPODetailDate.setText(po.getOrderDate());
            } else if (resource.status == Resource.Status.ERROR) {
                Toast.makeText(getContext(), "Failed to load order: " + resource.message, Toast.LENGTH_SHORT).show();
            }
        });
    }

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