package com.neusoft.smart.pos.user;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import com.neusoft.smart.pos.user.utils.MyDateFormat;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.annotation.Order;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

import java.text.DateFormat;
import java.util.Properties;

@SpringCloudApplication
@EnableFeignClients
@MapperScan("com.neusoft.smart.pos.persistent.mapper")
@ComponentScan("com.neusoft.smart.pos")
@EnableRedisHttpSession
@Order(0)
public class ServiceUserApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceUserApplication.class, args);
    }

    @Bean
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean("captchaProducer")
    Producer captchaProducer() {
        DefaultKaptcha captchaProducer = new DefaultKaptcha();
        Properties properties = new Properties();
        properties.setProperty("kaptcha.textproducer.char.string", "0123456789");
        Config config = new Config(properties);
        captchaProducer.setConfig(config);
        return captchaProducer;
    }

    @Bean
    public MappingJackson2HttpMessageConverter MappingJsonpHttpMessageConverter(Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder) {

        ObjectMapper mapper = jackson2ObjectMapperBuilder.build();

        DateFormat dateFormat = mapper.getDateFormat();
        mapper.setDateFormat(new MyDateFormat(dateFormat));

        return new MappingJackson2HttpMessageConverter(mapper);
    }
}
