package example;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.freedomoss.crowdcontrol.webharvest.plugin.datastore.enums.DataStoreColumnType;
import org.webharvest.runtime.processors.plugins.DbRowVariable;

import com.workfusion.rpa.core.plugin.PluginAdapterFactory;
import com.workfusion.rpa.core.plugin.datastore.DatastorePluginAdapter;

import static com.workfusion.rpa.core.plugin.PluginAdapterFactory.PluginsEnum.DATA_STORE;

public class DatastoreExample  {

    private final DatastorePluginAdapter datastorePluginAdapter = PluginAdapterFactory.getInstance().getPluginAdapter(DATA_STORE);
    private static final String DS_NAME = "awesome_db_name_examplle";
    private static final String TEXT_COLUNM_NAME = "textcolumn";
    private static final String DATE_COLUNM_NAME = "datecolumn";
    private static final String INT_COLUNM_NAME = "intcol";
    private static final String TIMESTAMP_COLUNM_NAME = "timestampcol";
    private static final String DATE = "2006-11-11";
    private static final String TIMESTAMP = "2016-06-22 19:10:25";


    public void update() {
        String updateQuery = "update @this set timestampcol = CURRENT_TIMESTAMP where textcolumn = 'rowToBeUpdated' and textcolumn != '''\"<>&'";
        datastorePluginAdapter.updateQuery(DS_NAME, updateQuery);
    }

    public List<DbRowVariable> select() {
        //language=SQL
        final String query1 = "select * from @this where textcolumn != '''\"<>&'";//language=SQL
        return datastorePluginAdapter.selectQuery(DS_NAME, query1);

    }



    public void insertRow() {

        final Map<String, String> row = new HashMap<>();
        row.put(TEXT_COLUNM_NAME, "single row insert b1&<ldkfn dfv=''sfgn''>sbt gs rtb</sd\"''><_ ");
        //  row.put(DATE_COLUNM_NAME, DATE);
        // row.put(INT_COLUNM_NAME, Integer.MAX_VALUE + "");//Long is not supported here
        // row.put(TIMESTAMP_COLUNM_NAME, TIMESTAMP);

        datastorePluginAdapter.insertRow(DS_NAME, row);
    }

    public void insertRowsListOfRows() {

        final Map<String, String> row = new HashMap<>();
        row.put(TEXT_COLUNM_NAME, "multipleinsertRowsAsMap");
        row.put(DATE_COLUNM_NAME, DATE);
        row.put(INT_COLUNM_NAME, "8888999");
        row.put(TIMESTAMP_COLUNM_NAME, TIMESTAMP);

        final List<Map<String, String>> objects = new ArrayList<>();

        objects.add(row);
        objects.add(row);
        objects.add(row);

        datastorePluginAdapter.insertRowsAsMap(DS_NAME, objects);
    }

    public void insertRowsListOfLists() {
        final String  rowToBeDeleted = "rowToBeDeleted";
        final List<List<String>> objects = new ArrayList<>();
        objects.add(Arrays.asList(TEXT_COLUNM_NAME, DATE_COLUNM_NAME, INT_COLUNM_NAME, TIMESTAMP_COLUNM_NAME));
        objects.add(Arrays.asList("multipleinsertRowsAsList", DATE, "1111", TIMESTAMP));
        objects.add(Arrays.asList("multipleinsertRowsAsList", DATE, "2222", TIMESTAMP));
        objects.add(Arrays.asList("rowToBeUpdated", DATE, "3333", TIMESTAMP));
        objects.add(Arrays.asList("rowToBeUpdated", DATE, "4444", TIMESTAMP));
        objects.add(Arrays.asList(rowToBeDeleted, DATE, "5555", TIMESTAMP));
        objects.add(Arrays.asList(rowToBeDeleted, DATE, "6666", TIMESTAMP));
        objects.add(Arrays.asList(rowToBeDeleted, DATE, "7777", TIMESTAMP));
        objects.add(Arrays.asList(rowToBeDeleted, DATE, "8888", TIMESTAMP));

        datastorePluginAdapter.insertRowsAsList(DS_NAME, objects);

    }

    public void createDatastore() {

        final Map<String, DataStoreColumnType> s = new HashMap();
        s.put(TEXT_COLUNM_NAME, DataStoreColumnType.TEXT);
        s.put(DATE_COLUNM_NAME, DataStoreColumnType.DATE);
        s.put(INT_COLUNM_NAME, DataStoreColumnType.INTEGER);
        s.put(TIMESTAMP_COLUNM_NAME, DataStoreColumnType.TIMESTAMP);

        datastorePluginAdapter.createDatastore(DS_NAME, s, null);

    }

    public void delete() {
        final String deleteQuery = "delete FROM @this WHERE (@id % 2 = 0)  and textcolumn = 'rowToBeDeleted';";
        datastorePluginAdapter.deleteQuery(DS_NAME, deleteQuery);
    }


    public void exceptionalExecution(){

        try {
            datastorePluginAdapter.selectQuery("name that does not exist", "select * from @this");
        } catch (Exception e) {
            final StringWriter sw = new StringWriter();
            final PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            final String sStackTrace = "Planned exception: "  +  sw.toString();

            final Map<String, String> row = new HashMap<>();
            row.put(TEXT_COLUNM_NAME, (sStackTrace.replaceAll("'", "''")));

            datastorePluginAdapter.insertRow(DS_NAME, row);
        }
    }


}
