package weaver.interfaces.htsc.wpscenter.utils;

import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.share.ShareManager;
import weaver.general.GCONST;
import java.util.HashMap;
import java.util.Map;

public class Config {

    /**
     * 持有自己的对象
     */
    private static Config config;

    private BaseBean toolBean = new BaseBean();

    private static String PROP_FILE_NAME = "wpscenter_for_weaver";

    /**
     * 不做区分，只判断是否打开
     */
    public static final String OPEN_ALL = "all";

    /**
     * 开关，查看文档
     */
    public static final String OPEN_PREVIEW = "view";

    /**
     * 开关，编辑文档
     */
    public static final String OPEN_EDIT = "edit";

    /**
     * 开关，格式处理
     */
    public static final String OPEN_FORMAT = "format";

    /**
     * 地址分隔符号
     */
    public static String URL_SEPARATOR = "/";

    /**
     * 接口所在的域
     */
    public static final String OPEN_API = "/open";

    /**
     * 编辑服务调用接口，用于获取编辑地址
     */
    public static String WEBOFFICE_V1_URL = OPEN_API + "/weboffice/v1/url";

    /**
     * 预览服务调用接口，用于获取预览地址
     */
    public static String PREVIEW_V1_URL = OPEN_API + "/preview/v1/url";

    /**
     * TOKEN获取接口
     */
    public static String AUTH_V1_APP_INSCOPE_TOKEN = OPEN_API + "/auth/v1/app/inscope/token";

    /**
     * 文档类型转换接口
     */
    public static String CPS_V1_OFFICE_CONVERT = OPEN_API + "/cps/v1/office/convert";

    /**
     * 任务结果查询
     */
    public static String CPS_V1_TASK_QUERY = OPEN_API + "/cps/v1/task/query";

    /**
     * 文件下载接口
     */
    public static String CPS_V1_DOWNLOAD_FILE = OPEN_API + "/cps/v1/download/file";

    /**
     * 内容操作接口
     */
    public static String CPS_V1_OFFICE_OPERATE = OPEN_API + "/cps/v1/office/operate";

    /**
     * 图片操作接口
     */
    public static String CPS_V1_IMAGE_OPERATE = OPEN_API + "/cps/v1/image/operate";

    /**
     * 模板套用接口
     */
    public static String CPS_V1_OFFICE_WRAPHEADER = OPEN_API + "/cps/v1/office/wrapheader";

    /**
     * 文档合并
     */
    public static String CPS_V1_OFFICE_MERGE = OPEN_API + "/cps/v1/office/merge";

    /**
     * 公文智能排版
     */
    public static String CPS_V1_OFFICE_SMARTOFFICIAL = OPEN_API + "/cps/v1/office/smartofficial";

    /**
     * 文字处理默认等待次数
     */
    public static int TASK_MAX_WAIT_COUNT = 200;

    /**
     * 任务处理默认等待周期
     */
    public static int TASK_WAIT_INTVAL_TIME = 50;

    private Config(){

    }

    private static Config getConfigInstance(){
        if(null == config) {
            config = new Config();
        }
        return config;
    }

    public BaseBean getToolBean() {
        return toolBean;
    }

    public void setToolBean(BaseBean toolBean) {
        this.toolBean = toolBean;
    }

    private Map<String, String> getConfigFromDB(){
        Map<String, String> config = new HashMap<String, String>();

        // TODO 从数据库读取配置信息

        return config;
    }

    private Map<String, String> getConfigFromDB(String propFileName){
        Map<String, String> config = new HashMap<String, String>();
        config = getConfigFromDB();
        return config;
    }

    public static String getOAUrl() {
        String ecologyContentPath = GCONST.getContextPath();
        String downloadurl = getPropertyValue("wpscenter_for_weaver", "ip");
        String port = getPropertyValue("wpscenter_for_weaver", "port");
        if(!port.isEmpty()){
            downloadurl += ":"+port;
        }
        downloadurl = downloadurl + ecologyContentPath;
        return downloadurl;
    }

