package com.example;

import com.example.App;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class CartController {

    private static final String CART_FILE = "cart.txt";

    @FXML
    private Button cartBtn, loginBtn, btnCheckOut;

    @FXML
    private TableView<Item> cartContent;

    @FXML
    private TableColumn<Item, String> nameColumn;

    @FXML
    private TableColumn<Item, String> descriptionColumn;

    @FXML
    private TableColumn<Item, Integer> quantityColumn;

    @FXML
    private TableColumn<Item, Double> priceColumn;

    @FXML
    private Label cartTitle, mainTitle;

    // Observable list to hold cart items
    private final ObservableList<Item> cartItems = FXCollections.observableArrayList();

    @FXML
    public void initialize() {
        // Set up columns to match Item properties
        nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
        descriptionColumn.setCellValueFactory(new PropertyValueFactory<>("description"));
        quantityColumn.setCellValueFactory(new PropertyValueFactory<>("quantity"));
        priceColumn.setCellValueFactory(new PropertyValueFactory<>("price"));

        // Load cart items from file
        loadCartItemsFromFile();

        // Populate TableView with cart items
        cartContent.setItems(cartItems);
    }

    private void loadCartItemsFromFile() {
        try (BufferedReader reader = new BufferedReader(new FileReader(CART_FILE))) {
            String line;
            while ((line = reader.readLine()) != null) {
                // Assuming each line contains only the book name for simplicity
                cartItems.add(new Item(line, "Book Description", 1, 9.99));
            }
        } catch (IOException e) {
            System.err.println("Error loading cart items: " + e.getMessage());
        }
    }

    @FXML
    private void handleCartButton(ActionEvent event) {
        try {
            App.setRoot("cart");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @FXML
    private void handleLoginButton(ActionEvent event) {
        try {
            App.setRoot("login");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @FXML
    private void handleCheckOutButton(ActionEvent event) {
        if (cartItems.isEmpty()) {
            System.out.println("Cart is empty. Cannot proceed to checkout.");
            return;
        }

        System.out.println("Proceeding to checkout...");
        try {
            App.setRoot("checkOut");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
