package com.huatek.busi.config;

import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import com.google.common.collect.Lists;
import com.huatek.busi.constant.ApiConstant;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.sql.beans.LocalUser;
import com.huatek.torch.util.jwt.TokenMgr;
import com.huatek.torch.util.uuid.Uuid;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * swagger2配置bean
 *
 * @author anker cheng
 *
 * @Date 2019年7月2日
 */
@Configuration
@EnableWebMvc
@EnableSwagger2
public class SpringfoxConfig {
    private String uid = "f52b1f9329cb4cd68d2fc660d9c2aac5";
    private String userName = "admin";
    private String uerMobile = "18710556200";
    private String userEmail = "1123@qq.com";
    private String userPhone = "0298888888";
    private String userLeader = "1";
    private String groupId = "4641af6d9255494e80abc16e69f2dc08";
    private String languageType = "zh_CN";
    private long time = 1200000L;

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any()).build()
                .globalOperationParameters(globalOperationParameters()).apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title(ApiConstant.TITLE).description("HTTP对外开放接口").version("1.0.0")
                .termsOfServiceUrl("http://www.huatek.com").license("LICENSE").licenseUrl("http://www.huatek.com")
                .build();
    }

    private List<Parameter> globalOperationParameters() {

        LocalUser sub = new LocalUser(uid, userName, uerMobile, userEmail, userPhone, userLeader, groupId, languageType,
                time);
        String token = TokenMgr.createJWT(Uuid.getUUID(), ConstantUtil.JWT_ISS, TokenMgr.generalSubject(sub), time);

        List<Parameter> parmList = Lists.newArrayList();
        ParameterBuilder pb = new ParameterBuilder();

        parmList.add(pb.name("token").description("token").modelRef(new ModelRef("string")).parameterType("header").defaultValue(token)
                .required(true).build());

        return parmList;
    }
}