    public static boolean isWpsDocCenterOpen(){
        boolean isopen = false;

        isopen = "1".equals(getPropertyValue("doccenter_open"));

        return isopen;
    }

    public static boolean isWpsDocCenterOpen(String type){
        boolean isopen = false;
        if(OPEN_ALL.equals(type)) {
            return isWpsDocCenterOpen();
        }

        return isWpsDocCenterOpen() && isProperty("doccenter_open_" + type,1,1);
    }

    public static String getWpsDocCenterDomain(){
        return getPropertyValue("doccenter_domain");
    }

    public static String getWpsDocCenterAppid(){
        return getPropertyValue("doccenter_appid");
    }

    public static String getWpsDocCenterSercet(){
        return getPropertyValue("doccenter_sercet");
    }

    /**
     * 获取Token的接口地址
     * @return
     */
    public static String getAuthv1AppInscopeToken(){
        return getWpsDocCenterDomain() + AUTH_V1_APP_INSCOPE_TOKEN;
    }

    /**
     * 获取编辑地址的接口地址
     * @return
     */
    public static String getWebofficeV1Url(){
        return getWpsDocCenterDomain() + WEBOFFICE_V1_URL;
    }

    /**
     * 获取预览服务接口地址
     * @return
     */
    public static String getPreviewV1Url(){
        return getWpsDocCenterDomain() + PREVIEW_V1_URL;
    }

    /**
     * 获取文件转换类型接口地址
     * @return
     */
    public static String getCpsV1OfficeConvert(){
        return getWpsDocCenterDomain() + CPS_V1_OFFICE_CONVERT;
    }

    /**
     * 获取任务结果查询接口地址
     * @return
     */
    public static String getCpsV1TaskQuery(){
        return getWpsDocCenterDomain() + CPS_V1_TASK_QUERY;
    }

    /**
     * 获取文档下载接口地址
     * @return
     */
    public static String getCpsV1DownloadFile(){
        return getWpsDocCenterDomain() + CPS_V1_DOWNLOAD_FILE;
    }

    /**
     * 获取文件操作接口地址
     * @return
     */
    public static String getCpsV1OfficeOperate(){
        return getWpsDocCenterDomain() + CPS_V1_OFFICE_OPERATE;
    }

    /**
     * 获取图片操作接口地址
     * @return
     */
    public static String getCpsV1ImageOperate(){
        return getWpsDocCenterDomain() + CPS_V1_IMAGE_OPERATE;
    }

    /**
     * 获取模板套用接口地址
     * @return
     */
    public static String getCpsV1OfficeWrapheader(){
        return getWpsDocCenterDomain() + CPS_V1_OFFICE_WRAPHEADER;
    }

    /**
     * 获取文件合并接口地址
     * @return
     */
    public static String getCpsV1OfficeMerge(){
        return getWpsDocCenterDomain() + CPS_V1_OFFICE_MERGE;
    }

    /**
     * 获取智能公文接口地址
     * @return
     */
    public static String getCpsV1OfficeSmartOfficial(){
        return getWpsDocCenterDomain() + CPS_V1_OFFICE_SMARTOFFICIAL;
    }

    /**
     * 判断是否打开日志输出
     * @Deprecated 请使用 isLogInfo
     * @return
     */
    @Deprecated
    public static boolean isLog() {
        return isLogInfo();
    }

    /**
     * 判断是否打开日志输出
     * @return
     */
    public static boolean isLogInfo() {
        String isOpenLog = "";

        // TODO 从数据库中查询到参数值
        // @debug
        // isOpenLog = "1";

        return isProperty("doccenter_writeLog", 1);
    }

    /**
     * 判断配置中是否有某参数
     *
     * @param key
     * @return
     */
    public static Boolean hasProperty(String key) {
        return hasProperty(PROP_FILE_NAME, key);
    }

