package backend.service;

import backend.dao.ChartsDao;
import backend.dao.QuestionsDao;
import backend.dao.UserDao;
import backend.model.Charts;

import java.util.*;

public class ChartsService {
    private ChartsDao chartsDao = new ChartsDao();
    private UserDao userDao = new UserDao();

    public Vector<Vector<String>> commonHandler(List<Charts> chartsList){
        Vector<Vector<String>> chartsData = new Vector<>();
        if(chartsList != null) {
            int i = 1;
            for (Charts chartsPo : chartsList) {
                Vector<String> row = new Vector<>();
                row.add(String.valueOf(i++));
                row.add(String.valueOf(userDao.findById(chartsPo.getUserid()).getUsername()));
                row.add(String.valueOf(chartsPo.getCount()));
                row.add(String.valueOf(chartsPo.getScore()));
                chartsData.add(row);
            }
        }
        return chartsData;
    }
    public Vector<Vector<String>> getAll(){
        List<Charts> chartsList = chartsDao.findAll();
        return commonHandler(chartsList);
    }
    public Vector<Vector<String>> sortByScoreUp(){
        List<Charts> chartsList = chartsDao.sortByScoreUp();
        return commonHandler(chartsList);
    }
    public Vector<Vector<String>> sortByScoreDown(){
        List<Charts> chartsList = chartsDao.sortByScoreDown();
        return commonHandler(chartsList);
    }
    public boolean updateCharts(Charts charts){
        return chartsDao.updateCharts(charts);
    }
    public boolean deleteCharts(int userid){
        return chartsDao.deleteCharts(userid);
    }

    public boolean insertCharts(Charts charts){
        return chartsDao.insertCharts(charts);
    }

}
