package com.munjaon.client.service;

import com.munjaon.client.cubrid.service.CubridService;
import com.munjaon.client.mariadb.service.MariaDBService;
import com.munjaon.client.model.MunjaonMsg;
import com.munjaon.client.mssql.service.MssqlService;
import com.munjaon.client.mysql.service.MysqlService;
import com.munjaon.client.oracle.service.OracleService;
import com.munjaon.client.postgresql.service.PostgresqlService;
import com.munjaon.client.tibero.service.TiberoService;
import lombok.Getter;

import java.util.EnumSet;
import java.util.List;

@Getter
public enum DatabaseTypeWorker {
    TYPE_OF_CUBRID("CUBRID") {
        @Override
        public boolean checkTableExists() {
            CubridService cubridService = (CubridService) DatabaseService.CUBRID_SERVICE.getService();
            return cubridService.checkTableExists();
        }

        @Override
        public List<MunjaonMsg> selectToDeliver(String msgType) {
            CubridService cubridService = (CubridService) DatabaseService.CUBRID_SERVICE.getService();
            return cubridService.selectToDeliver(msgType);
        }

        @Override
        public int updateToDeliver(String msgId) {
            CubridService cubridService = (CubridService) DatabaseService.CUBRID_SERVICE.getService();
            return cubridService.updateToDeliver(msgId);
        }

        @Override
        public int updateDeliverForList(List<String> msgIdList) {
            CubridService cubridService = (CubridService) DatabaseService.CUBRID_SERVICE.getService();
            return cubridService.updateDeliverForList(msgIdList);
        }

        @Override
        public int updateToReport(MunjaonMsg params) {
            CubridService cubridService = (CubridService) DatabaseService.CUBRID_SERVICE.getService();
            return cubridService.updateToReport(params);
        }

        @Override
        public List<MunjaonMsg> selectToMove() {
            CubridService cubridService = (CubridService) DatabaseService.CUBRID_SERVICE.getService();
            return cubridService.selectToMove();
        }

        @Override
        public List<MunjaonMsg> selectToExpire(String expireTime) {
            CubridService cubridService = (CubridService) DatabaseService.CUBRID_SERVICE.getService();
            return cubridService.selectToExpire(expireTime);
        }

        @Override
        public int insertToLog(String msgId) {
            CubridService cubridService = (CubridService) DatabaseService.CUBRID_SERVICE.getService();
            return cubridService.insertToLog(msgId);
        }

        @Override
        public int deleteFromDeliver(String msgId) {
            CubridService cubridService = (CubridService) DatabaseService.CUBRID_SERVICE.getService();
            return cubridService.deleteFromDeliver(msgId);
        }

        @Override
        public void moveToDeliver() {
            CubridService cubridService = (CubridService) DatabaseService.CUBRID_SERVICE.getService();
            cubridService.moveToDeliver();
        }
    },
    TYPE_OF_MARIADB("MARIADB") {
        @Override
        public boolean checkTableExists() {
            MariaDBService mariaDBService = (MariaDBService) DatabaseService.MARIADB_SERVICE.getService();
            return mariaDBService.checkTableExists();
        }

        @Override
        public List<MunjaonMsg> selectToDeliver(String msgType) {
            MariaDBService mariaDBService = (MariaDBService) DatabaseService.MARIADB_SERVICE.getService();
            return mariaDBService.selectToDeliver(msgType);
        }

        @Override
        public int updateToDeliver(String msgId) {
            MariaDBService mariaDBService = (MariaDBService) DatabaseService.MARIADB_SERVICE.getService();
            return mariaDBService.updateToDeliver(msgId);
        }

        @Override
        public int updateDeliverForList(List<String> msgIdList) {
            MariaDBService mariaDBService = (MariaDBService) DatabaseService.MARIADB_SERVICE.getService();
            return mariaDBService.updateDeliverForList(msgIdList);
        }

        @Override
        public int updateToReport(MunjaonMsg params) {
            MariaDBService mariaDBService = (MariaDBService) DatabaseService.MARIADB_SERVICE.getService();
            return mariaDBService.updateToReport(params);
        }

        @Override
        public List<MunjaonMsg> selectToMove() {
            MariaDBService mariaDBService = (MariaDBService) DatabaseService.MARIADB_SERVICE.getService();
            return mariaDBService.selectToMove();
        }

        @Override
        public List<MunjaonMsg> selectToExpire(String expireTime) {
            MariaDBService mariaDBService = (MariaDBService) DatabaseService.MARIADB_SERVICE.getService();
            return mariaDBService.selectToExpire(expireTime);
        }

        @Override
        public int insertToLog(String msgId) {
            MariaDBService mariaDBService = (MariaDBService) DatabaseService.MARIADB_SERVICE.getService();
            return mariaDBService.insertToLog(msgId);
        }

        @Override
        public int deleteFromDeliver(String msgId) {
            MariaDBService mariaDBService = (MariaDBService) DatabaseService.MARIADB_SERVICE.getService();
            return mariaDBService.deleteFromDeliver(msgId);
        }

        @Override
        public void moveToDeliver() {
            MariaDBService mariaDBService = (MariaDBService) DatabaseService.MARIADB_SERVICE.getService();
            mariaDBService.moveToDeliver();
        }
    },
    TYPE_OF_ORACLE("ORACLE") {
        @Override
        public boolean checkTableExists() {
            OracleService oracleService = (OracleService) DatabaseService.ORACLE_SERVICE.getService();
            return oracleService.checkTableExists();
        }

        @Override
        public List<MunjaonMsg> selectToDeliver(String msgType) {
            OracleService oracleService = (OracleService) DatabaseService.ORACLE_SERVICE.getService();
            return oracleService.selectToDeliver(msgType);
        }

        @Override
        public int updateToDeliver(String msgId) {
            OracleService oracleService = (OracleService) DatabaseService.ORACLE_SERVICE.getService();
            return oracleService.updateToDeliver(msgId);
        }

        @Override
        public int updateDeliverForList(List<String> msgIdList) {
            OracleService oracleService = (OracleService) DatabaseService.ORACLE_SERVICE.getService();
            return oracleService.updateDeliverForList(msgIdList);
        }

        @Override
        public int updateToReport(MunjaonMsg params) {
            OracleService oracleService = (OracleService) DatabaseService.ORACLE_SERVICE.getService();
            return oracleService.updateToReport(params);
        }

        @Override
        public List<MunjaonMsg> selectToMove() {
            OracleService oracleService = (OracleService) DatabaseService.ORACLE_SERVICE.getService();
            return oracleService.selectToMove();
        }

        @Override
        public List<MunjaonMsg> selectToExpire(String expireTime) {
            OracleService oracleService = (OracleService) DatabaseService.ORACLE_SERVICE.getService();
            return oracleService.selectToExpire(expireTime);
        }

        @Override
        public int insertToLog(String msgId) {
            OracleService oracleService = (OracleService) DatabaseService.ORACLE_SERVICE.getService();
            return oracleService.insertToLog(msgId);
        }

        @Override
        public int deleteFromDeliver(String msgId) {
            OracleService oracleService = (OracleService) DatabaseService.ORACLE_SERVICE.getService();
            return oracleService.deleteFromDeliver(msgId);
        }

        @Override
        public void moveToDeliver() {
            OracleService oracleService = (OracleService) DatabaseService.ORACLE_SERVICE.getService();
            oracleService.moveToDeliver();
        }
    },
    TYPE_OF_MSSQL("MSSQL") {
        @Override
        public boolean checkTableExists() {
            MssqlService mssqlService = (MssqlService) DatabaseService.MSSQL_SERVICE.getService();
            return mssqlService.checkTableExists();
        }

        @Override
        public List<MunjaonMsg> selectToDeliver(String msgType) {
            MssqlService mssqlService = (MssqlService) DatabaseService.MSSQL_SERVICE.getService();
            return mssqlService.selectToDeliver(msgType);
        }

        @Override
        public int updateToDeliver(String msgId) {
            MssqlService mssqlService = (MssqlService) DatabaseService.MSSQL_SERVICE.getService();
            return mssqlService.updateToDeliver(msgId);
        }

        @Override
        public int updateDeliverForList(List<String> msgIdList) {
            MssqlService mssqlService = (MssqlService) DatabaseService.MSSQL_SERVICE.getService();
            return mssqlService.updateDeliverForList(msgIdList);
        }

        @Override
        public int updateToReport(MunjaonMsg params) {
            MssqlService mssqlService = (MssqlService) DatabaseService.MSSQL_SERVICE.getService();
            return mssqlService.updateToReport(params);
        }

        @Override
        public List<MunjaonMsg> selectToMove() {
            MssqlService mssqlService = (MssqlService) DatabaseService.MSSQL_SERVICE.getService();
            return mssqlService.selectToMove();
        }

        @Override
        public List<MunjaonMsg> selectToExpire(String expireTime) {
            MssqlService mssqlService = (MssqlService) DatabaseService.MSSQL_SERVICE.getService();
            return mssqlService.selectToExpire(expireTime);
        }

        @Override
        public int insertToLog(String msgId) {
            MssqlService mssqlService = (MssqlService) DatabaseService.MSSQL_SERVICE.getService();
            return mssqlService.insertToLog(msgId);
        }

        @Override
        public int deleteFromDeliver(String msgId) {
            MssqlService mssqlService = (MssqlService) DatabaseService.ORACLE_SERVICE.getService();
            return mssqlService.deleteFromDeliver(msgId);
        }

        @Override
        public void moveToDeliver() {
            MssqlService oracleService = (MssqlService) DatabaseService.MSSQL_SERVICE.getService();
            oracleService.moveToDeliver();
        }
    },
    TYPE_OF_POSTGRESQL("POSTGRESQL") {
        @Override
        public boolean checkTableExists() {
            PostgresqlService postgresqlService = (PostgresqlService) DatabaseService.POSTGRESQL_SERVICE.getService();
            return postgresqlService.checkTableExists();
        }

        @Override
        public List<MunjaonMsg> selectToDeliver(String msgType) {
            PostgresqlService postgresqlService = (PostgresqlService) DatabaseService.POSTGRESQL_SERVICE.getService();
            return postgresqlService.selectToDeliver(msgType);
        }

        @Override
        public int updateToDeliver(String msgId) {
            PostgresqlService postgresqlService = (PostgresqlService) DatabaseService.POSTGRESQL_SERVICE.getService();
            return postgresqlService.updateToDeliver(msgId);
        }

        @Override
        public int updateDeliverForList(List<String> msgIdList) {
            PostgresqlService postgresqlService = (PostgresqlService) DatabaseService.POSTGRESQL_SERVICE.getService();
            return postgresqlService.updateDeliverForList(msgIdList);
        }

        @Override
        public int updateToReport(MunjaonMsg params) {
            PostgresqlService postgresqlService = (PostgresqlService) DatabaseService.POSTGRESQL_SERVICE.getService();
            return postgresqlService.updateToReport(params);
        }

        @Override
        public List<MunjaonMsg> selectToMove() {
            PostgresqlService postgresqlService = (PostgresqlService) DatabaseService.POSTGRESQL_SERVICE.getService();
            return postgresqlService.selectToMove();
        }

        @Override
        public List<MunjaonMsg> selectToExpire(String expireTime) {
            PostgresqlService postgresqlService = (PostgresqlService) DatabaseService.POSTGRESQL_SERVICE.getService();
            return postgresqlService.selectToExpire(expireTime);
        }

        @Override
        public int insertToLog(String msgId) {
            PostgresqlService postgresqlService = (PostgresqlService) DatabaseService.POSTGRESQL_SERVICE.getService();
            return postgresqlService.insertToLog(msgId);
        }

        @Override
        public int deleteFromDeliver(String msgId) {
            PostgresqlService postgresqlService = (PostgresqlService) DatabaseService.POSTGRESQL_SERVICE.getService();
            return postgresqlService.deleteFromDeliver(msgId);
        }

        @Override
        public void moveToDeliver() {
            PostgresqlService postgresqlService = (PostgresqlService) DatabaseService.POSTGRESQL_SERVICE.getService();
            postgresqlService.moveToDeliver();
        }
    },
    TYPE_OF_MYSQL("MYSQL") {
        @Override
        public boolean checkTableExists() {
            MysqlService mysqlService = (MysqlService) DatabaseService.MYSQL_SERVICE.getService();
            return mysqlService.checkTableExists();
        }

        @Override
        public List<MunjaonMsg> selectToDeliver(String msgType) {
            MysqlService mysqlService = (MysqlService) DatabaseService.MYSQL_SERVICE.getService();
            return mysqlService.selectToDeliver(msgType);
        }

        @Override
        public int updateToDeliver(String msgId) {
            MysqlService mysqlService = (MysqlService) DatabaseService.MYSQL_SERVICE.getService();
            return mysqlService.updateToDeliver(msgId);
        }

        @Override
        public int updateDeliverForList(List<String> msgIdList) {
            MysqlService mysqlService = (MysqlService) DatabaseService.MYSQL_SERVICE.getService();
            return mysqlService.updateDeliverForList(msgIdList);
        }

        @Override
        public int updateToReport(MunjaonMsg params) {
            MysqlService mysqlService = (MysqlService) DatabaseService.MYSQL_SERVICE.getService();
            return mysqlService.updateToReport(params);
        }

        @Override
        public List<MunjaonMsg> selectToMove() {
            MysqlService mysqlService = (MysqlService) DatabaseService.MYSQL_SERVICE.getService();
            return mysqlService.selectToMove();
        }

        @Override
        public List<MunjaonMsg> selectToExpire(String expireTime) {
            MysqlService mysqlService = (MysqlService) DatabaseService.MYSQL_SERVICE.getService();
            return mysqlService.selectToExpire(expireTime);
        }

        @Override
        public int insertToLog(String msgId) {
            MysqlService mysqlService = (MysqlService) DatabaseService.MYSQL_SERVICE.getService();
            return mysqlService.insertToLog(msgId);
        }

        @Override
        public int deleteFromDeliver(String msgId) {
            MysqlService mysqlService = (MysqlService) DatabaseService.MYSQL_SERVICE.getService();
            return mysqlService.deleteFromDeliver(msgId);
        }

        @Override
        public void moveToDeliver() {
            MysqlService mysqlService = (MysqlService) DatabaseService.MYSQL_SERVICE.getService();
            mysqlService.moveToDeliver();
        }
    },
    TYPE_OF_TIBERO("TIBERO") {
        @Override
        public boolean checkTableExists() {
            TiberoService tiberoService = (TiberoService) DatabaseService.TIBERO_SERVICE.getService();
            return tiberoService.checkTableExists();
        }

        @Override
        public List<MunjaonMsg> selectToDeliver(String msgType) {
            TiberoService tiberoService = (TiberoService) DatabaseService.TIBERO_SERVICE.getService();
            return tiberoService.selectToDeliver(msgType);
        }

        @Override
        public int updateToDeliver(String msgId) {
            TiberoService tiberoService = (TiberoService) DatabaseService.TIBERO_SERVICE.getService();
            return tiberoService.updateToDeliver(msgId);
        }

        @Override
        public int updateDeliverForList(List<String> msgIdList) {
            TiberoService tiberoService = (TiberoService) DatabaseService.TIBERO_SERVICE.getService();
            return tiberoService.updateDeliverForList(msgIdList);
        }

        @Override
        public int updateToReport(MunjaonMsg params) {
            TiberoService tiberoService = (TiberoService) DatabaseService.TIBERO_SERVICE.getService();
            return tiberoService.updateToReport(params);
        }

        @Override
        public List<MunjaonMsg> selectToMove() {
            TiberoService tiberoService = (TiberoService) DatabaseService.TIBERO_SERVICE.getService();
            return tiberoService.selectToMove();
        }

        @Override
        public List<MunjaonMsg> selectToExpire(String expireTime) {
            TiberoService tiberoService = (TiberoService) DatabaseService.TIBERO_SERVICE.getService();
            return tiberoService.selectToExpire(expireTime);
        }

        @Override
        public int insertToLog(String msgId) {
            TiberoService tiberoService = (TiberoService) DatabaseService.TIBERO_SERVICE.getService();
            return tiberoService.insertToLog(msgId);
        }

        @Override
        public int deleteFromDeliver(String msgId) {
            TiberoService tiberoService = (TiberoService) DatabaseService.TIBERO_SERVICE.getService();
            return tiberoService.deleteFromDeliver(msgId);
        }

        @Override
        public void moveToDeliver() {
            TiberoService tiberoService = (TiberoService) DatabaseService.TIBERO_SERVICE.getService();
            tiberoService.moveToDeliver();
        }
    };

    DatabaseTypeWorker(final String name) {
        this.type = name;
    }
    private final String type;
    public static DatabaseTypeWorker find(String type) {
        for (DatabaseTypeWorker databaseTypeWorker : EnumSet.allOf(DatabaseTypeWorker.class)) {
            if (type.equals(databaseTypeWorker.getType())) {
                return databaseTypeWorker;
            }
        }

        return null;
    }

    public abstract boolean checkTableExists();
    public abstract List<MunjaonMsg> selectToDeliver(String msgType);
    public abstract int updateToDeliver(String msgId);
    public abstract int updateDeliverForList(List<String> msgIdList);;
    public abstract int updateToReport(MunjaonMsg params);
    public abstract List<MunjaonMsg> selectToMove();
    public abstract List<MunjaonMsg> selectToExpire(String expireTime);
    public abstract int insertToLog(String msgId);
    public abstract int deleteFromDeliver(String msgId);
    public abstract void moveToDeliver();
}
