package pl.koszela.jan.layer.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;
import pl.koszela.jan.layer.model.domain.Price;
import pl.koszela.jan.layer.model.domain.impl.Item;
import pl.koszela.jan.layer.model.service.impl.DefaultModelService;
import pl.koszela.jan.layer.service.ProductService;

/**
 * Created on 12.08.2017.
 *
 * @author Jan Koszela
 */
@Service("productService")
@DependsOn("modelService")
//@Component("productService")
//@Qualifier("productService")
public class DefaultProductService implements ProductService {

  private Map<Item, Price> products;

  @Autowired
  @Qualifier("modelService")
  private DefaultModelService modelService;

  public DefaultProductService() {
    System.out.println(this.getClass().getCanonicalName());
    System.out.println(modelService.getItems());

    putProductsFromModel();
  }

  private void putProductsFromModel() {
    /*
     * We assume that the data's are ordered in model layer without lack of id's
     */
//     modelService = new DefaultModelService();

    products = new TreeMap<>();
//    Item[] items = modelService.getItems();
//    Price[] prices = modelService.getStockPrices();
//
//    for (int i = 0; (i < items.length) && (i < prices.length); i++) {
//      if (items[i].getId() == prices[i].getId()) {
//        products.put(items[i], prices[i]);
//      }
//    }

    /*
     * Data should be loaded in order.
     * If will be some lack of data during loading in the "model layer",
     * that the "product service" should put only valid and matched data.
     */
//    Iterator<Item> items = Arrays.asList(modelService.getItems()).iterator();
//    Iterator<Price> prices = Arrays.asList(modelService.getStockPrices()).iterator();
//
//    while(items.hasNext() && prices.hasNext()){
//      Item item = items.next();
//      Price price = prices.next();
//      if (item.getId() == price.getId()) {
//        products.put(item, price);
//      }
//    }
  }

  @Override
  public Map<Item, Price> getProducts() {
    return this.products;
  }

  @Override
  public Item getItem(int id) {
    for (Item item : products.keySet()) {
      if (item.getId() == id) {
        return item;
      }
    }
    return null;
  }

  @Override
  public Item getItem(String name) {
    for (Item item : products.keySet()) {
      if (item.getName().equals(name)) {
        return item;
      }
    }
    return null;
  }

  @Override
  public Price getPrice(Item item) {
    for (Price price : products.values()) {
      if (price.getId() == item.getId()) {
        return price;
      }
    }
    return null;
  }

  @Override
  public Price getPrice(int id) {
    for (Price price : products.values()) {
      if (price.getId() == id) {
        return price;
      }
    }
    return null;
  }

  @Override
  public Price getPrice(String productName) {
    for (Map.Entry<Item, Price> entry : products.entrySet()) {
      if (entry.getKey().getName().equals(productName)) {
        return entry.getValue();
      }
    }
    return null;
  }

  @Override
  public Price[] getStockPrices() {
    return modelService.getStockPrices();
  }

  @Override
  public List<Price> getListStockPrice() {
    return Arrays.asList(modelService.getStockPrices());
  }

}

@@ -129,24 +129,63 @@
 il.TreeMap;%0A
+import javax.annotation.PostConstruct;%0A
 import org.s
@@ -239,128 +239,8 @@
 ed;%0A
-import org.springframework.beans.factory.annotation.Qualifier;%0Aimport org.springframework.context.annotation.DependsOn;%0A
 impo
@@ -428,20 +428,8 @@
 ice.
-impl.Default
 Mode
@@ -582,97 +582,8 @@
 e%22)%0A
-@DependsOn(%22modelService%22)%0A//@Component(%22productService%22)%0A//@Qualifier(%22productService%22)%0A
 publ
@@ -697,37 +697,8 @@
 red%0A
-  @Qualifier(%22modelService%22)%0A
   pr
@@ -703,23 +703,16 @@
 private 
-Default
 ModelSer
@@ -737,183 +737,22 @@
 %0A%0A  
-public DefaultProductService() %7B%0A    System.out.println(this.getClass().getCanonicalName());%0A    System.out.println(modelService.getItems());%0A%0A    putProductsFromModel();%0A  %7D%0A
+@PostConstruct
 %0A  p
@@ -887,57 +887,8 @@
   */
-%0A//     modelService = new DefaultModelService();
 %0A%0A  
@@ -913,26 +913,24 @@
 reeMap%3C%3E();%0A
-//
     Item%5B%5D i
@@ -957,26 +957,24 @@
 getItems();%0A
-//
     Price%5B%5D 
@@ -1009,29 +1009,25 @@
 ckPrices();%0A
-//%0A//
+%0A
     for (int
@@ -1080,26 +1080,24 @@
 th); i++) %7B%0A
-//
       if (it
@@ -1127,34 +1127,32 @@
 s%5Bi%5D.getId()) %7B%0A
-//
         products
@@ -1178,26 +1178,24 @@
 es%5Bi%5D);%0A
-//
       %7D%0A
 //    %7D%0A
@@ -1182,26 +1182,24 @@
 %5D);%0A      %7D%0A
-//
     %7D%0A%0A    /

Fix initialization process for ProductService which one is based on modelService