package eu.dnetlib.usagestats.controllers;

import com.fasterxml.jackson.databind.ObjectMapper;

import eu.dnetlib.usagestats.sushilite.UsageReport;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by tsampikos on 10/4/2017.
 */

@RestController
class SushiLiteController {

    private final Logger log = Logger.getLogger(this.getClass());

    @Autowired
    private UsageReport report;

    /*
    @RequestMapping(value = "/", method = RequestMethod.GET)
    String defaultMap(){
        return "<body><center>" +
                "<a href=\"/usagestats/sushilite\"><h2>OpenAIRE SUSHI Lite Client</h2></a>" +
                "</center></body>";
    }
    */

    /*
    @RequestMapping(value = "/sushilite", method = RequestMethod.GET)
    public String sushilite(){
        return "<body><center>" +
                "<h2>OpenAIRE SUSHI Lite Client</h2>" +
                "<h3> Supported Reports</h3>" +
                "<ul>" +
                "<li><a href=\"/usagestats/sushilite/AR1/\">AR1 Report</a></li>" +
                "<li><a href=\"/usagestats/sushilite/IR1/\">IR1 Report</a></li>" +
                "<li><a href=\"/usagestats/sushilite/RR1/\">RR1 Report</a></li>" +
                "</ul>" +
                "</center></body>";
    }
    */

    /*
    @RequestMapping(value = "/sushilite/AR1", method = RequestMethod.GET)
    public String ar1Map(){
        return "<body>" +
                "<h2>OpenAIRE SUSHI Lite Client</h2>" +
                "<form action=\"/usagestats/sushilite/GetReport/\" method=\"get\">" +
                "<h3>Report Request</h3>" +
                "Report Name: " +
                "<select name='Report'>" +
                "<option value='AR1'>AR1</option>" +
                "</select>" +
                "<br />" +
                "Release: " +
                "<select name='Release'>" +
                "<option value=\"4\">4</option>" +
                "</select>" +
                "<br />" +
                "Requestor: " +
                "<input type=\"text\" name=\"RequestorID\" value=\"anonymous\">" +
                "<h3>Report Filters</h3>" +
                "<h4>DateRange</h4>" +
                "<p>Valid date formats: yyyy-mm-dd or yyyy-mm<br />" +
                "Defaults to the last available month if no dates are provided" +
                "</p>" +
                "BeginDate:<input type=\"text\" name=\"BeginDate\" value=\"2016-03\" style=\"width: 80px; padding:2px\"/>" +
                "<br />" +
                "EndDate:<input type=\"text\" name=\"EndDate\" value=\"2016-06\"  style=\"width: 80px; padding:2px\"/>" +
                "<h4>Filters</h4>" +
                "<p>You must provide either a Repository Identifier(available prefix: openaire and opendoar) or an Item Identifier(available prefix: openaire, oid and doi)</p>" +
                //"<p>Repository Identifier format: namespace:value<br />" +
                //"namespace must be either be openaire or repoid<br />" +
                //"value is the integer value assigned by CORE or IRUS-UK" +
                //"</p>" +

                "RepositoryIdentifier:" +
                "<input type=\"text\" name=\"RepositoryIdentifier\">" +
                "<br />" +
                "ItemIdentifier:" +
                "<input type=\"text\" name=\"ItemIdentifier\" value=\"openaire:od_________7::fb90de6f20d79783d05749d8f60417d5\">" +

                //"<p>You may also limit the results to include or exclude items that have a DOI<br />" +
                //"Valid values: Yes or No</p>" +
                //"hasDOI:" +
                //"<select name='hasDOI'>" +
                //"<option value=\"\">Not applicable</option>" +
                //"<option value=\"Yes\">Yes</option>" +
                //"<option value=\"No\">No</option>" +
                //"</select>" +

                "<h3>Report Attributes</h3>" +
                "<p>Valid Granularity values: Monthly or Totals</p>" +
                "Granularity:" +
                "<select name='Granularity'>" +
                "<option value=\"Monthly\">Monthly</option>" +
                "<option value=\"Totals\">Totals</option>" +
                "</select>" +
                //"<p>The format in which the response is to be returned defaults to JSON unless a callback function name is entered in which case JSONP is returned.</p>" +
                //"<input type=\"text\" name=\"Callback\">" +
                "<p>The Pretty attribute is just for humans playing with the API and looking at results in a browser.</p>" +
                "<p><input type=\"checkbox\" name=\"Pretty\" value=\"Pretty\" checked=\"checked\">Pretty print json(p) for humans</p>" +
                "<input type=\"submit\" class=\"button\" value=\"GetReport\"/>" +
                "</form>" +
                "</body>";
    }
    */

