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

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

import org.hso.ecommerce.entities.supplier.SupplierOrder;
import org.hso.ecommerce.repos.supplier.SupplierOrderRepository;
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.RequestMapping;

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

	@Autowired
	private final SupplierOrderRepository supplierOrderRepository = null;

	@GetMapping("supplierOrders")
	public String listSuppliers(Model model) {
		
		List<UImodelSupplierOrder> totals = new ArrayList<UImodelSupplierOrder>();

		for (SupplierOrder orders : supplierOrderRepository.findAll()) {
			totals.add(new UImodelSupplierOrder(orders));
		}

		model.addAttribute("orders", totals);

		return "intern/supplierOrders/index";
	}

	public class UImodelSupplierOrder {
		long id;
		String dateOrder;
		String supplierName;
		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 String getSupplierName() {
			return supplierName;
		}

		public void setSupplierName(String supplierName) {
			this.supplierName = supplierName;
		}
		
		public UImodelSupplierOrder(SupplierOrder order) {
			this.id = order.id;
			this.supplierName = order.supplier.name;
			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;
			}
		}
	}
}
