package com.bytesw.bytebot.controller;

import com.bytesw.bytebot.bean.AgentBean;
import com.bytesw.bytebot.bean.ReservationBean;
import com.bytesw.bytebot.service.ReservationService;
import com.bytesw.xdf.annotation.ProgramSecurity;
import com.bytesw.xdf.controller.XDFController;
import com.bytesw.xdf.sql.beans.Pagination;
import com.google.gson.GsonBuilder;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.orm.ObjectOptimisticLockingFailureException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController()
@RequestMapping("/service/settings/reservation")
@ProgramSecurity("BUSINESS-PARAMETERS")
@Log4j2
public class ReservationController {

    @Autowired
    private ReservationService service;

    @Autowired
    private GsonBuilder gson;


    @DeleteMapping(value = "/{id}")
    public ResponseEntity<String> delete(@PathVariable("id") Long id, HttpServletRequest req) {
        this.service.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }

    @PostMapping(value = "/")
    public ResponseEntity<String> create(@RequestBody ReservationBean bean, HttpServletRequest req) {
        this.service.create(bean);
        return new ResponseEntity(gson.create().toJson(bean), HttpStatus.OK);
    }

    @PutMapping(value = "/{id}")
    public ResponseEntity<String> update(@PathVariable("id") Long id, @RequestBody ReservationBean bean, HttpServletRequest req) {
        try {
            this.service.update(bean, id);
        } catch (ObjectOptimisticLockingFailureException var6) {
            return new ResponseEntity(gson.create().toJson("error"), HttpStatus.CONFLICT);
        }

        return new ResponseEntity(HttpStatus.OK);
    }

    @GetMapping(value = "/{id}")
    public ResponseEntity<String> findOne(@ApiParam(value = "id", required = true) @PathVariable("id") Long id, HttpServletRequest req) {
        try {
            return new ResponseEntity<>(gson.create().toJson(this.service.getReservation(id)), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(gson.create().toJson(ExceptionUtils.getFullStackTrace(e)), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @GetMapping(value = "/")
    public ResponseEntity<String> findAll(HttpServletRequest req) {
        try {
            return new ResponseEntity<>(gson.create().toJson(this.service.getReservationAll()), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(gson.create().toJson("Error detectado al obtener informacion"), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping(value = "/page")
    public ResponseEntity<String> paginationReservation(@RequestBody Pagination<ReservationBean> pagination) {
        try {
            this.service.searchByPagination(pagination);
            return new ResponseEntity<>(gson.create().toJson(pagination), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(gson.create().toJson(ExceptionUtils.getFullStackTrace(e)), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