    /*
    @RequestMapping(value = "/sushilite/IR1", method = RequestMethod.GET)
    public String ir1Map(){
        return "<body>" +
                "<h2>OpenAIRE SUSHI Lite Client</h2>" +
                "<form action=\"/usagestats/sushilite/GetReport/\" method=\"get\">" +
                "<h3>Report Request</h3>" +
                "Report Name: " +
                "<select name='Report'>" +
                "<option value='IR1'>IR1</option>" +
                "</select>" +
                "<br />" +
                "Release: " +
                "<select name='Release'>" +
                "<option value=\"4\">4</option>" +
                "</select>" +
                "<br />" +
                "Requestor: " +
                "<input type=\"text\" name=\"RequestorID\" value=\"anonymous\">" +
                "<h3>Report Filters</h3>" +
                "<h4>DateRange</h4>" +
                "<p>Valid date formats: yyyy-mm-dd or yyyy-mm<br />" +
                "Defaults to the last available month if no dates are provided" +
                "</p>" +
                "BeginDate:<input type=\"text\" name=\"BeginDate\" value=\"2016-03\" style=\"width: 80px; padding:2px\"/>" +
                "<br />" +
                "EndDate:<input type=\"text\" name=\"EndDate\" value=\"2016-06\"  style=\"width: 80px; padding:2px\"/>" +
                "<h4>Filters</h4>" +
                "<p>You must provide either a Repository Identifier(available prefix: openaire and opendoar) or an Item Identifier(available prefix: openaire, oid and doi)</p>" +
                //"<p>Repository Identifier format: namespace:value<br />" +
                //"namespace must be either be openaire or repoid<br />" +
                //"value is the integer value assigned by CORE or IRUS-UK" +
                //"</p>" +

                "RepositoryIdentifier:" +
                "<input type=\"text\" name=\"RepositoryIdentifier\" value=\"openaire:opendoar____::7eabe3a1649ffa2b3ff8c02ebfd5659f\">" +
                "<br />" +
                "ItemIdentifier:" +
                "<input type=\"text\" name=\"ItemIdentifier\">" +

                "<p>Optional filter to only show results for a single item type, e.g. article, book, etc.</p>" +
                "ItemDataType:" +
                "<select name='ItemDataType'>" +
                "<option value=\"\">--- Select ItemDataType ---</option>" +
                "<option value='Annotation'>Annotation</option>" +
                "<option value='Article'>Article</option>" +
                "<option value='Bachelor thesis'>Bachelor thesis</option>" +
                "<option value='Book'>Book</option>" +
                "<option value='clinical trial'>clinical trial</option>" +
                "<option value='Collection'>Collection</option>" +
                "<option value='Conference object'>Conference object</option>" +
                "<option value='Contribution for newspaper or weekly magazine'>Contribution for newspaper or weekly magazine</option>" +
                "<option value='Dataset'>Dataset</option>" +
                "<option value='Doctoral thesis'>Doctoral thesis</option>" +
                "<option value='Event'>Event</option>" +
                "<option value='External research sushilite'>External research sushilite</option>" +
                "<option value='Film'>Film</option>" +
                "<option value='Image'>Image</option>" +
                "<option value='InteractiveResource'>InteractiveResource</option>" +
                "<option value='Internal sushilite'>Internal sushilite</option>" +
                "<option value='Lecture'>Lecture</option>" +
                "<option value='Master thesis'>Master thesis</option>" +
                "<option value='Newsletter'>Newsletter</option>" +
                "<option value='Other'>Other</option>" +
                "<option value='Part of book or chapter of book'>Part of book or chapter of book</option>" +
                "<option value='Patent'>Patent</option>" +
                "<option value='PhysicalObject'>PhysicalObject</option>" +
                "<option value='Preprint'>Preprint</option>" +
                "<option value='Report'>Report</option>" +
                "<option value='Research'>Research</option>" +
                "<option value='Review'>Review</option>" +
                "<option value='Software'>Software</option>" +
                "<option value='Sound'>Sound</option>" +
                "<option value='Unknown'>Unknown</option>" +
                "</select>" +

                //"<p>You may also limit the results to include or exclude items that have a DOI<br />" +
                //"Valid values: Yes or No</p>" +
                //"hasDOI:" +
                //"<select name='hasDOI'>" +
                //"<option value=\"\">Not applicable</option>" +
                //"<option value=\"Yes\">Yes</option>" +
                //"<option value=\"No\">No</option>" +
                //"</select>" +

                "<h3>Report Attributes</h3>" +
                "<p>Valid Granularity values: Monthly or Totals</p>" +
                "Granularity:" +
                "<select name='Granularity'>" +
                "<option value=\"Monthly\">Monthly</option>" +
                "<option value=\"Totals\">Totals</option>" +
                "</select>" +
                //"<p>The format in which the response is to be returned defaults to JSON unless a callback function name is entered in which case JSONP is returned.</p>" +
                //"<input type=\"text\" name=\"Callback\">" +
                "<p>The Pretty attribute is just for humans playing with the API and looking at results in a browser.</p>" +
                "<p><input type=\"checkbox\" name=\"Pretty\" value=\"Pretty\" checked=\"checked\">Pretty print json(p) for humans</p>" +
                "<input type=\"submit\" class=\"button\" value=\"GetReport\"/>" +
                "</form>" +
                "</body>";
    }
    */

