/*
 * 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.SESSION;
import cz.csu.sdmx.dao.LogicConditionsDao;
import cz.csu.sdmx.dao.UsersDao;
import cz.csu.sdmx.dao.ValidationResultsDao;
import java.text.ParseException;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author themi
 */
@Controller
public class DebugController {
    
    @Autowired
    LogicConditionsDao logicConditionsDao;
    
    @Autowired
    UsersDao usersDao;
    
    @Autowired
    ValidationResultsDao validationResultsDao;
    
    @RequestMapping(value = "/debug", method = RequestMethod.GET)
//    @Transactional
    public String debugStart(Model model, HttpSession session) throws ParseException {
        session.removeAttribute(SESSION.USER);
        session.removeAttribute(SESSION.DSD);
        
//        if(usersDao.list().isEmpty()){
//            User newUser;
//            
//            newUser = new User();
//            newUser.setPersonalNumber("33");
//            newUser.setAllowedUrns(Arrays.asList("BIS:EXT_DEBT"));
//            newUser.setPermissions(Arrays.asList(Permission.ADMINISTER_USERS));
//            usersDao.insert(newUser);
//            
//            newUser = new User();
//            newUser.setPersonalNumber("11221651231");
//            newUser.setAllowedUrns(Arrays.asList("urn:sdmx:org.sdmx.infomodel.keyfamily.KeyFamily=ESTAT:JVS:2.0:compact","BIS:EXT_DEBT:compact"));
////            newUser.setPermissions(Arrays.asList(Permission.ADMINISTER_CONDITIONS));
//            usersDao.insert(newUser);
//
//            newUser = new User();
//            newUser.setPersonalNumber("Admin0000");
//            newUser.setAllowedUrns(Arrays.asList("*"));
////            newUser.setPermissions(Arrays.asList(Permission.ADMINISTER_CONDITIONS, Permission.ADMINISTER_USERS));
//            newUser.setPermissions(Arrays.asList(Permission.ADMINISTER_USERS));
//            usersDao.insert(newUser);
//            for(int i = 0; i < 4; i++){
//                newUser = new User();
//            newUser.setPersonalNumber("l" + i);
//            newUser.setAllowedUrns(Arrays.asList("*"));
////            newUser.setPermissions(Arrays.asList(Permission.ADMINISTER_CONDITIONS, Permission.ADMINISTER_USERS));
//            newUser.setPermissions(Arrays.asList(Permission.ADMINISTER_USERS));
//            usersDao.insert(newUser);
//            }
//        }
//        
//        if(logicConditionsDao.list().isEmpty()){
//            String dims = "UNIT;UNIT_MULT;TIME_FORMAT;TIME_PER_COLLECT;FREQ;REF_AREA;REGION;SEASONAL_ADJUST;INDICATOR;ACTIVITY;OCCUPATION";
//            List<String> commonDimensions = new ArrayList<>();
//            commonDimensions.addAll(Arrays.asList(dims.split(";")));
//            LogicCondition condition = new LogicCondition();
//            condition.setObsValAttrName("OBS_VALUE");
//            condition.setDsdName("urn:sdmx:org.sdmx.infomodel.keyfamily.KeyFamily=ESTAT:JVS:2.0:compact");
//            condition.setExpression("[SIZECLASS=GE10]<=[SIZECLASS=_T]");
//            condition.setCommonDimensions(commonDimensions);
//            logicConditionsDao.insert(condition);
//            condition = new LogicCondition();
//            condition.setObsValAttrName("OBS_VALUE");
//            condition.setDsdName("urn:sdmx:org.sdmx.infomodel.keyfamily.KeyFamily=ESTAT:JVS:2.0:compact");
//            condition.setExpression("[SIZECLASS=GE10]<=[SIZECLASS=_T]");
//            condition.setCommonDimensions(commonDimensions);
//            logicConditionsDao.insert(condition); 
//            condition = new LogicCondition();
//            condition.setDsdName("testUrn");
//            condition.setObsValAttrName("OBS_VALUE");
//            condition.setExpression("[SIZECLASS=GE10]<=[SIZECLASS=_T]");
//            condition.setCommonDimensions(commonDimensions);
//            logicConditionsDao.insert(condition); 
//        }
//         
//        if(validationResultsDao.list().isEmpty()){
//            for(int i = 0; i < 25; i++){
//            ValidationResult validationResult = new ValidationResult();
//            validationResult.setDsds("DsdUri");
//            validationResult.setDataFilename("Datafilename");
//            ValidationError validationError = new ValidationError();
//            validationError.setFilename("filename");
//            validationError.setLineNumber("111,553");
//            validationError.setText("Text of validation error");
//            validationError.setType(ValidationError.FATAL);
//            validationError.setErrorCode("Test error code");
//
//            validationResult.addError(validationError);
//
//            validationResultsDao.insert(validationResult);
//            }
//        }
//        
        model.addAttribute("springVersion", org.springframework.core.SpringVersion.getVersion() + " version");
//        model.addAttribute("decodIdent", usersDao.decodePersonalNumber("dadsgsdgsdf421"));
        model.addAttribute("decodIdent", usersDao.decodePersonalNumber("44"));
//         condition = new LogicCondition();
//         condition.setDsdName("2426");
//         condition.setExpressionsString("241++5426");
//         condition.setCommonDimensions(commonDimensions);
//         logicConditionsDao.insert(condition);
         
         
//         LogicCondition condition27 = logicConditionsDao.find(27);
//         
//         System.out.println("++++Id" + condition27.getId());
         
//         System.out.println("++++++++++++Insert");
//         List<User> users = usersDao.getLike("A");
//         for(User user : users){
//             System.out.println("---" + user.getPersonalNumber());
//         }
////        entityManager.persist(newUser);
////         usersDao.
//         System.out.println("++++++++++++ID" + newUser.getPersonalNumber());
//         
//         List<String> dsds = logicConditionsDao.getDSDs();
//         for(String dsd : dsds){
//             System.out.println("++++dsd+++" + dsd);
//         }
//         
//         List<LogicCondition> conditions = logicConditionsDao.getByDSDUri("2426");
//         for(LogicCondition c : conditions){
//             System.out.println("++++condition+++" + Utils.join(c.getCommonDimensions(), ","));
//         }
         
        return "startDebug";
    }
    
    
}
