package com.altimetrik.ee.demo.controller;


import com.altimetrik.ee.demo.bean.Stocks;
import com.altimetrik.ee.demo.service.StocksService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/stocks")
public class StocksController {

  @Autowired
  StocksService stocksService;

  @GetMapping(value = "/stockQuote", produces = "application/json")
  public ResponseEntity<List<Stocks>>
  getSpecificStocks(@RequestParam(value="symbols", required = false) List<String> symbols) {
    try {
      List<Stocks> stocksList = new ArrayList<Stocks>();
      if (symbols == null)
        stocksService.getStocks().forEach(stocksList::add);
      else if (symbols.size()<=10) {
        stocksService.getStocksByIds(symbols).forEach(stocksList::add);
      }
      else
        return new ResponseEntity<>(HttpStatus.PAYLOAD_TOO_LARGE);
      if (stocksList.isEmpty()) {
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
      }
      return new ResponseEntity<>(stocksList, HttpStatus.OK);
    } catch (Exception e) {
      return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
    }
  }
}
