package org.ots.app.restctrlr;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.ots.app.dao.OtsDao;
import org.ots.app.domain.GridObject;
import org.ots.app.domain.UserCreateRqst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
public class AppRestController {

	@Autowired
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	
	@Autowired
	OtsDao otsDao;

	@RequestMapping(path = "/getDelta", method = RequestMethod.GET)
	public String getOTSFirstController() {
		int count = namedParameterJdbcTemplate.execute("select count(1) from otsdb.user",
				new PreparedStatementCallback<Integer>() {
					@Override
					public Integer doInPreparedStatement(PreparedStatement ps)
							throws SQLException, DataAccessException {
						ResultSet rs = ps.executeQuery();
						rs.next();
						int rowCount = rs.getInt(1);
						return rowCount;
					}
				});
		System.out.println(count);
		return "hello delta boot " + count;
	}

	@RequestMapping(path = "/getGridData", method = RequestMethod.GET)
	@CrossOrigin(origins = "*")
	public ResponseEntity<List<GridObject>> getGridData() {
		List<GridObject> gridDataList = new ArrayList<>();
		for (int i = 0; i < 200; i++) {
			GridObject e = new GridObject();
			e.setActive(true);
			e.setName("A" + i);
			e.setOid(i);
			e.setOrderDate(new Date().toString());
			int position = (i % 3) + 1;
			e.setPosition(position);
			gridDataList.add(e);
		}
		ResponseEntity<List<GridObject>> response = new ResponseEntity<List<GridObject>>(gridDataList,
				HttpStatus.ACCEPTED);
		return response;
	}

	@RequestMapping(path = "/createUsr", method = RequestMethod.POST)
	@CrossOrigin(origins = "*")
	public ResponseEntity<Boolean> createUser(@RequestBody UserCreateRqst userCreateRqst) {
		otsDao.createUsr(userCreateRqst);
		ResponseEntity<Boolean> response = new ResponseEntity<>(true, HttpStatus.OK);
		return response;
	}
	
	
	@RequestMapping(path = "/isUsrExists", method = RequestMethod.POST)
	@CrossOrigin(origins = "*")
	public ResponseEntity<Boolean> isUsrExists(@RequestParam String uname, @RequestParam String email) {
		ResponseEntity<Boolean> response = new ResponseEntity<>(otsDao.isUserExists(email, uname), HttpStatus.OK);
		return response;
	}

}
