package com.flowable.local.work.rest;

import com.flowable.autoconfigure.platform.PropertyConfigurationService;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/*
http://localhost:8090/custom-api/application-property/keyDoesNotExist -> null
http://localhost:8090/custom-api/application-property/string/keyDoesNotExist -> null
http://localhost:8090/custom-api/application-property/keyDoesNotExist?default=abc -> abc
http://localhost:8090/custom-api/application-property/string/keyDoesNotExist?default=abc -> abc
http://localhost:8090/custom-api/application-property/server.port -> 8090
http://localhost:8090/custom-api/application-property/string/server.port -> 8090
http://localhost:8090/custom-api/application-property/server.port?default=abc -> 8090
http://localhost:8090/custom-api/application-property/string/server.port?default=abc -> 8090
http://localhost:8090/custom-api/application-property/boolean/keyDoesNotExist -> false
http://localhost:8090/custom-api/application-property/boolean/keyDoesNotExist?default=true -> true
http://localhost:8090/custom-api/application-property/boolean/flowable.platform.idm.minimal-setup -> true
http://localhost:8090/custom-api/application-property/boolean/flowable.platform.idm.minimal-setup?default=false -> true
http://localhost:8090/custom-api/application-property/integer/keyDoesNotExist -> 0
http://localhost:8090/custom-api/application-property/integer/keyDoesNotExist?default=123 -> true
http://localhost:8090/custom-api/application-property/integer/server.port -> 8090
http://localhost:8090/custom-api/application-property/integer/server.port?default=123 -> 8090

http://localhost:8090/custom-api/application-property/integer/flowable.platform.idm.minimal-setup -> ConversionFailedException
 */

@RestController
@RequestMapping("/custom-api")
@Validated
public class ApplicationPropertyController {

    protected final PropertyConfigurationService propertyConfigurationService;

    public ApplicationPropertyController(PropertyConfigurationService propertyConfigurationService) {
        this.propertyConfigurationService = propertyConfigurationService;
    }

    @GetMapping(value = {"/application-property/{key}", "/application-property/string/{key}"})
    public ResponseEntity<String> getStringVariable(
            @PathVariable String key,
            @RequestParam(name = "default", required = false) String defaultValue) {
        return ResponseEntity.ok(propertyConfigurationService.getProperty(key, defaultValue));
    }

    @GetMapping("/application-property/boolean/{key}")
    public ResponseEntity<Boolean> getBooleanVariable(
            @PathVariable String key,
            @RequestParam(name = "default", required = false) Boolean defaultValue) {
        if (defaultValue == null) {
            defaultValue = false;
        }
        return ResponseEntity.ok(propertyConfigurationService.getBooleanProperty(key, defaultValue));
    }

    @GetMapping("/application-property/integer/{key}")
    public ResponseEntity<Integer> getIntegerVariable(
            @PathVariable String key,
            @RequestParam(name = "default", required = false) Integer defaultValue) {
        if (defaultValue == null) {
            defaultValue = 0;
        }
        return ResponseEntity.ok(propertyConfigurationService.getIntegerProperty(key, defaultValue));
    }
}
