package eu.dnetlib.espas.wrapper;

import eu.dnetlib.api.enabling.DatabaseService;
import eu.dnetlib.api.enabling.ISLookUpService;
import eu.dnetlib.api.enabling.ISLookUpServiceException;
import eu.dnetlib.api.enabling.ISRegistryService;
import eu.dnetlib.api.enabling.ISRegistryServiceException;
import eu.dnetlib.api.enabling.ISSNService;
import eu.dnetlib.api.enabling.ISSNServiceException;
import eu.dnetlib.api.enabling.ResultSetService;
import eu.dnetlib.api.functionality.UserProfileService;
import gr.uoa.di.driver.util.ServiceLocator;
import org.springframework.web.HttpRequestHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by antleb on 3/20/14.
 */
public class StatusServlet implements HttpRequestHandler {

    private ServiceLocator<ISLookUpService> lookUpServiceServiceLocator;
	private ServiceLocator<ISRegistryService> registryServiceServiceLocator;
	private ServiceLocator<DatabaseService> databaseServiceServiceLocator;
	private ServiceLocator<ResultSetService> resultSetServiceServiceLocator;
	private ServiceLocator<ISSNService> issnServiceServiceLocator;

    private String ISUrl;
	private String dbName;

    private String version;


    @Override
    public void handleRequest(
            HttpServletRequest httpServletRequest,
            HttpServletResponse httpServletResponse) throws ServletException, IOException {


        PrintWriter w = httpServletResponse.getWriter();

        httpServletResponse.setContentType("text/html");
        httpServletResponse.setCharacterEncoding("utf-8");

        w.write("<html><body>");
        w.write("<h1>ESPAS Wrapper status</h1>");
        w.write("<table border='1'><tr>");
        w.write("<th>version</th><td>");
        w.write(version);
        w.write("</td></tr>");

		w.write("<tr><th>database</th><td>");
		w.write(dbName);
		w.write("</td></tr>");

        w.write("<tr><th>IS url</th><td>");
        w.write(ISUrl);
        w.write("</td></tr>");
        w.write("<tr><th>connection with lookup service</th><td");
        try {
            lookUpServiceServiceLocator.getService().searchProfile("collection('/db/DRIVER/')//RESOURCE_PROFILE");
            w.write(" bgcolor='lightgreen'>OK");
        }  catch (ISLookUpServiceException e) {
           w.write(" bgcolor='red'>ERROR");
        }
        w.write("</td></tr>");
		w.write("<tr><th>connection with registry service</th><td");
		try {
			registryServiceServiceLocator.getService().identify();
			w.write(" bgcolor='lightgreen'>OK");
		} catch (Exception e) {
			w.write(" bgcolor='red'>ERROR");
		}
		w.write("</td></tr>");
		w.write("<tr><th>connection with ISSN service</th><td");
		try {
			issnServiceServiceLocator.getService().identify();
			w.write(" bgcolor='lightgreen'>OK");
		} catch (Exception e) {
			w.write(" bgcolor='red'>ERROR");
		}
		w.write("</td></tr>");
		w.write("<tr><th>connection with ResultSet service</th><td");
		try {
			resultSetServiceServiceLocator.getService().identify();
			w.write(" bgcolor='lightgreen'>OK");
		} catch (Exception e) {
			w.write(" bgcolor='red'>ERROR");
		}
		w.write("</td></tr>");
		w.write("<tr><th>connection with Database service</th><td");
		try {
			databaseServiceServiceLocator.getService().identify();
			w.write(" bgcolor='lightgreen'>OK");
		} catch (Exception e) {
			w.write(" bgcolor='red'>ERROR");
		}
		w.write("</td></tr>");
		w.write("</table></body></html>");
    }

    public ServiceLocator<ISLookUpService> getLookUpServiceServiceLocator() {
        return lookUpServiceServiceLocator;
    }

    public void setLookUpServiceServiceLocator(ServiceLocator<ISLookUpService> lookUpServiceServiceLocator) {
        this.lookUpServiceServiceLocator = lookUpServiceServiceLocator;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getISUrl() {
        return ISUrl;
    }

    public void setISUrl(String ISUrl) {
        this.ISUrl = ISUrl;
    }

	public String getDbName() {
		return dbName;
	}

	public void setDbName(String dbName) {
		this.dbName = dbName;
	}

	public ServiceLocator<ISRegistryService> getRegistryServiceServiceLocator() {
		return registryServiceServiceLocator;
	}

	public void setRegistryServiceServiceLocator(ServiceLocator<ISRegistryService> registryServiceServiceLocator) {
		this.registryServiceServiceLocator = registryServiceServiceLocator;
	}

	public ServiceLocator<DatabaseService> getDatabaseServiceServiceLocator() {
		return databaseServiceServiceLocator;
	}

	public void setDatabaseServiceServiceLocator(ServiceLocator<DatabaseService> databaseServiceServiceLocator) {
		this.databaseServiceServiceLocator = databaseServiceServiceLocator;
	}

	public ServiceLocator<ResultSetService> getResultSetServiceServiceLocator() {
		return resultSetServiceServiceLocator;
	}

	public void setResultSetServiceServiceLocator(ServiceLocator<ResultSetService> resultSetServiceServiceLocator) {
		this.resultSetServiceServiceLocator = resultSetServiceServiceLocator;
	}

	public ServiceLocator<ISSNService> getIssnServiceServiceLocator() {
		return issnServiceServiceLocator;
	}

	public void setIssnServiceServiceLocator(ServiceLocator<ISSNService> issnServiceServiceLocator) {
		this.issnServiceServiceLocator = issnServiceServiceLocator;
	}
}