package org.hso.ecommerce.controller.intern.suppliers;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import org.hso.ecommerce.entities.booking.Booking;
import org.hso.ecommerce.entities.booking.BookingAccountEntry;
import org.hso.ecommerce.entities.supplier.Supplier;
import org.hso.ecommerce.entities.supplier.SupplierOrder;
import org.hso.ecommerce.repos.booking.BookingAccountEntryRepository;
import org.hso.ecommerce.repos.booking.BookingRepository;
import org.hso.ecommerce.repos.supplier.SupplierOrderRepository;
import org.hso.ecommerce.repos.supplier.SupplierRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/intern/")
public class SupplierIndexController {

	@Autowired
	private final SupplierRepository supplierRepository = null;

	@Autowired
	private final SupplierOrderRepository supplierOrderRepository = null;

	@Autowired
	private final BookingAccountEntryRepository bookingAccountEntryRepository = null;

	@Autowired
	private final BookingRepository bookingRepository = null;

	@GetMapping("suppliers")
	public String listSuppliers(Model model) {

		List<UImodelSuppliers> totals = new ArrayList<UImodelSuppliers>();

		for (Supplier supplier : supplierRepository.findAll()) {
			UImodelSuppliers tmp = new UImodelSuppliers(supplier.id, supplier.name);
			totals.add(tmp);
		}

		model.addAttribute("suppliers", totals);
		return "intern/suppliers/index";
	}

	@GetMapping("/suppliers/{id}")
	public String supplierDetail(Model model, @PathVariable String id) {

		long supplierId = Long.parseLong(id);

		// add orders from supplier to UImodel
		List<UImodelSupplierDetailOrders> orders = new ArrayList<UImodelSupplierDetailOrders>();
		for (SupplierOrder supplierOrder : supplierOrderRepository.findOrderBySupplierID(supplierId)) {
			orders.add(new UImodelSupplierDetailOrders(supplierOrder));
		}

		// get latest supplier booking
		Optional<BookingAccountEntry> supplierBooking = bookingAccountEntryRepository.getBySupplier(supplierId);

		// get account balance
		String supplierBalance = ((supplierBooking.isPresent())
				? String.format("%.2f", ((float) supplierBooking.get().newSumCent / 100))
				: "0,00");

		// add bookings from supplier to UImodel
		List<UImodelSupplierDetailBookings> bookings = new ArrayList<UImodelSupplierDetailBookings>();
		for (Booking booking : bookingRepository.getBySupplier(supplierId)) {
			bookings.add(new UImodelSupplierDetailBookings(booking));
		}

		UImodelSupplierDetail total = new UImodelSupplierDetail(supplierRepository.findSupplierById(supplierId).name,
				supplierBalance, orders, bookings);

		model.addAttribute("SupplierDetail", total);

		return "intern/suppliers/id";
	}

	public class UImodelSuppliers {
		long id;
		String name;

		public long getId() {
			return id;
		}

		public void setId(long id) {
			this.id = id;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public UImodelSuppliers(long id, String name) {
			this.id = id;
			this.name = name;
		}

	}

	public class UImodelSupplierDetail {

		String name;
		String balance;
		List<UImodelSupplierDetailOrders> orders;
		List<UImodelSupplierDetailBookings> bookings;

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getBalance() {
			return balance;
		}

		public void setBalance(String balance) {
			this.balance = balance;
		}

		public List<UImodelSupplierDetailOrders> getOrders() {
			return orders;
		}

		public void setOrders(List<UImodelSupplierDetailOrders> orders) {
			this.orders = orders;
		}

		public List<UImodelSupplierDetailBookings> getBookings() {
			return bookings;
		}

		public void setBookings(List<UImodelSupplierDetailBookings> bookings) {
			this.bookings = bookings;
		}

		public UImodelSupplierDetail(String name, String balance, List<UImodelSupplierDetailOrders> orders,
				List<UImodelSupplierDetailBookings> bookings) {
			this.name = name;
			this.balance = balance;
			this.orders = orders;
			this.bookings = bookings;
		}

	}

	public class UImodelSupplierDetailOrders {
		long id;
		String dateOrder;
		String articleName;
		long articleId;
		String priceNetto;
		String quantity;
		String price_total;
		boolean arrived;

		public long getId() {
			return id;
		}

		public void setId(long id) {
			this.id = id;
		}

		public String getDateOrder() {
			return dateOrder;
		}

		public void setDateOrder(String dateOrder) {
			this.dateOrder = dateOrder;
		}

		public String getArticleName() {
			return articleName;
		}

		public void setArticleName(String articleName) {
			this.articleName = articleName;
		}

		public String getPriceNetto() {
			return priceNetto;
		}

		public void setPriceNetto(String priceNetto) {
			this.priceNetto = priceNetto;
		}

		public String getQuantity() {
			return quantity;
		}

		public void setQuantity(String quantity) {
			this.quantity = quantity;
		}

		public String getPrice_total() {
			return price_total;
		}

		public void setPrice_total(String price_total) {
			this.price_total = price_total;
		}

		public long getArticleId() {
			return articleId;
		}

		public void setArticleId(long articleId) {
			this.articleId = articleId;
		}

		public boolean isArrived() {
			return arrived;
		}

		public void setArrived(boolean arrived) {
			this.arrived = arrived;
		}

		public UImodelSupplierDetailOrders(SupplierOrder order) {
			this.id = order.id;
			this.articleName = order.ordered.title;
			this.articleId = order.ordered.id;
			this.priceNetto = String.format("%.2f", ((float) order.pricePerUnitNetCent / 100));
			this.quantity = String.valueOf(order.numberOfUnits);
			this.price_total = String.format("%.2f", ((float) order.totalPriceNet / 100));

			Date date = new Date();
			date.setTime(order.created.getTime());
			this.dateOrder = new SimpleDateFormat("dd.MM.yyyy").format(date);

			if (order.delivered != null) {
				arrived = true;
			} else {
				arrived = false;
			}
		}
	}

	public class UImodelSupplierDetailBookings {

		String dateBooking;
		String price;
		String srcName;
		String balance;
		String reason;
		long orderID;

		public String getDateBooking() {
			return dateBooking;
		}

		public void setDateBooking(String dateBooking) {
			this.dateBooking = dateBooking;
		}

		public String getPrice() {
			return price;
		}

		public void setPrice(String price) {
			this.price = price;
		}

		public String getSrcName() {
			return srcName;
		}

		public void setSrcName(String srcName) {
			this.srcName = srcName;
		}

		public String getBalance() {
			return balance;
		}

		public void setBalance(String balance) {
			this.balance = balance;
		}

		public String getReason() {
			return reason;
		}

		public void setReason(String reason) {
			this.reason = reason;
		}

		public long getOrderID() {
			return orderID;
		}

		public void setOrderID(long orderID) {
			this.orderID = orderID;
		}

		public UImodelSupplierDetailBookings(Booking booking) {
			Date date = new Date();
			date.setTime(booking.reason.supplierOrder.created.getTime());
			this.dateBooking = new SimpleDateFormat("dd.MM.yyyy").format(date);
			this.price = String.format("%.2f", ((float) booking.amountCent / 100));
			this.srcName = ((booking.source.isMainAccount) ? "Hauptkonto" : booking.source.supplierAccount.name);
			this.balance = String.format("%.2f", ((float) booking.destination.newSumCent / 100));
			this.reason = booking.reason.comment;
			this.orderID = booking.reason.supplierOrder.id;
		}
	}
}