    /**
     * 判断配置中是否有某参数
     *
     * @param propFileName
     * @param key
     * @return
     */
    public static Boolean hasProperty(String propFileName, String key) {
        boolean hasProperty = getConfigInstance().getConfigFromDB(propFileName).containsKey(key);
        if(!hasProperty) {
            String value = getConfigInstance().getToolBean().getPropValue(propFileName, key);
            hasProperty = !Tools.isEmptyOrNull(value);
        }
        return hasProperty;
    }
    /**
     * 获取指定文件中的配置参数
     *
     * @param key
     * @return
     */
    public static String getPropertyValue(String key) {
        return getPropertyValue(PROP_FILE_NAME, key);
    }

    /**
     * 获取配置中的指定配置参数
     *
     * @param propFileName
     * @param key
     * @return
     */
    public static String getPropertyValue(String propFileName, String key) {
        String value = getConfigInstance().getConfigFromDB(propFileName).get(key);
        if(Tools.isEmptyOrNull(value)) {
            value = getConfigInstance().getToolBean().getPropValue(propFileName, key);
        }
        return Util.null2s(value, "");
    }

    /**
     * 从配置文件中获取参数, 并转为int
     *
     * @param key
     * @param def
     * @return
     */
    public static String getPropString(String key, String def) {
        return Util.null2s(getPropertyValue(key), def);
    }

    /**
     * 从配置文件中获取参数, 并转为int
     *
     * @param propFileName
     * @param key
     * @param def
     * @return
     */
    public static String getPropString(String propFileName, String key, String def) {
        return Util.null2s(getPropertyValue(propFileName, key), def);
    }

    /**
     * 从配置中获取参数, 并转为int
     *
     * @param key
     * @return
     */
    public static int getPropIntValue(String key, int def) {
        return Util.getIntValue(getPropertyValue(key), def);
    }

    /**
     * 从配置文件中获取参数, 并转为int
     *
     * @param propFileName
     * @param key
     * @param def
     * @return
     */
    public static int getPropIntValue(String propFileName, String key, int def) {
        return Util.getIntValue(getPropertyValue(propFileName, key), def);
    }

    /**
     * 检查配置文件中的属性值
     *
     * @param key
     * @param value
     * @return
     */
    public static boolean isProperty(String key, String value) {
        return null == value && null == key || null != value && value.equals(getPropString(key,""));
    }

    /**
     * 检查配置文件中的属性值，未配置视为参数def的值
     *
     * @param key
     * @param value
     * @return
     */
    public static boolean isProperty(String key, String value, String def) {
        return null == value && null == key || null != value && value.equals(getPropString(key, def));
    }

    /**
     * 检查配置文件中的属性值，如果未配置默认为-1
     *
     * @param propKey
     * @param value
     * @return
     */
    public static boolean isProperty(String propKey, int value) {
        return isProperty(propKey, value, -1);
    }

    /**
     * 检查配置文件中的属性值，如果未配置默认为-1
     *
     * @param propKey
     * @param value
     * @return
     */
    public static boolean isProperty(String propFileName, String propKey, int value) {
        return isProperty(propFileName, propKey, value, -1);
    }

    /**
     * 检查配置文件中的属性值，未配置视为参数def的值
     *
     * @param propKey
     * @param value
     * @param def
     * @return
     */
    public static boolean isProperty(String propKey, int value, int def) {
        return value == getPropIntValue(propKey, def);
    }

    /**
     * 检查配置文件中的属性值，未配置视为参数def的值
     *
     * @param propKey
     * @param value
     * @param def
     * @return
     */
    public static boolean isProperty(String propFileName, String propKey, int value, int def) {
        return value == getPropIntValue(propFileName, propKey, def);
    }

    public static int getTaskMaxWaitCount() {
        return Util.getIntValue(getPropertyValue("doccenter_taskmaxwaitcount"), TASK_MAX_WAIT_COUNT);
    }

    public static int getTaskWaitIntvalTime() {
        return Util.getIntValue(getPropertyValue("doccenter_taskwaitintvaltime"), TASK_WAIT_INTVAL_TIME);
    }

    public static String getViewerWaterProp(String propKey) {
        return getPropertyValue("doccenter_water_viewer_" + propKey);
    }

