package com.bocloud.cmp.model.jumpserver;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RuleConfiguration {
    private UserConfs userConfs; // 用户配置。
    private ResConfs resConfs; // 资源配置。
    private AccountConfs accountConfs; // 帐号配置。
    private List<String> protocols; // 指定协议列表。protoType = 1 时生效，可取值范围：ssh telnet rdp xdmcp vnc xfwd

    public UserConfs getUserConfs() {
        return userConfs;
    }

    public UserConfs safeGetUserConfs() {
        if (userConfs == null) {
            userConfs = new UserConfs();
        }
        return userConfs;
    }

    public void setUserConfs(UserConfs userConfs) {
        this.userConfs = userConfs;
    }

    public ResConfs getResConfs() {
        return resConfs;
    }

    public ResConfs safeGetResConfs() {
        if (resConfs == null) {
            resConfs = new ResConfs();
        }
        return resConfs;
    }

    public void setResConfs(ResConfs resConfs) {
        this.resConfs = resConfs;
    }

    public AccountConfs getAccountConfs() {
        return accountConfs;
    }

    public AccountConfs safeGetAccountConfs() {
        if (accountConfs == null) {
            accountConfs = new AccountConfs();
        }
        return accountConfs;
    }

    public void setAccountConfs(AccountConfs accountConfs) {
        this.accountConfs = accountConfs;
    }

    public List<String> getProtocols() {
        return protocols;
    }

    public List<String> safeGetProtocols() {
        if (protocols == null) {
            protocols = new ArrayList<>();
        }
        return protocols;
    }

    public void setProtocols(List<String> protocols) {
        this.protocols = protocols;
    }

    public static class UserConfs {
        private List<Long> users; // 指定用户ID列表，userType = 1 时生效。
        private List<Long> userGroups; // 指定用户组ID列表，userType = 1 时生效。
        private List<Rule> rules; // 指定规则列表， userType = 2 时生效。

        public List<Long> getUsers() {
            return users;
        }

        public void setUsers(List<Long> users) {
            this.users = users;
        }

        public List<Long> getUserGroups() {
            return userGroups;
        }

        public void setUserGroups(List<Long> userGroups) {
            this.userGroups = userGroups;
        }

        public List<Rule> getRules() {
            return rules;
        }

        public void setRules(List<Rule> rules) {
            this.rules = rules;
        }
    }

    public static class ResConfs {
        private List<Long> devs; // 指定资产ID列表，resType = 1 时生效。
        private List<Long> devGroups; // 指定资产组ID列表，resType = 1 时生效。
        private List<Rule> rules; // 指定规则列表， resType = 2 时生效。

        public List<Long> getDevs() {
            return devs;
        }

        public void setDevs(List<Long> devs) {
            this.devs = devs;
        }

        public List<Long> getDevGroups() {
            return devGroups;
        }

        public void setDevGroups(List<Long> devGroups) {
            this.devGroups = devGroups;
        }

        public List<Rule> getRules() {
            return rules;
        }

        public void setRules(List<Rule> rules) {
            this.rules = rules;
        }
    }

    public static class AccountConfs {
        private List<String> accounts; // 指定帐号列表。accountType = 1 时生效。
        private List<Rule> rules; // 指定规则列表， resType = 2 时生效。

        public List<String> getAccounts() {
            return accounts;
        }

        public void setAccounts(List<String> accounts) {
            this.accounts = accounts;
        }

        public List<Rule> getRules() {
            return rules;
        }

        public void setRules(List<Rule> rules) {
            this.rules = rules;
        }
    }

    public static class Rule {
        //==========================被UserConfs使用时==========================
        // 规则类型，userType = 2 时生效。
        // 可取值范围：
        //      LOGINNAME-用户帐号、
        //      USERNAME-用户名、
        //      ROLE-用户角色、
        //      EXTINFO-附加信息、
        //      AUTHTYPE-认证方式、
        //      EMAIL-工作邮箱、
        //      REMARK-备注、
        //      USERGROUP-用户组
        //==========================被ResConfs使用时==========================
        // 规则类型，resType = 2 时生效。
        // 可取值范围：
        //      NAME-资源名、
        //      IP-资源ip、
        //      DESCRIPTION-简要说明、
        //      SYSTYPE-资源的系统类型、
        //      EXTINFO-附加信息、
        //      OWNER-责任人、
        //      RESGROUP-资产分组
        //==========================被AccountConfs使用时==========================
        // 规则类型，accountType = 2 时生效。
        // 可取值范围：
        //      NAME-帐号名、
        //      TYPE-帐号类型（SUPER-特权、NORMAL-非特权）
        private String type;
        // 规则匹配值，userType = 2 时生效。
        private String value;
        // 匹配操作符，userType = 2 时生效。
        // 取值范围：= != > < >= <= contains/startsWith/endsWith/between(字符串包含) ~（正则匹配），
        // 前缀!contains、!startsWith、!endsWith、!~、!between 表示取反，匹配失败时废弃处理。
        // 另外，如果value为ID数组，operator=in，此时页面不显示operator，默认取值为in。
        private String operator;
        // 忽略大小写，userType = 2 时生效。true：忽略大小写， false：否。
        private Boolean ignoreCase;

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public String getOperator() {
            return operator;
        }

        public void setOperator(String operator) {
            this.operator = operator;
        }

        public Boolean getIgnoreCase() {
            return ignoreCase;
        }

        public void setIgnoreCase(Boolean ignoreCase) {
            this.ignoreCase = ignoreCase;
        }
    }

    public static Map<String, String> confOperates = new HashMap<>();

    static {
        confOperates.put("contains", "包含");
        confOperates.put("!contains", "不包含");
        confOperates.put("~", "正则匹配");
        confOperates.put("!~", "正则不匹配");
        confOperates.put("between", "在日期范围内");
        confOperates.put("!between", "不在日期范围内");
        confOperates.put("startsWith", "前缀为");
        confOperates.put("!startsWith", "非前缀为");
        confOperates.put("endsWith", "后缀为");
        confOperates.put("!endsWith", "非后缀为");
    }

    public static Map<String, String> userConfTypes = new HashMap<>();

    static {
        userConfTypes.put("LOGINNAME", "用户账号");
        userConfTypes.put("USERNAME", "用户名");
        userConfTypes.put("EMAIL", "工作邮箱");
        userConfTypes.put("REMARK", "备注");
        userConfTypes.put("ROLE", "角色");
        userConfTypes.put("AUTHTYPE", "认证方式");
        userConfTypes.put("USERGROUP", "用户组");
    }

    public static Map<String, String> resConfTypes = new HashMap<>();

    static {
        resConfTypes.put("NAME", "资产名");
        resConfTypes.put("IP", "IP");
        resConfTypes.put("DESCRIPTION", "简要说明");
        resConfTypes.put("OWNER", "责任人");
        resConfTypes.put("RESGROUP", "资产组");
        resConfTypes.put("SYSTYPE", "资产类型");
    }

    public static Map<String, String> accConfTypes = new HashMap<>();

    static {
        accConfTypes.put("NAME", "指定账号");
        accConfTypes.put("TYPE", "帐号类型");
    }

    public static Map<String, String> accConfKinds = new HashMap<>();

    static {
        accConfTypes.put("NORMAL", "普通帐号");
        accConfTypes.put("SUPER", "特权账号");
    }
}