    /*
    @RequestMapping(value = "/sushilite/RR1", method = RequestMethod.GET)
    public String rr1Map(){
        return "<body>" +
                "<h2>OpenAIRE SUSHI Lite Client</h2>" +
                "<form action=\"/usagestats/sushilite/GetReport/\" method=\"get\">" +
                "<h3>Report Request</h3>" +
                "Report Name: " +
                "<select name='Report'>" +
                "<option value='RR1'>RR1</option>" +
                "</select>" +
                "<br />" +
                "Release: " +
                "<select name='Release'>" +
                "<option value=\"4\">4</option>" +
                "</select>" +
                "<br />" +
                "Requestor: " +
                "<input type=\"text\" name=\"RequestorID\" value=\"anonymous\">" +
                "<h3>Report Filters</h3>" +
                "<h4>DateRange</h4>" +
                "<p>Valid date formats: yyyy-mm-dd or yyyy-mm<br />" +
                "Defaults to the last available month if no dates are provided" +
                "</p>" +
                "BeginDate:<input type=\"text\" name=\"BeginDate\" value=\"2016-03\" style=\"width: 80px; padding:2px\"/>" +
                "<br />" +
                "EndDate:<input type=\"text\" name=\"EndDate\" value=\"2016-06\"  style=\"width: 80px; padding:2px\"/>" +
                "<h4>Filters</h4>" +
                //"<p>You must provide either a Repository Identifier or an Item Identifier</p>" +
                //"<p>Repository Identifier format: namespace:value<br />" +
                //"namespace must be either be openaire or repoid<br />" +
                //"value is the integer value assigned by CORE or IRUS-UK" +
                //"</p>" +

                "RepositoryIdentifier(available prefix: openaire and opendoar):" +
                "<input type=\"text\" name=\"RepositoryIdentifier\">" +
                "<br />" +

                "<p>Optional filter to only show results for a single item type, e.g. article, book, etc.</p>" +
                "ItemDataType:" +
                "<select name='ItemDataType'>" +
                "<option value=\"\">--- Select ItemDataType ---</option>" +
                "<option value='Annotation'>Annotation</option>" +
                "<option value='Article'>Article</option>" +
                "<option value='Bachelor thesis'>Bachelor thesis</option>" +
                "<option value='Book'>Book</option>" +
                "<option value='clinical trial'>clinical trial</option>" +
                "<option value='Collection'>Collection</option>" +
                "<option value='Conference object'>Conference object</option>" +
                "<option value='Contribution for newspaper or weekly magazine'>Contribution for newspaper or weekly magazine</option>" +
                "<option value='Dataset'>Dataset</option>" +
                "<option value='Doctoral thesis'>Doctoral thesis</option>" +
                "<option value='Event'>Event</option>" +
                "<option value='External research sushilite'>External research sushilite</option>" +
                "<option value='Film'>Film</option>" +
                "<option value='Image'>Image</option>" +
                "<option value='InteractiveResource'>InteractiveResource</option>" +
                "<option value='Internal sushilite'>Internal sushilite</option>" +
                "<option value='Lecture'>Lecture</option>" +
                "<option value='Master thesis'>Master thesis</option>" +
                "<option value='Newsletter'>Newsletter</option>" +
                "<option value='Other'>Other</option>" +
                "<option value='Part of book or chapter of book'>Part of book or chapter of book</option>" +
                "<option value='Patent'>Patent</option>" +
                "<option value='PhysicalObject'>PhysicalObject</option>" +
                "<option value='Preprint'>Preprint</option>" +
                "<option value='Report'>Report</option>" +
                "<option value='Research'>Research</option>" +
                "<option value='Review'>Review</option>" +
                "<option value='Software'>Software</option>" +
                "<option value='Sound'>Sound</option>" +
                "<option value='Unknown'>Unknown</option>" +
                "</select>" +

                //"<p>You may also limit the results to include or exclude items that have a DOI<br />" +
                //"Valid values: Yes or No</p>" +
                //"hasDOI:" +
                //"<select name='hasDOI'>" +
                //"<option value=\"\">Not applicable</option>" +
                //"<option value=\"Yes\">Yes</option>" +
                //"<option value=\"No\">No</option>" +
                //"</select>" +

                "<h3>Report Attributes</h3>" +
                "<p>Valid Granularity values: Monthly or Totals</p>" +
                "Granularity:" +
                "<select name='Granularity'>" +
                "<option value=\"Monthly\">Monthly</option>" +
                "<option value=\"Totals\">Totals</option>" +
                "</select>" +
                //"<p>The format in which the response is to be returned defaults to JSON unless a callback function name is entered in which case JSONP is returned.</p>" +
                //"<input type=\"text\" name=\"Callback\">" +
                "<p>The Pretty attribute is just for humans playing with the API and looking at results in a browser.</p>" +
                "<p><input type=\"checkbox\" name=\"Pretty\" value=\"Pretty\" checked=\"checked\">Pretty print json(p) for humans</p>" +
                "<input type=\"submit\" class=\"button\" value=\"GetReport\"/>" +
                "</form>" +
                "</body>";
    }
    */

