package com.vctgo.common.core.context;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.vctgo.common.core.constant.SecurityConstants;
import com.vctgo.common.core.text.Convert;
import com.vctgo.common.core.utils.StrUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 获取当前线程变量中的 租户id 部门id 用户id、用户名称、Token等信息
 * 注意： 必须在网关通过请求头的方法传入，同时在HeaderInterceptor拦截器设置值。 否则这里无法获取
 *
 * @author vctgo
 */
public class SecurityContextHolder {
    private static final TransmittableThreadLocal<Map<String, Object>> THREAD_LOCAL = new TransmittableThreadLocal<>();

    private static final TransmittableThreadLocal<List<String>> THREAD_LOCAL_IGNORE_TABLES = new TransmittableThreadLocal<>();

    public static void removeTemporaryIgnoreTables() {
        THREAD_LOCAL_IGNORE_TABLES.remove();
    }

    public static void setTemporaryIgnoreTables(List<String> ignoreTables) {
        List<String> ignoreTableList = THREAD_LOCAL_IGNORE_TABLES.get();
        if (Objects.nonNull(ignoreTableList)) {
            List<String> ignoreTableTmpList = new ArrayList<>();
            ignoreTableTmpList.addAll(ignoreTables);
            ignoreTableTmpList.addAll(ignoreTableList);
            THREAD_LOCAL_IGNORE_TABLES.set(ignoreTableTmpList);
        } else {
            THREAD_LOCAL_IGNORE_TABLES.set(ignoreTables);
        }
    }

    public static void removeTemporaryIgnoreTables(List<String> ignoreTables) {
        List<String> ignoreTableList = THREAD_LOCAL_IGNORE_TABLES.get();
        if (Objects.nonNull(ignoreTableList)) {
            List<String> ignoreTableTmpList = new ArrayList<>(ignoreTableList);
            ignoreTables.forEach(ignoreTableTmpList::remove);
            THREAD_LOCAL_IGNORE_TABLES.set(ignoreTableTmpList);
        }
    }

    public static List<String> getTemporaryIgnoreTables() {
        return THREAD_LOCAL_IGNORE_TABLES.get();
    }

    public static void set(String key, Object value) {
        Map<String, Object> map = getLocalMap();
        map.put(key, value == null ? StrUtils.EMPTY : value);
    }

    public static String get(String key) {
        Map<String, Object> map = getLocalMap();
        return Convert.toStr(map.getOrDefault(key, StrUtils.EMPTY));
    }


    public static <T> T get(String key, Class<T> clazz) {
        Map<String, Object> map = getLocalMap();
        return StrUtils.cast(map.getOrDefault(key, null));
    }

    public static Map<String, Object> getLocalMap() {
        Map<String, Object> map = THREAD_LOCAL.get();
        if (map == null) {
            map = new ConcurrentHashMap<String, Object>();
            THREAD_LOCAL.set(map);
        }
        return map;
    }

    public static void setLocalMap(Map<String, Object> threadLocalMap) {
        THREAD_LOCAL.set(threadLocalMap);
    }


    public static Long getTenantId() {
        return Convert.toLong(get(SecurityConstants.DETAILS_TENANT_ID), 9999L);
    }

    public static void setTenantId(String tenantId) {
        set(SecurityConstants.DETAILS_TENANT_ID, tenantId);
    }

    public static Integer getTenantType() {
        return Convert.toInt(get(SecurityConstants.DETAILS_TENANT_TYPE), 1);
    }

    public static void setTenantType(String tenantType) {
        set(SecurityConstants.DETAILS_TENANT_TYPE, tenantType);
    }

    public static void setTenantName(String tenantName) {
        set(SecurityConstants.DETAILS_TENANT_NAME, tenantName);
    }

    public static String getTenantName() {
        return get(SecurityConstants.DETAILS_TENANT_NAME);
    }

    public static Long getDeptId() {
        return Convert.toLong(get(SecurityConstants.DETAILS_DEPT_ID));
    }

    public static void setDeptId(String deptId) {
        set(SecurityConstants.DETAILS_DEPT_ID, deptId);
    }

    public static void setRemoteHeader(Object account) {
        set(SecurityConstants.REMOTE_HEADER, account);
    }

    public static Long getUserId() {
        return Convert.toLong(get(SecurityConstants.DETAILS_USER_ID), 0L);
    }

    public static void setUserId(String account) {
        set(SecurityConstants.DETAILS_USER_ID, account);
    }

    public static String getUserName() {
        return get(SecurityConstants.DETAILS_USERNAME);
    }

    public static void setUserName(String username) {
        set(SecurityConstants.DETAILS_USERNAME, username);
    }

    public static String getUserAccount() {
        return get(SecurityConstants.DETAILS_USER_ACCOUNT);
    }

    public static void setUserAccount(String userAccount) {
        set(SecurityConstants.DETAILS_USER_ACCOUNT, userAccount);
    }

    public static String getNickName() {
        return get(SecurityConstants.DETAILS_NICKNAME);
    }

    public static void setNickName(String nickName) {
        set(SecurityConstants.DETAILS_NICKNAME, nickName);
    }

    public static String getUserKey() {
        return get(SecurityConstants.USER_KEY);
    }

    public static void setUserKey(String userKey) {
        set(SecurityConstants.USER_KEY, userKey);
    }

    public static String getPermission() {
        return get(SecurityConstants.ROLE_PERMISSION);
    }

    public static void setPermission(String permissions) {
        set(SecurityConstants.ROLE_PERMISSION, permissions);
    }

    public static void remove() {
        THREAD_LOCAL.remove();
    }
}
