package com.bofeng.support;


import com.bofeng.Consts;
import com.bofeng.JwtHelper;
import com.bofeng.entity.User;
import com.bofeng.service.UserService;
import com.google.common.collect.Sets;
import com.yvan.freemarker.FreemarkerFunction;
import com.yvan.platform.Conv;
import com.yvan.springmvc.HttpUtils;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * freemarker 的静态资源函数
 * Created by luoyifan on 2017/3/28.
 */
@Component
public class StaticFunctions {

    @Value("${yvan.static_domain}")
    private String staticDomain;

    @Value("${yvan.static_version}")
    private String staticVersion;

    @Autowired
    private BofengProperties bofengProperties;

    @Autowired
    private UserService userService;

    /**
     * 第三方前端库函数资源
     * 根据 jzt.static_domain 的配置，生成静态资源路径
     * <p/>
     * 比如 /css/base.css 会根据配置变成
     * http://static.yyjzt.com/js/jquery.js 或
     * /static/js/jquery.js
     */
    @FreemarkerFunction("s3")
    public String s3(String resourcePath) {
        return staticDomain + resourcePath;
    }

    /**
     * 自己编写的前端库资源
     * 根据 jzt.static_domain 的配置，生成静态资源路径
     * <p/>
     * 比如 /css/base.css 会根据配置变成
     * http://static.yyjzt.com/css/base.css?v=20170328 或
     * /static/css/base.css?v=20170328
     */
    @FreemarkerFunction("s0")
    public String s0(String resourcePath) {
        return staticDomain + resourcePath + "?v=" + staticVersion;
    }

    @FreemarkerFunction("currentUser")
    public User currentUser() {
        return userService.getUserById(Conv.NL(JwtHelper.getUserId()));
    }

    @FreemarkerFunction("currentResources")
    public MenuResource[] currentResources() {
        if (Consts.USER_TYPE_ADMIN.equals(JwtHelper.getUserType())) {
            return bofengProperties.getAdminMenu();
        }
        if (Consts.USER_TYPE_CASHIER.equals(JwtHelper.getUserType())) {
            return bofengProperties.getCashierMenu();
        }
        if (Consts.USER_TYPE_WORKER.equals(JwtHelper.getUserType())) {
            return bofengProperties.getWorkerMenu();
        }
        return null;
    }

    @FreemarkerFunction("getActiveNavMenu")
    public Set<String> getActiveNavMenu() {

        val request = HttpUtils.currentRequest();
        val root = currentResources();
        Set<String> r = Sets.newLinkedHashSet();

        String uri = request.getRequestURI();
        for (MenuResource node1 : root) {

            for (MenuResource node2 : node1.getChilds()) {

                for (MenuResource node3 : node2.getChilds()) {

                    if (HttpUtils.isEqualPath(node3.getUrl(), uri)) {
                        r.add(node1.getMenuId());
                        r.add(node2.getMenuId());
                        r.add(node3.getMenuId());
                    }
                }//node3

                if (HttpUtils.isEqualPath(node2.getUrl(), uri)) {
                    r.add(node1.getMenuId());
                    r.add(node2.getMenuId());
                }
            }//node2

            if (HttpUtils.isEqualPath(node1.getUrl(), uri)) {
                r.add(node1.getMenuId());
            }
        }//node1

        return r;
    }
}
