package com.munjaon.client.server.service;

import com.munjaon.client.service.DatabaseTypeWorker;
import org.json.simple.JSONObject;

public class DataMoveService extends Service {
    private DatabaseTypeWorker worker;

    public DataMoveService(String serviceName) {
        super(serviceName);
    }

    @Override
    public void checkReady() {
        worker = DatabaseTypeWorker.find(System.getProperty("DBMS"));
        if (worker == null) {
            saveSystemLog("[checkReady][DBMS : " + System.getProperty("DBMS") + " is not ready]");
            return;
        }

        this.IS_READY_YN = worker.checkTableExists();
        if (this.IS_READY_YN) {
            saveSystemLog("[checkReady][DBMS : " + System.getProperty("DBMS") + " is ready]");
        } else {
            saveSystemLog("[checkReady][DBMS : " + System.getProperty("DBMS") + " is not ready]");
        }
    }

    @Override
    public void initResources() {

    }

    @Override
    public void releaseResources() {

    }

    @Override
    public void doService() {
        while (isRun()) {
            try {
                messageService();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    private void messageService() {
        try {
            while (isRun()) {
                worker.moveToDeliver();
                Thread.sleep(3000L);
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @Override
    public JSONObject monitorService() {
        return null;
    }
}
