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

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.hso.ecommerce.entities.supplier.ArticleOffer;
import org.hso.ecommerce.repos.shop.ArticleRepository;
import org.hso.ecommerce.repos.shop.OffersRepository;
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 SupplierOfferController {

	@Autowired
	private final OffersRepository offersRepository = null;

	@Autowired
	private final ArticleRepository articleRepository = null;

	@GetMapping("supplierOffers")
	public String internListedArticles(Model model) {

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

		for (ArticleOffer article : offersRepository.findAll()) {
			UImodelOfferedArticle tmp = new UImodelOfferedArticle();
			tmp.addData(article, articleRepository.findArticleIDByRelatedID(article.id));
			totals.add(tmp);
		}

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

	public class UImodelOfferedArticle {

		long offer_id;
		String title;
		String manufacturer;
		String articlenumber;
		String supplierName;
		String price;
		String ads;
		int listedArticleId;
		boolean offerIsListed; // true --> offered article is listed

		public long getOffer_id() {
			return offer_id;
		}

		public void setOffer_id(long offer_id) {
			this.offer_id = offer_id;
		}

		public boolean isOfferIsListed() {
			return offerIsListed;
		}

		public void setOfferIsListed(boolean offerIsListed) {
			this.offerIsListed = offerIsListed;
		}

		public String getTitle() {
			return title;
		}

		public void setTitle(String title) {
			this.title = title;
		}

		public String getManufacturer() {
			return manufacturer;
		}

		public void setManufacturer(String manufacturer) {
			this.manufacturer = manufacturer;
		}

		public String getArticlenumber() {
			return articlenumber;
		}

		public void setArticlenumber(String articlenumber) {
			this.articlenumber = articlenumber;
		}

		public String getSupplierName() {
			return supplierName;
		}

		public void setSupplierName(String supplierName) {
			this.supplierName = supplierName;
		}

		public String getPrice() {
			return price;
		}

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

		public String getAds() {
			return ads;
		}

		public void setAds(String ads) {
			this.ads = ads;
		}

		public int getListedArticleId() {
			return listedArticleId;
		}

		public void setListedArticleId(int listedArticleId) {
			this.listedArticleId = listedArticleId;
		}

		public void addData(ArticleOffer article, Optional<Integer> listedArticleId) {

			this.offer_id = article.id;
			this.title = article.title;
			this.manufacturer = article.manufacturer;
			this.articlenumber = article.articleNumber;
			this.supplierName = article.cheapestSupplier.name;
			this.price = String.format("%.2f", ((float) article.pricePerUnitNet / 100));
			this.ads = (article.shouldBeAdvertised) ? "Ja" : "Nein";

			if (listedArticleId.isPresent()) {
				// this offer is listed --> show link
				this.listedArticleId = listedArticleId.get();
				offerIsListed = true;
			} else {
				// this offer is not listed
				offerIsListed = false;
			}
		}
	}
}
