package eu.dnetlib.usagestats.controllers;

import eu.dnetlib.usagestats.domain.UsageStats;
import eu.dnetlib.usagestats.repos.DatasourceRepo;
import eu.dnetlib.usagestats.repos.OrganizationRepo;
import eu.dnetlib.usagestats.repos.ProjectRepo;
import eu.dnetlib.usagestats.repos.ResultRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by eri_k on 3/27/2016.
 */
@RestController
@Transactional
public class UsageStatsController {
    @Autowired
    DatasourceRepo datasourceRepo;

    @Autowired
    ResultRepo resultRepo;

    @Autowired
    OrganizationRepo organizationRepo;

    @Autowired
    ProjectRepo projectRepo;


    //-----------------FOR DATASOURCES--------------------------

    @RequestMapping(value = "/stats/datasource/getClicks")
    public UsageStats getDatasourceClicks(@RequestParam(value = "datasourceId") String datasourceId) throws Exception {

        return datasourceRepo.getClicks(datasourceId);
    }


    @RequestMapping(value = "/stats/datasource/getPopular")
    public List<UsageStats> getDatasourceByPopularity() throws Exception {
        return datasourceRepo.getMostPopular();
    }


    @RequestMapping(value = "/stats/datasource/getPubs")
    public List<UsageStats> getDatasourcePubsByPopularity(@RequestParam(value = "datasourceId") String datasourceId) throws Exception {
        return datasourceRepo.getMostPopularPubs(datasourceId);
    }

    @RequestMapping(value = "/stats/datasource/getProjects")
    public List<UsageStats> getDatasourceProjectsByPopularity(@RequestParam(value = "datasourceId") String datasourceId) throws Exception {
        return datasourceRepo.getMostPopularProjects(datasourceId);
    }

    @RequestMapping(value = "/stats/datasource/getPopularity")
    public List<UsageStats> getDatasourcePopularity(@RequestParam(value = "datasourceId") String datasourceId) throws Exception {
        return datasourceRepo.getPopularityOverTime(datasourceId);
    }


    //-----------------FOR PROJECTS--------------------------

    @RequestMapping(value = "/stats/project/getClicks")
    public UsageStats getProjectClicks(@RequestParam(value = "projectId") String projectId) throws Exception {

        return projectRepo.getClicks(projectId);
    }


    @RequestMapping(value = "/stats/project/getPopular")
    public List<UsageStats> getProjectByPopularity() throws Exception {
        return projectRepo.getMostPopular();
    }


    @RequestMapping(value = "/stats/project/getPubs")
    public List<UsageStats> getProjectPubsByPopularity(@RequestParam(value = "projectId") String projectId) throws Exception {
        return projectRepo.getMostPopularPubs(projectId);
    }



    @RequestMapping(value = "/stats/project/getPopularity")
    public List<UsageStats> getProjectPopularity(@RequestParam(value = "projectId") String projectId) throws Exception {
        return projectRepo.getPopularityOverTime(projectId);
    }



    //FOR ORGANIZATIONS


    @RequestMapping(value = "/stats/organization/getClicks")
    public UsageStats getOrganizationClicks(@RequestParam(value = "organizationId") String organizationId) throws Exception {

        return organizationRepo.getClicks(organizationId);
    }


    @RequestMapping(value = "/stats/organization/getPopular")
    public List<UsageStats> getOrganizationByPopularity() throws Exception {
        return organizationRepo.getMostPopular();
    }


    @RequestMapping(value = "/stats/organization/getProjects")
    public List<UsageStats> getOrganizationProjectsByPopularity(@RequestParam(value = "organizationId") String organizationId) throws Exception {
        return organizationRepo.getMostPopularProjects(organizationId);
    }



    @RequestMapping(value = "/stats/organization/getPopularity")
    public List<UsageStats> getOrganizationPopularity(@RequestParam(value = "organizationId") String organizationId) throws Exception {
        return projectRepo.getPopularityOverTime(organizationId);
    }




    // FOR RESULTS

    @RequestMapping(value = "/stats/result/getClicks")
    public UsageStats getResultClicks(@RequestParam(value = "resultId") String resultId) throws Exception {

        return resultRepo.getClicks(resultId);
    }


    @RequestMapping(value = "/stats/result/getPopular")
    public List<UsageStats> getResultByPopularity() throws Exception {
        return resultRepo.getMostPopular();
    }


    @RequestMapping(value = "/stats/result/getPopularity")
    public List<UsageStats> getResultPopularity(@RequestParam(value = "resultId") String resultId) throws Exception {
        return resultRepo.getPopularityOverTime(resultId);
    }


}
