package shop.frontend;

import shop.backend.Cart;
import shop.backend.CartPosition;
import shop.backend.OnlineShop;
import shop.backend.Order;
import shop.backend.Product;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class ShopTUI {
    private static OnlineShop onlineShop;
    private static Cart cart;
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws FileNotFoundException {
        IO.println("\n+--------------------------------------+");
        IO.println("|     WELCOME TO THE ONLINE SHOP      |");
        IO.println("+--------------------------------------+");
        onlineShop = new OnlineShop();
        cart = new Cart();
        onlineShop.initShop("Shop/resources/produkte.csv");
        run();
    }

    public static void menu() {
        IO.println("\n+--------------------------------------+");
        IO.println("|1. Show all products                 |");
        IO.println("|2. Search products                   |");
        IO.println("|3. Add to cart                       |");
        IO.println("|4. Show cart                         |");
        IO.println("|5. Edit cart                         |");
        IO.println("|6. Checkout                          |");
        IO.println("|7. Exit                              |");
        IO.println("+--------------------------------------+");
        IO.print("Your choice: ");
    }

    public static void showProducts() {
        IO.println("\nProduct overview:");

        for (Product prod : onlineShop.getLager()) {
            IO.println(prod);
        }

    }

    public static void run() {
        int choice;
        do {
            menu();
            choice = scanner.nextInt();
            switch (choice) {
                case 1:
                    showProducts();
                    break;
                case 2:
                    search();
                    break;
                case 3:
                    addToCart();
                    break;
                case 4:
                    showCart();
                    break;
                case 5:
                    editCart();
                    break;
                case 6:
                    checkout();
                    break;
                case 7:
                    exit();
                    break;

            }
        } while (choice != 7);
    }

    public static void exit() {
        IO.println("Thank you for your visit!");
    }

    public static void search() {
        String keyWord = IO.readln("Search term: ");
        ArrayList<Product> results = onlineShop.seachByName(keyWord);

        if (results.isEmpty()) {
            IO.println("No products found!");
        } else {
            IO.println("\nSearch results:");

            for (Product pr : results) {
                IO.println(pr);
            }
            IO.println();
        }
    }

    public static void addToCart() {
        String strId = IO.readln("Product ID: ");
        int id = Integer.parseInt(strId);
        Product p = onlineShop.getProdId(id);

        if (p == null) {
            IO.println("Product not found!");
            return;
        }

        String strQuantity = IO.readln("Quantity: ");
        int quantity = Integer.parseInt(strQuantity);
        boolean success = cart.addProduct(p, quantity);
        if (success) {
            IO.println("Added: " + p.getName() + " x " + quantity);
        } else {

            IO.println("Not enough in stock!");
            IO.println("Available: " + p.getStock());
        }
    }

    public static void showCart() {
        if (cart.isEmpty()) {
            IO.println("Cart is empty.");
            return;
        }



        IO.println("\nYour shopping cart:");
        IO.println("----------------------------------------");
        for (CartPosition pos : cart.getPositions()) {
            IO.println(pos.getProduct());
        }
        IO.println("----------------------------------------");
        IO.println(String.format("Product value (net):       %6.2f €", cart.getTotalNet()));
        IO.println(String.format("TOTAL:                     %6.2f €", cart.getTotalGross()));
        IO.println("----------------------------------------");
    }

    public static void editCart() {
        if (cart.isEmpty()) {
            IO.println("Cart is empty!");
            return;
        }

        showCart();
        String strId = IO.readln("Product ID to change: ");
        int id = Integer.parseInt(strId);
        for (CartPosition pos : cart.getPositions()) {
            if (pos.getProduct().getProdID() == id) {
                String strQuantity = IO.readln("New quantity (0 to remove): ");
                int quantity = Integer.parseInt(strQuantity);
                cart.updateProductQuantity(id, quantity);
                IO.println("Cart updated!");
                break;
            }
        }
    }

    public static void printConfirmation(Order order) {
        IO.println("----------------------------------------");
        IO.println("          ORDER CONFIRMATION           ");
        IO.println("----------------------------------------");
        IO.println("Customer: " + order.getCustomerName());
        IO.println("Delivery address: " + order.getAddress());
        IO.println("----------------------------------------");
        IO.println("Your order:");

        for (CartPosition pos : order.getPositions()) {
            IO.println("  " + pos.getProduct().getName() + " x " + pos.getQuantity());
        }

        IO.println("----------------------------------------");
        IO.println(String.format("Product net value:             %6.2f €", cart.getTotalNet()));
        IO.println(String.format("Final total product value:     %6.2f €", cart.getTotalGross()));
        IO.println("----------------------------------------");
        IO.println("Thank you for your order, lil bro!");
        IO.println("----------------------------------------");
    }

    public static void checkout() {
        if (cart.isEmpty()) {
            IO.println("Cart is empty!");
            return;
        }

        IO.println("\n--- CHECKOUT ---");
        String name = IO.readln("Name: ");
        String address = IO.readln("Delivery address: ");

        Order successOrder = onlineShop.checkOutLogic(cart, name,address);
        if(successOrder == null){
            IO.println("Order not successful! -> not enough in stock...");
        }else {
            printConfirmation(successOrder);
            cart.clear();
            IO.println("Cart has been cleared. You can continue shopping!");
        }
    }
}