package com.bytesw.bytebot.controller;

import com.bytesw.bytebot.bean.AirportBean;
import com.bytesw.bytebot.service.AirportService;
import com.bytesw.xdf.annotation.ProgramSecurity;
import com.bytesw.xdf.controller.XDFController;
import com.google.gson.GsonBuilder;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.log4j.Log4j2;
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.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController()
@RequestMapping("/service/settings/airport")
@ProgramSecurity("BUSINESS-PARAMETERS")
@Log4j2
public class AirportController extends XDFController<AirportBean, Long> {
    @Autowired
    private AirportService service;

    @Autowired
    protected GsonBuilder gsonBuilder;

    public AirportController(AirportService service) {
        super(service);
    }

    @ApiResponses({@ApiResponse(
            code = 200,
            message = "Success",
            response = ResponseEntity.class
    ), @ApiResponse(
            code = 404,
            message = "Not Found"
    ), @ApiResponse(
            code = 505,
            message = "Internal Server Error"
    )})
    @RequestMapping(
            value = {"/{id}"},
            method = {RequestMethod.DELETE}
    )
    public ResponseEntity<String> delete(@PathVariable("id") Long id, HttpServletRequest req) {
        this.service.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }

    @RequestMapping(
            value = {"/"},
            method = {RequestMethod.POST}
    )
    @ApiResponses({@ApiResponse(
            code = 200,
            message = "Success",
            response = ResponseEntity.class
    ), @ApiResponse(
            code = 409,
            message = "Already Exists"
    ), @ApiResponse(
            code = 505,
            message = "Internal Server Error"
    )})
    public ResponseEntity<String> create(@RequestBody AirportBean bean, HttpServletRequest req) {
        bean = this.service.create(bean);
        return new ResponseEntity(this.gson.toJson(bean), HttpStatus.OK);
    }

    @RequestMapping(
            value = {"/{id}"},
            method = {RequestMethod.PUT}
    )
    @ApiResponses({@ApiResponse(
            code = 200,
            message = "Success",
            response = ResponseEntity.class
    ), @ApiResponse(
            code = 404,
            message = "Not Found"
    ), @ApiResponse(
            code = 505,
            message = "Internal Server Error"
    )})
    public ResponseEntity<String> update(@PathVariable("id") Long id, @RequestBody AirportBean bean, HttpServletRequest req) {
        try {
            this.service.update(bean, id);
        } catch (ObjectOptimisticLockingFailureException var6) {
            AirportBean actualBean = this.service.getById(id);
            return new ResponseEntity(this.gson.toJson(actualBean), HttpStatus.CONFLICT);
        }

        return new ResponseEntity(HttpStatus.OK);
    }
}
