package cn.stylefeng.team.generator.api.enums;

import cn.hutool.core.util.StrUtil;
import lombok.Getter;

@Getter
public enum DatabaseTypeEnum {

    MYSQL("mysql",
            "com.mysql.cj.jdbc.Driver",
            "jdbc:mysql://{}:{}/{}?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true",
            "        <!--Mysql数据库驱动-->\n" +
                    "        <dependency>\n" +
                    "            <groupId>mysql</groupId>\n" +
                    "            <artifactId>mysql-connector-java</artifactId>\n" +
                    "            <version>${mysql-connector-java.version}</version>\n" +
                    "        </dependency>"),

    PG_SQL("pgsql",
            "org.postgresql.Driver",
            "jdbc:postgresql://{}:{}/{}",
            "        <!--PostgreSQL数据库驱动-->\n" +
                    "        <dependency>\n" +
                    "            <groupId>org.postgresql</groupId>\n" +
                    "            <artifactId>postgresql</artifactId>\n" +
                    "            <version>42.2.5</version>\n" +
                    "        </dependency>"),

    ORACLE("oracle",
            "oracle.jdbc.OracleDriver",
            "jdbc:oracle:thin:@//{}:{}/{}",
            "        <!--Oracle数据库驱动-->\n" +
                    "        <dependency>\n" +
                    "            <groupId>com.oracle</groupId>\n" +
                    "            <artifactId>ojdbc6</artifactId>\n" +
                    "            <version>11.2.0.3</version>\n" +
                    "        </dependency>"),

    DM("dm",
            "dm.jdbc.driver.DmDriver",
            "jdbc:dm://{}:{}/{}?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai&useSSL=true&characterEncoding=UTF-8",
            "        <!--达梦数据库驱动-->\n" +
                    "        <dependency>\n" +
                    "            <groupId>com.dameng</groupId>\n" +
                    "            <artifactId>Dm8JdbcDriver18</artifactId>\n" +
                    "            <version>8.1.1.49</version>\n" +
                    "        </dependency>"),

    MS_SQL("sqlserver",
            "net.sourceforge.jtds.jdbc.Driver",
            "jdbc:jtds:sqlserver://{}:{};DatabaseName={}",
            "        <!--sql server数据库驱动-->\n" +
                    "        <dependency>\n" +
                    "            <groupId>net.sourceforge.jtds</groupId>\n" +
                    "            <artifactId>jtds</artifactId>\n" +
                    "            <version>1.3.1</version>\n" +
                    "        </dependency>"),

    OPEN_GAUSS("openGauss",
            "org.opengauss.Driver",
            "jdbc:opengauss://{}:{}/{}",
            "        <!--openGauss数据库驱动-->\n" +
                    "        <dependency>\n" +
                    "            <groupId>org.opengauss</groupId>\n" +
                    "            <artifactId>opengauss-jdbc</artifactId>\n" +
                    "            <version>5.0.0-og</version>\n" +
                    "        </dependency>");

    private final String dbType;

    private final String className;

    private final String url;

    private final String dbDriverPom;

    DatabaseTypeEnum(String dbType, String className, String url, String dbDriverPom) {
        this.dbType = dbType;
        this.className = className;
        this.url = url;
        this.dbDriverPom = dbDriverPom;
    }

    public static String getDatabaseUrl(String dbType, String host, String port, String dbName) {
        for (DatabaseTypeEnum value : DatabaseTypeEnum.values()) {
            if (value.dbType.equals(dbType)) {
                return StrUtil.format(value.url, host, port, dbName);
            }
        }
        return StrUtil.format(MYSQL.url, host, port, dbName);
    }

    public static String getDbDriverClass(String dbType) {
        for (DatabaseTypeEnum value : DatabaseTypeEnum.values()) {
            if (value.dbType.equals(dbType)) {
                return value.className;
            }
        }
        return MYSQL.className;
    }

    public static String getDbDriverPom(String dbType) {
        for (DatabaseTypeEnum value : DatabaseTypeEnum.values()) {
            if (value.dbType.equals(dbType)) {
                return value.dbDriverPom;
            }
        }
        return MYSQL.dbDriverPom;
    }

}
