package thyyxxk.webserver.service.redislike;

import org.springframework.stereotype.Service;
import thyyxxk.webserver.dao.his.redislike.RedisLikeDao;
import thyyxxk.webserver.entity.dictionary.CodeName;
import thyyxxk.webserver.entity.login.UserInfo;
import thyyxxk.webserver.utils.StringUtil;
import thyyxxk.webserver.utils.TokenUtil;

import java.util.HashMap;
import java.util.Map;

@Service
public class RedisLikeService {

    public final static String SCROLLING_MESSAGES = "scrollingMessages";
    public final static String SYSTEM_UPDATES_MESSAGE = "systemUpdatesMessage";

    private final static Map<String, String> EMPLOYEE_NAME_MAP = new HashMap<>();
    private final static Map<String, String> DEPT_NAME_MAP = new HashMap<>();
    private final static Map<String, String> DEPT_AND_EMPLOYEE_NAME = new HashMap<>();
    private final static Map<String, String> REGION_MAP = new HashMap<>();
    private final static Map<String, String> WJW_ANAESTHESIA_MAP = new HashMap<>();
    private final static Map<String, String> WJW_DEPARTMENT_MAP = new HashMap<>();
    private final static Map<String, String> WJW_NATION_MAP = new HashMap<>();
    private final static Map<String, String> COUNTRY_MAP = new HashMap<>();
    private final static Map<String, UserInfo> USER_MAP = new HashMap<>();
    private final RedisLikeDao dao;

    private final static Map<String, String> MESSAGE = new HashMap<>(2);

    public void setMessage(String key, String value) {
        MESSAGE.put(key, value);
    }

    public Map<String, String> getMessage() {
        return MESSAGE;
    }

    public RedisLikeService(RedisLikeDao dao) {
        this.dao = dao;
    }

    public String getEmployeeName(String code) {
        if (StringUtil.invalidValue(code)) {
            return null;
        }
        String username = EMPLOYEE_NAME_MAP.get(code);
        if (null == username) {
            username = dao.selectEmployeeName(code);
            EMPLOYEE_NAME_MAP.put(code, username);
        }
        return username;
    }

    public String getDeptAndEmployeeName(String code) {
        if (StringUtil.invalidValue(code)) {
            return null;
        }
        String result = DEPT_AND_EMPLOYEE_NAME.get(code);
        if (null == result) {
            result = dao.selectDeptAndUser(code);
            DEPT_AND_EMPLOYEE_NAME.put(code, result);
        }
        return result;
    }

    public String getDeptName(String code) {
        if (StringUtil.invalidValue(code)) {
            return null;
        }
        String deptName = DEPT_NAME_MAP.get(code);
        if (null == deptName) {
            deptName = dao.selectDeptName(code);
            DEPT_NAME_MAP.put(code, deptName);
        }
        return deptName;
    }

    public String getRegionName(String code) {
        if (StringUtil.isBlank(code)) {
            return null;
        }
        String regionName = REGION_MAP.get(code);
        if (null == regionName) {
            CodeName region = dao.selectAdmdvsNameAndParentName(code);
            if (null == region) {
                region = dao.selectAdmdvsName(code);
                if (null == region) {
                    region = dao.selectAdmdvsNameAndParentName(code.substring(0, 4) + "00");
                    if (null == region) {
                        region = dao.selectAdmdvsName(code.substring(0, 4) + "00");
                    }
                }
            }
            if (null == region) {
                regionName = "未知";
            } else {
                regionName = region.getCode() + region.getName();
            }
            REGION_MAP.put(code, regionName);
        }
        return regionName;
    }

    public String getWjwAnaesthesia(String code) {
        if (StringUtil.invalidValue(code)) {
            return null;
        }
        String wjwCode = WJW_ANAESTHESIA_MAP.get(code);
        if (null == wjwCode) {
            wjwCode = dao.selectWjwAnaesthesia(code);
            WJW_ANAESTHESIA_MAP.put(code, wjwCode);
        }
        return wjwCode;
    }

    public String getWjwDepartment(String code) {
        if (StringUtil.invalidValue(code)) {
            return null;
        }
        String wjwCode = WJW_DEPARTMENT_MAP.get(code);
        if (null == wjwCode) {
            wjwCode = dao.selectWjwDepartment(code);
            WJW_DEPARTMENT_MAP.put(code, wjwCode);
        }
        return wjwCode;
    }

    public String getWjwNation(String code) {
        if (StringUtil.invalidValue(code)) {
            return null;
        }
        String wjwCode = WJW_NATION_MAP.get(code);
        if (null == wjwCode) {
            wjwCode = dao.selectWjwNation(code);
            WJW_NATION_MAP.put(code, wjwCode);
        }
        return wjwCode;
    }

    public String getCountryName(String code) {
        if (StringUtil.invalidValue(code)) {
            return null;
        }
        String name = COUNTRY_MAP.get(code);
        if (null == name) {
            name = dao.selectCountryName(code);
            COUNTRY_MAP.put(code, name);
        }
        return name;
    }

    public UserInfo getUserInfoByCode(String code) {
        if (StringUtil.invalidValue(code)) {
            return null;
        }
        UserInfo userInfo = USER_MAP.get(code);
        if (null == userInfo) {
            userInfo = dao.selectUserInfo(code);
            userInfo.setDeptName(getDeptName(userInfo.getDeptCode()));
            userInfo.setRoles(dao.selectUserRoles(code));
            USER_MAP.put(code, userInfo);
        }
        return userInfo;
    }

    public UserInfo getUserInfoByToken() {
        return getUserInfoByCode(TokenUtil.getTokenUserId());
    }

    public void handleUserRoleChanged(String code) {
        UserInfo userInfo = getUserInfoByCode(code);
        userInfo.setRoles(dao.selectUserRoles(code));
        USER_MAP.replace(code, userInfo);
    }

    public void handleUserPwdChanged(String code, String pwd) {
        UserInfo userInfo = getUserInfoByCode(code);
        userInfo.setPassword(pwd);
        USER_MAP.replace(code, userInfo);
    }

    public void handleUserLogin(String code) {
        USER_MAP.remove(code);
        getUserInfoByCode(code);
    }
}
