package com.chenyang.nse.bussiness.enumeration;

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

public enum EdbType {
   DB2("DB2", "DB2", "com.ibm.db2.jcc.DB2Driver", "jdbc:db2://{ip}:{port}/{servername}"),
   AS400("AS400", "AS400", "com.ibm.as400.access.AS400JDBCDriver", "jdbc:as400://{ip}"),
   MSSQLSERVER("MSSQLSERVER", "MSSQLSERVER", "com.microsoft.sqlserver.jdbc.SQLServerDriver", "jdbc:sqlserver://{ip}:{port};DatabaseName={servername};sendStringParametersAsUnicode=false"),
   MYSQL("MYSQL", "MYSQL", "com.mysql.cj.jdbc.Driver", "jdbc:mysql://{ip}:{port}/{servername}?zeroDateTimeBehavior=convertToNull"),
   KINGBASE("KINGBASE", "KINGBASE", "com.kingbase8.Driver", "jdbc:kingbase8://{ip}:{port}/{servername}"),
   MARIADB("MARIADB", "MYSQL", "com.mysql.cj.jdbc.Driver", "jdbc:mysql://{ip}:{port}/{servername}?zeroDateTimeBehavior=convertToNull"),
   ORACLE("ORACLE", "ORACLE", "oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST={ip})(PORT={port}))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME={servername})))"),
   ORACLEINS("ORACLEINS", "ORACLE", "oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@{ip}:{port}:{servername}"),
   HIVE("HIVE", "HIVE", "org.apache.hive.jdbc.HiveDriver", "jdbc:hive2://{ip}:{port}/{servername}"),
   SYBASE("SYBASE", "SYBASE", "net.sourceforge.jtds.jdbc.Driver", "jdbc:jtds:sybase://{ip}:{port}/{servername}"),
   SYBASE_IQ("SYBASE_IQ", "SYBASE_IQ", "com.sybase.jdbc4.jdbc.SybDriver", "jdbc:sybase:Tds:{ip}:{port}/{servername}"),
   TERADATA("TERADATA", "TERADATA", "com.teradata.jdbc.TeraDriver", "jdbc:teradata://{ip}/DATABASE={servername},DBS_PORT={port},LOB_SUPPORT=off,tmode=TERA"),
   INFORMIX("INFORMIX", "INFORMIX", "com.informix.jdbc.IfxDriver", "jdbc:informix-sqli://{ip}:{port}/{servername}:INFORMIXSERVER={server};DB_LOCALE=en_us.8859-1;"),
   GREENPLUM("GREENPLUM", "GREENPLUM", "org.postgresql.Driver", "jdbc:postgresql://{ip}:{port}/{servername}"),
   POSTGRESQL("POSTGRESQL", "POSTGRESQL", "org.postgresql.Driver", "jdbc:postgresql://{ip}:{port}/{servername}"),
   HANA("HANA", "HANA", "com.sap.db.jdbc.Driver", "jdbc:sap://{ip}:{port}?reconnect=true"),
   RAPIDSDB("RAPIDSDB", "RAPIDSDB", "com.mysql.cj.jdbc.Driver", "jdbc:mysql://{ip}:{port}"),
   MONGODB("MONGODB", "MONGODB", "com.mongodb.jdbc.MongoDriver", "mongodb://{ip}:{port}/admin"),
   KAFKA("KAFKA", "KAFKA", "", "bootstrap.servers://{ip}:{port}?characterEncoding=utf-8"),
   GBASE8S("GBASE8S", "GBASE8S", "com.gbasedbt.jdbc.Driver", "jdbc:gbasedbt-sqli://{ip}:{port}/{servername}:GBASEDBTSERVER={server}"),
   DM("DM", "DM", "dm.jdbc.driver.DmDriver", "jdbc:dm://{ip}:{port}"),
   HIVE_TDH("HIVE_TDH", "HIVE_TDH", "org.apache.hive.jdbc.HiveDriver", "jdbc:hive2://{ip}:{port}/{servername};transportMode=http;httpPath=cliservice"),
   GAUSS("GAUSS", "GAUSS", "org.opengauss.Driver", "jdbc:opengauss://{ip}:{port}/{servername}"),
   GAUSSDB("GAUSSDB", "GAUSSDB", "com.huawei.opengauss.jdbc.Driver", "jdbc:opengauss://{ipPorts}/{servername}?loadBalanceHosts=true&reWriteBatchedInserts=true"),
   OCEANBASE("OCEANBASE", "MYSQL", "com.mysql.cj.jdbc.Driver", "jdbc:mysql://{ip}:{port}/{servername}?zeroDateTimeBehavior=convertToNull");

