package com.jambotronGroup.jambotron.controllers;

import com.jambotronGroup.jambotron.model.Bean;
import com.jambotronGroup.jambotron.model.NameValueItem;
import com.jambotronGroup.jambotron.model.Setting;
import com.jambotronGroup.jambotron.system.SystemService;
import org.springframework.beans.factory.NamedBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "http://localhost:4200", maxAge = 3600, allowCredentials="true")
@RestController
@RequestMapping("/api/system")
public class SystemController {

    @Autowired
    SystemService systemService;





    @GetMapping("/data-source-properties")
    public ResponseEntity<List<NameValueItem>> getDataSourceProperties(@RequestParam(required = false) String title) {
        try {
            List<NameValueItem> properties =  systemService.getDataSourceProperties();
            //userRepository.findAll().forEach(users::add);


            if (properties.isEmpty()) {
                return new ResponseEntity<>(HttpStatus.NO_CONTENT);
            }

            return new ResponseEntity<>(properties, HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }


    @GetMapping("/beans")
    public ResponseEntity<List<Bean>> getLoadedBeans(@RequestParam(required = false) String title) {
        try {
            List<Bean> beans =  systemService.getLodedBeans();
            //userRepository.findAll().forEach(users::add);


            if (beans.isEmpty()) {
                return new ResponseEntity<>(HttpStatus.NO_CONTENT);
            }

            return new ResponseEntity<>(beans, HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
    @GetMapping("/beans/custom")
    public ResponseEntity<List<Bean>> getLoadedCustomBeans(@RequestParam(required = false) String title) {
        try {
            List<Bean> beans =  systemService.getLodedCustomBeans();
            //userRepository.findAll().forEach(users::add);


            if (beans.isEmpty()) {
                return new ResponseEntity<>(HttpStatus.NO_CONTENT);
            }

            return new ResponseEntity<>(beans, HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
