package cokr.xit.fims.chongno;

import java.util.Arrays;
import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

import cokr.xit.base.ServletCustomizer;
import cokr.xit.base.docs.jasper.JasperView;
import cokr.xit.fims.FimsConf;
import cokr.xit.fims.SpringBootApplicationBridge;
import cokr.xit.fims.mngt.service.bean.TaskProcessor;

@ImportAutoConfiguration({ServletCustomizer.class})
@ComponentScan(basePackageClasses = cokr.xit.applib.HttpStatusCodeExceptionControllerAdvice.class)
public class MainApplication extends SpringBootApplicationBridge {
	public static void main(String[] args) {
		allowRestrictedHeaders();
		System.setProperty("sgg", FimsConf.get().getSgg());
		SpringApplication.run(MainApplication.class, args);
	}

	public static void allowRestrictedHeaders() {
		String allowRestrictedHeaders = System.getProperty("jdk.httpclient.allowRestrictedHeaders");
		if(allowRestrictedHeaders == null) {
			allowRestrictedHeaders = "";
		}
		List<String> allowRestrictedHeadersArr = Arrays.asList(allowRestrictedHeaders.split(","));

		String[] items = {"connection","host","content-length","expect","upgrade"};

		for(String item : items) {
			if(!allowRestrictedHeadersArr.contains(item)) {
				if(!allowRestrictedHeaders.equals("")) {
					allowRestrictedHeaders += ",";
				}
				allowRestrictedHeaders += item;
			}
		}
		System.setProperty("jdk.httpclient.allowRestrictedHeaders", allowRestrictedHeaders);
	}

	@Bean
	public JasperView jasperView() {
		return new JasperView();
	}

	@Bean("taskProcessor")
	public TaskProcessor taskProcessor() {
		return TaskProcessor.get(ChongnoTaskProcessor::new);
	}
}