package fi.nls.oskari.db;

import fi.nls.oskari.domain.map.view.Bundle;
import fi.nls.oskari.log.LogFactory;
import fi.nls.oskari.log.Logger;
import fi.nls.oskari.map.view.BundleService;
import fi.nls.oskari.map.view.BundleServiceMybatisImpl;
import fi.nls.oskari.util.IOHelper;
import fi.nls.oskari.util.OskariRuntimeException;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BundleHelper {

    private static final Logger LOG = LogFactory.getLogger(BundleHelper.class);
    private static final BundleService SERVICE = new BundleServiceMybatisImpl();

    private BundleHelper() {
    }

    /**
     * @deprecated startup is no longer used for bundles
     * @param namespace
     * @param bundleid
     * @param title
     * @return
     */
    public static String getDefaultBundleStartup(final String namespace, final String bundleid, final String title) {
        return null;
    }
    public static String getBundleStartup(final String path, final String bundleid, final String title) {
        if(bundleid == null) {
            throw new OskariRuntimeException("Missing bundleid");
        }
        return null;
    }

    public static boolean isBundleRegistered(final String id) {
        return SERVICE.getBundleTemplateByName(id) != null;
    }

    public static void registerBundle(final Bundle bundle) {
        if(isBundleRegistered(bundle.getName())) {
            // already registered
            LOG.info("Bundle", bundle.getName(), "already registered - Skipping!");
            return;
        }
        SERVICE.addBundleTemplate(bundle);
    }

    public static boolean isBundleRegistered(final String id, Connection conn) throws SQLException {
        return getRegisteredBundle(id, conn) != null;
    }

    public static void registerBundle(final Bundle bundle, Connection conn) throws SQLException {
        if(isBundleRegistered(bundle.getName(), conn)) {
            // already registered
            LOG.info("Bundle", bundle.getName(), "already registered - Skipping!");
            return;
        }

        try(PreparedStatement statement =
                    conn.prepareStatement("INSERT INTO portti_bundle(name, config, state) VALUES(?,?,?)")) {
            statement.setString(1,bundle.getName());
            statement.setString(2,bundle.getConfig());
            statement.setString(3,bundle.getState());
            statement.execute();
        }
    }

    public static Bundle getRegisteredBundle(final String id, Connection conn) throws SQLException {
        try(PreparedStatement statement =
                    conn.prepareStatement("SELECT id, name, config, state FROM portti_bundle WHERE name=?")) {
            statement.setString(1, id);
            try (ResultSet rs = statement.executeQuery()) {
                if(!rs.next()) {
                    return null;
                }
                Bundle b = new Bundle();
                b.setBundleId(rs.getLong("id"));
                b.setName(rs.getString("name"));
                b.setConfig(rs.getString("config"));
                b.setState(rs.getString("state"));
                return b;
            }
        }
    }
}

@@ -995,24 +995,25 @@
 null;%0A    %7D%0A
+%0A
     public s
@@ -1116,16 +1116,17 @@
       if
+ 
 (bundlei
@@ -1436,32 +1436,33 @@
 le) %7B%0A        if
+ 
 (isBundleRegiste
@@ -1954,16 +1954,17 @@
       if
+ 
 (isBundl
@@ -2154,32 +2154,33 @@
   %7D%0A%0A        try
+ 
 (PreparedStateme
@@ -2185,32 +2185,37 @@
 ment statement =
+ conn
 %0A               
@@ -2211,32 +2211,24 @@
             
-    conn
 .prepareStat
@@ -2334,16 +2334,17 @@
 tring(1,
+ 
 bundle.g
@@ -2388,16 +2388,17 @@
 tring(2,
+ 
 bundle.g
@@ -2444,16 +2444,17 @@
 tring(3,
+ 
 bundle.g
@@ -2627,16 +2627,17 @@
      try
+ 
 (Prepare
@@ -2658,16 +2658,21 @@
 tement =
+ conn
 %0A       
@@ -2684,16 +2684,8 @@
     
-    conn
 .pre
@@ -2885,16 +2885,17 @@
       if
+ 
 (!rs.nex
@@ -3250,11 +3250,502 @@
  %7D%0A    %7D
+%0A%0A    public static void removeBundle(final Bundle bundle, Connection conn) throws SQLException %7B%0A        if (isBundleRegistered(bundle.getName(), conn)) %7B%0A            try (PreparedStatement statement = conn.prepareStatement(%22DELETE FROM portti_bundle WHERE name=?%22)) %7B%0A                statement.setString(1, bundle.getName());%0A                statement.execute();%0A            %7D%0A        %7D else %7B%0A            LOG.info(%22Bundle%22, bundle.getName(), %22not registered - Skipping!%22);%0A        %7D%0A    %7D
 %0A%7D%0A

Add method to remove bundle