   private String dbcode;
   private String dbname;
   private String dbdriver;
   private String urltemplate;

   private EdbType(String dbcode, String dbname, String dbdriver, String urltemplate) {
      this.dbcode = dbcode;
      this.dbname = dbname;
      this.dbdriver = dbdriver;
      this.urltemplate = urltemplate;
   }

   public String getDbcode() {
      return this.dbcode;
   }

   public void setDbcode(String dbcode) {
      this.dbcode = dbcode;
   }

   public String getDbname() {
      return this.dbname;
   }

   public void setDbname(String dbname) {
      this.dbname = dbname;
   }

   public String getDbdriver() {
      return this.dbdriver;
   }

   public void setDbdriver(String dbdriver) {
      this.dbdriver = dbdriver;
   }

   public String getUrltemplate() {
      return this.urltemplate;
   }

   public void setUrltemplate(String urltemplate) {
      this.urltemplate = urltemplate;
   }

   public static EdbType getEdbTypeByDbcode(String dbcode) {
      EdbType[] db = values();

      for(EdbType item : db) {
         String f = item.getDbcode();
         if (f.equals(dbcode)) {
            return item;
         }
      }

      return null;
   }

   public static String getUrlByEdbType(EdbType db, String ip, String port, String servername) {
      String url = db.getUrltemplate().replace("{ip}", ip).replace("{port}", port).replace("{servername}", servername).replace(" ", "");
      return url;
   }

   public static String getUrlByEdbcode(String dbcode, String ip, String dbservername) {
      EdbType db = getEdbTypeByDbcode(dbcode);
      String url = db.getUrltemplate().replace("{ip}", ip).replace(" ", "");
      return url;
   }

   public static String getUrlByEdbcode(String dbcode, String ip, String port, String dbservername) {
      EdbType db = getEdbTypeByDbcode(dbcode);
      String url = db.getUrltemplate().replace("{ip}", ip).replace("{port}", port).replace("{servername}", dbservername).replace(" ", "");
      if (dbcode.equals("MYSQL") || dbcode.equals("MARIADB")) {
         url = url + "&useSSL=false&rewriteBatchedStatements=true&serverTimezone=UTC";
      }

      return url;
   }

   public static String getUrlByEdbcode(String dbcode, List<Map<String, String>> ips, String schema, String dbservername) {
      StringBuilder ipPorts = new StringBuilder();
      EdbType db = getEdbTypeByDbname(dbcode);

      for(Map<String, String> map : ips) {
         String ip = (String)map.get("ip");
         String port = (String)map.get("port");
         ipPorts.append(ip).append(":").append(port).append(",");
      }

      String url = db.getUrltemplate().replace("{ipPorts}", ipPorts.toString().substring(0, ipPorts.length() - 1)).replace("{servername}", dbservername);
      return url;
   }

   public static EdbType getEdbTypeByDbname(String dbname) {
      EdbType[] db = values();

      for(EdbType item : db) {
         String f = item.getDbname();
         if (f.equals(dbname)) {
            return item;
         }
      }

      return null;
   }

   public static String getUrlByEdbcode(String dbcode, String ip, String port, String dbservername, String servername) {
      EdbType db = getEdbTypeByDbcode(dbcode);
      String url = db.getUrltemplate().replace("{ip}", ip).replace("{port}", port).replace("{servername}", dbservername).replace("{server}", servername).replace(" ", "");
      return url;
   }

   public static String getUrlByEdbcode(String dbcode, String ip, String port, String dbservername, String servername, String pageCode) {
      EdbType db = getEdbTypeByDbcode(dbcode);
      String url = db.getUrltemplate().replace("{ip}", ip).replace("{port}", port).replace("{servername}", dbservername).replace("{server}", servername == null ? "" : servername).replace(" ", "");
      if (dbcode.equals("TERADATA")) {
         url = url + ",charset=UTF8";
      } else if (dbcode.equals("MYSQL") || dbcode.equals("MARIADB")) {
         url = url + "&useUnicode=true&characterEncoding=" + pageCode;
      }

      return url;
   }

   public static List<String> getDbTypeList() {
      List<String> list = new ArrayList();
      EdbType[] db = values();

      for(EdbType item : db) {
         list.add(item.getDbcode());
      }

      return list;
   }
}