    @RequestMapping(value = "/sushilite/GetReport/", method = RequestMethod.GET)
    public String getReport(@RequestParam(value = "Report", defaultValue = "") String reportP, @RequestParam(value = "Release", defaultValue = "4") String release, @RequestParam(value = "RequestorID", defaultValue = "anonymous") String requestorId,
                            @RequestParam(value = "BeginDate", defaultValue = "") String beginDate, @RequestParam(value = "EndDate", defaultValue = "") String endDate, @RequestParam(value = "RepositoryIdentifier", defaultValue = "") String repositoryIdentifier,
                            @RequestParam(value = "ItemIdentifier", defaultValue = "") String itemIdentifier, @RequestParam(value = "ItemDataType", defaultValue = "") String itemDataType,
                            @RequestParam(value = "hasDOI", defaultValue = "") String hasDoi, @RequestParam(value = "Granularity", defaultValue = "Monthly") String granularity, @RequestParam(value = "Callback", defaultValue = "") String callback,
                            @RequestParam(value = "Pretty", defaultValue = "") String pretty) throws com.fasterxml.jackson.core.JsonProcessingException{
        log.info("Sushi Report request: " + reportP + " from " + requestorId);
        log.info("repository identifier: " + repositoryIdentifier + " - item identifier: " + itemIdentifier);

        ObjectMapper objectMapper = new ObjectMapper();
        if (pretty.equalsIgnoreCase("pretty")) {
            //objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
            return "<pre>" + objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(report.getReport(reportP, release, requestorId, beginDate, endDate, repositoryIdentifier, itemIdentifier, itemDataType, hasDoi, granularity, callback)).replaceAll("/", "\\\\/") + "</pre>";
        }
        return objectMapper.writeValueAsString(report.getReport(reportP, release, requestorId, beginDate, endDate, repositoryIdentifier, itemIdentifier, itemDataType, hasDoi, granularity, callback)).replaceAll("/", "\\\\/");
        //return report.getReport(reportP, release, requestorId, beginDate, endDate, repositoryIdentifier, itemIdentifier, itemDataType, hasDoi, granularity, callback, pretty);
    }
}
