/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package teacher;

import entities.Indexbook;
import entities.Semester;
import entities.Users;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import manager.IndexbookSessionBeanRemote;

/**
 *
 * @author Szilárd
 */
@ManagedBean(name="ListStudentT")
@RequestScoped
public class ListStudentT {
    @EJB
    private IndexbookSessionBeanRemote indexbookSessionBean;

    int itemsPerPage = 15;
    int currentPage;
    HtmlDataTable tableComponent;
    String pagingDirection;
    String sortDirection;
    String from, to;
    int indexSize;
    List<Indexbook> indexB;
    Boolean[] sign,signH, listBoole;
    Integer[] markk, markkH, listMark;
    ArrayList lista;
    ArrayList lista2;
    boolean mark;
    Semester semester;
    Users user;
    String subject;
    /** Creates a new instance of ListStudentT */
    public ListStudentT() {
        Map map = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        indexB = (List<Indexbook>) map.get("index");
        subject = indexB.get(0).getSubjectId().getName();
        //indexSize = (Integer)map.get("indexsize");
        semester = (Semester) map.get("semester");
        user = (Users) map.get("felhasznalo");
        mark = (Boolean)map.get("mark");

        lista2 = new ArrayList();
        //listMark = new Integer[6];
        lista2.add(new SelectItem(0, "-"));
        lista2.add(new SelectItem(1, "elégtelen (1)"));
        lista2.add(new SelectItem(2, "elégséges (2)"));
        lista2.add(new SelectItem(3, "közepes (3)"));
        lista2.add(new SelectItem(4, "jó (4)"));
        lista2.add(new SelectItem(5, "jeles (5)"));


        listBoole = new Boolean[2];
        listBoole[0] = false;
        listBoole[1] = true;
        lista = new ArrayList();
        lista.add(new SelectItem(true, "Aláír"));
        lista.add(new SelectItem(false, "-"));

        markk = new Integer[indexB.size()];
        markkH = new Integer[indexB.size()];

        sign = new Boolean[indexB.size()];
        signH = new Boolean[indexB.size()];
        for(int i=0;i<indexB.size();i++){
            if(indexB.get(i).getSignature() == 1){
                sign[i] = true;
                signH[i] = true;
            }
            else{
                sign[i] = false;
                signH[i] = false;
            }

            markk[i] = indexB.get(i).getMark();
            markkH[i] = indexB.get(i).getMark();
        }



    }

    public List<Indexbook> getIndexB() {
        int fromIndex = currentPage*itemsPerPage;
        int toIndex = fromIndex+itemsPerPage;

        if(toIndex>=indexB.size())
            toIndex=indexB.size();
        return indexB.subList(fromIndex, toIndex);
    }

    public void setIndexB(List<Indexbook> indexB) {
        this.indexB = indexB;
    }

    public boolean isMark() {
        return mark;
    }

    public void setMark(boolean mark) {
        this.mark = mark;
    }

    public Boolean[] getSign() {
        return sign;
    }

    public void setSign(Boolean[] sign) {
        this.sign = sign;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    protected int calculatePageCount(){
        return (indexB.size()/itemsPerPage)+((indexB.size()%itemsPerPage) != 0 ? 1 : 0);
    }

    public int getPageCount(){
        return calculatePageCount();
    }

    public String getPagingDirection() {
        return pagingDirection;
    }

    public void setPagingDirection(String pagingDirection) {
        this.pagingDirection = pagingDirection;
    }

    public String page(){
        if(pagingDirection.equals("<") )//&& currentPage!=0)
            currentPage--;
        else if (pagingDirection.equals(">") )//&& currentPage+1<calculatePageCount())
            currentPage++;
        return null;
    }

    public String prevPage(){
        --currentPage;
        return null;
    }

    public String nextPage(){
        ++currentPage;
        return null;
    }

    public HtmlDataTable getTableComponent() {
        return tableComponent;
    }

    public void setTableComponent(HtmlDataTable tableComponent) {
        this.tableComponent = tableComponent;
    }

    public String backAction(){
        return "listkurzust";
    }

    public Boolean[] getListBoole() {
        return listBoole;
    }

    public void setListBoole(Boolean[] listBoole) {
        this.listBoole = listBoole;
    }

    public ArrayList getLista() {
        return lista;
    }

    public void setLista(ArrayList lista) {
        this.lista = lista;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public Integer[] getMarkk() {
        return markk;
    }

    public void setMarkk(Integer[] markk) {
        this.markk = markk;
    }

    public ArrayList getLista2() {
        return lista2;
    }

    public void setLista2(ArrayList lista2) {
        this.lista2 = lista2;
    }

    

    public String saveAction(){
        for(int i=0; i<sign.length;i++){
            if(!sign[i].equals(signH[i])){
                Indexbook id = this.indexbookSessionBean.findBySemesterAndUserANDSubjectId(indexB.get(i).getStudentId(), semester, indexB.get(i).getSubjectId());
                    if(sign[i])
                        this.indexbookSessionBean.updateSign(user.getName(), 1, id.getId());
                    else
                        this.indexbookSessionBean.updateSign(user.getName(), 0, id.getId());
            }
        }
        return null;
    }

    public String save2Action(){
        for(int i=0; i<markk.length;i++){
            if(!markk[i].equals(markkH[i])){
                Indexbook id = this.indexbookSessionBean.findBySemesterAndUserANDSubjectId(indexB.get(i).getStudentId(), semester, indexB.get(i).getSubjectId());
                        this.indexbookSessionBean.updateMark(user.getName(), markk[i], id.getId());
            }
        }
        return null;
    }
    
}
