/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.csu.sdmx.web.controllers;

import cz.csu.sdmx.constants.JSP;
import cz.csu.sdmx.dao.ValidationResultsDao;
import cz.csu.sdmx.constants.URL;
import cz.csu.sdmx.dao.LogicConditionsDao;
import cz.csu.sdmx.validation.model.LogicCondition;
import cz.csu.sdmx.validation.model.ValidationResult;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 *
 * @author themi
 */
@Controller
public class ResultController extends BaseController{
    
    public static final int RESULT_ON_PAGE = 10;
    
    @Autowired
    private ValidationResultsDao resultsDao;
    
    @Autowired
    private LogicConditionsDao conditionsDao;
    
    @RequestMapping(value = URL.RESULTS_LIST, method = RequestMethod.GET)
    public String showResultList(Model model) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        Date today = new Date();
        String todayS = formatter.format(today);
        return filterResults(todayS, todayS, model);
    }

    @RequestMapping(value = URL.RESULTS_FILTER, method = RequestMethod.POST)
    public String filterResults(@RequestParam String from, @RequestParam String to, Model model) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
        Date fromD = formatter.parse(from + " 00:00:00");
        Date toD = formatter.parse(to + " 23:59:59");   
        
        //get results in range of dates
        List<ValidationResult> results = resultsDao.getBetween(fromD, toD);
        //calculate number of pages
        int resultCount = results.size();
        int pageCount = resultCount / RESULT_ON_PAGE;
        if(resultCount % RESULT_ON_PAGE != 0)
            pageCount++;
        
        model.addAttribute("pageCount", pageCount);
        model.addAttribute("validationResults", results);
        model.addAttribute("from", from);
        model.addAttribute("to", to);

        return JSP.VALIDATION_RESULTS;
    } 

    @RequestMapping(value = URL.RESULT+"/{id}", method = RequestMethod.GET)
    public String showResult(@PathVariable int id, Model model) {
        ValidationResult result =  resultsDao.find(id);
        
        model.addAttribute("validationResult", resultsDao.find(id));
        if(result != null){
            List<LogicCondition> lcList = new ArrayList<>();
            for(String dsd : result.getDsdList())
                lcList.addAll(conditionsDao.getByDsdUrn(dsd));
            
            model.addAttribute("conditions", lcList);
        }
        
        return JSP.VALIDATION_RESULT_STANDALONE;
    }    
}
