package com.tiesheng.login.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.tiesheng.login.config.TieshengLoginConfigurer;
import com.tiesheng.util.exception.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.function.Consumer;

/**
 * @author hao
 */
@Service
public class LoginService {

    @Value("${tiesheng.global.host}")
    String globalHost;
    @Autowired(required = false)
    TieshengLoginConfigurer tieshengLoginConfigurer;


    /**
     * 检查配置
     */
    public void checkLoginConfigurer(Consumer<TieshengLoginConfigurer> consumer) {
        if (tieshengLoginConfigurer == null) {
            throw new ApiException("未配置登录逻辑");
        }
        consumer.accept(tieshengLoginConfigurer);
    }

    public String getContextPath() {
        String context = SpringUtil.getProperty("server.servlet.context-path");
        if (StrUtil.isEmpty(context)) {
            context = "";
        }

        return context;
    }

    public String buildPath(String path) {
        if (StrUtil.isEmpty(path)) {
            path = "";
        }

        if (!StrUtil.isEmpty(this.globalHost) && !StrUtil.startWith(path, "http")) {
            path = this.globalHost + this.getContextPath() + path;
        }

        return path;
    }


}