    public static Boolean hasViewerWaterProp(String propKey) {
        // doccenter_water_image_
        return hasProperty("doccenter_water_viewer_" + propKey);
    }

    public static String getImageWaterProp(String propKey) {
        return getPropertyValue("doccenter_water_image_" + propKey);
    }

    public static Boolean hasImageWaterProp(String propKey) {
        // doccenter_water_image_
        return hasProperty("doccenter_water_image_" + propKey);
    }

    public static String getTextWaterProp(String propKey) {
        return getPropertyValue("doccenter_water_text_" + propKey);
    }

    public static Boolean hasTextWaterProp(String propKey) {
        // doccenter_water_text_
        return hasProperty("doccenter_water_text_" + propKey);
    }

    public static String getUrlForMidFile() {
        return getPropertyValue("doccenter_url_for_mid_file");
    }

    public static Boolean mustClearMidFile() {
        return isProperty("doccenter_need_clear_mid_file",1);
    }

    public static Boolean alwaysUseDocCenter(int userid){
        String suffix = Util.null2String(getConfigInstance().getToolBean().getPropValue(PROP_FILE_NAME,"doccenter_alwaysUseLinux"));
        boolean somoneAlwaysUseLinux = !Tools.isEmptyOrNull(suffix) && ("," + suffix + ",").contains("," + userid + ",");
        return somoneAlwaysUseLinux;
    }

    public static Boolean alwaysUseDocCenter(User user){
        return alwaysUseDocCenter(user, OPEN_ALL);
    }

    public static Boolean alwaysUseDocCenter(User user, String type){
        Boolean hasright = false;
        // 如果开关打开了，默认开启
        if(isWpsDocCenterOpen(type)){
            return true;
        }

        if(null == user) return false;

        int userid = user.getUID();
        if(alwaysUseDocCenter(userid)) return true;
        try{
            // 判断所属分部
            String suffixSubcompant = Util.null2String(getConfigInstance().getToolBean().getPropValue(PROP_FILE_NAME,"doccenter_alwaysUseLinuxForSubcompant"));

            // 查询所有的上级分部
            int subCompanyId = user.getUserSubCompany1();
            boolean isSubcompant = !Tools.isEmptyOrNull(suffixSubcompant) && ("," + suffixSubcompant + ",").contains("," + subCompanyId + ",");
            if(isSubcompant) {
                return true;
            }

            ShareManager shareManager = new ShareManager();

            // 查询所有上级分部
            String allSubCompanyIds = shareManager.getUserAllVirSubCompany(userid+"",subCompanyId+"")+","+subCompanyId;

            // 判断与配置文件中是否有交集
            if(Tools.intersection(suffixSubcompant, allSubCompanyIds, ",")){
                return true;
            }

            // 判断所属部门
            String suffixDepartment = Util.null2String(getConfigInstance().getToolBean().getPropValue(PROP_FILE_NAME,"doccenter_alwaysUseLinuxForDepartment"));

            // 查询所有的上级分部
            int departmentId = user.getUserDepartment();
            boolean isDepartment = !Tools.isEmptyOrNull(suffixDepartment) && ("," + suffixDepartment + ",").contains("," + departmentId + ",");
            if(isDepartment) {
                return true;
            }

            // 查询所有上级部门
            String allSupDepartmentIds = shareManager.getUserAllVirDepartment(userid+"",departmentId+"")+","+departmentId;

            // 判断与配置文件中是否有交集
            if(Tools.intersection(suffixDepartment, allSupDepartmentIds, ",")){
                return true;
            }
        }catch (Exception e) {
            // Do nothing
        }

        return false;
    }

    public static Boolean isReplaceOssUrl() {
        return isProperty("doccenter_replaceOssUrl4Inner",1,1);
    }

    public static Boolean isOpenTrackRevisionsForEdit(){
        return isProperty("doccenter_openTrackRevisionsForEdit",1);
    }

    public static String getManageAppKey() {
        return getPropertyValue("doccenter_manage_appKey");
    }

    public static String getManageSercetKey() {
        return getPropertyValue("doccenter_manage_sercetKey");
    }
}
