package com.bytesw.bytebot.controller;

import com.bytesw.bytebot.bean.PassengerBean;
import com.bytesw.bytebot.service.PassengerService;
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;
import java.math.BigInteger;

@RestController()
@RequestMapping("/service/settings/passenger")
@ProgramSecurity("BUSINESS-PARAMETERS")
@Log4j2
public class PassengerController extends XDFController<PassengerBean, Long> {

    @Autowired
    private PassengerService service;

    @Autowired
    protected GsonBuilder gsonBuilder;

    public PassengerController(PassengerService 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 PassengerBean 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 PassengerBean bean, HttpServletRequest req) {
        try {
            this.service.update(bean, id);
        } catch (ObjectOptimisticLockingFailureException var6) {
            PassengerBean actualBean = this.service.getById(id);
            return new ResponseEntity(this.gson.toJson(actualBean), HttpStatus.CONFLICT);
        }

        return new ResponseEntity(HttpStatus.OK);
    }
}
