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

package salaselo.util;

import java.util.Date;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import salaselo.entities.Asignacion;
import salaselo.entities.Sala;

/**
 *
 * @author maldoror
 */
public class CalendarioUtil {
    public static Date[] getBordesSemana(Date dia)
    {
        Date dia_inicio,dia_final;
        switch(dia.getDay())
        {
            case 1:
                dia_inicio = new Date(dia.getYear(),dia.getMonth(),dia.getDate());
                dia_final = new Date(dia.getYear(),dia.getMonth(),dia.getDate()+6);
                break;
            case 2:
                dia_inicio = new Date(dia.getYear(),dia.getMonth(),dia.getDate()-1);
                dia_final = new Date(dia.getYear(),dia.getMonth(),dia.getDate()+5);
                break;
            case 3:
                dia_inicio = new Date(dia.getYear(),dia.getMonth(),dia.getDate()-2);
                dia_final = new Date(dia.getYear(),dia.getMonth(),dia.getDate()+4);
                break;
            case 4:
                dia_inicio = new Date(dia.getYear(),dia.getMonth(),dia.getDate()-3);
                dia_final = new Date(dia.getYear(),dia.getMonth(),dia.getDate()+3);
                break;
            case 5:
                dia_inicio = new Date(dia.getYear(),dia.getMonth(),dia.getDate()-4);
                dia_final = new Date(dia.getYear(),dia.getMonth(),dia.getDate()+2);
                break;
            case 6:
                dia_inicio = new Date(dia.getYear(),dia.getMonth(),dia.getDate()-5);
                dia_final = new Date(dia.getYear(),dia.getMonth(),dia.getDate()+1);
                break;
            case 0:
                dia_inicio = new Date(dia.getYear(),dia.getMonth(),dia.getDate()-6);
                dia_final = new Date(dia.getYear(),dia.getMonth(),dia.getDate());
                break;
            default:
                dia_inicio = null;
                dia_final = null;
        }
        Date[] ret = new Date[2];
        ret[0] = dia_inicio;
        ret[1] = dia_final;
        return ret;
    }

    public static String[][] makeHorariosArray(Date[] bordes,Sala sala)
    {
        String[][] ret = new String[7][7];
        for(int i = 0; i < 7; i++)
        {
            for(int j = 0; j < 7; j++)
            {
                ret[i][j] = new String("Disponible");
            }
        }
        try{
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            Transaction tx = session.beginTransaction();
            String stringIDate = (bordes[0].getYear()+1900) + "-"
                    + (bordes[0].getMonth()+1) + "-" + bordes[0].getDate();
            String stringFDate = (bordes[1].getYear()+1900) + "-"
                    + (bordes[1].getMonth()+1) + "-" + bordes[1].getDate();
            System.out.println(stringIDate + " " + stringFDate);
            String hQuery ="from Asignacion where dia>=DATE('" +
                    stringIDate + "') and dia <=DATE('" + stringFDate + "') and sala.salaID=" + sala.getSalaID();
            System.out.println(hQuery);
            List<Asignacion> asi = session.createQuery(hQuery ).list();
            System.out.println(asi.size());
            
            if(asi.isEmpty()){
                return ret;
            }
            System.out.println(asi.size());
            for(int i = 0; i < asi.size(); i++)
            {
                Asignacion curr = asi.get(i);
                int b = curr.getBloqueIndex();
                ret[b][(curr.getDia().getDay()+6)%7]=new String(curr.getUsuario().getNombre());
            }
            session.close();
            return ret;
        } catch(Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static int getDiaIndex(String ds)
    {
        String diaSemana = ds.toUpperCase();
        if(diaSemana.equals("LUNES")) return 0;
        if(diaSemana.equals("MARTES")) return 1;
        if(diaSemana.equals("MIERCOLES") || diaSemana.equals("MIÉRCOLES")) return 2;
        if(diaSemana.equals("JUEVES")) return 3;
        if(diaSemana.equals("VIERNES")) return 4;
        if(diaSemana.equals("SÁBADO") || diaSemana.equals("SABADO")) return 5;
        if(diaSemana.equals("DOMINGO")) return 6;
        return -1;
    }

    public static String dateToString(Date date)
    {
        if(date==null) return null;
        String ret = (date.getYear()+1900) + "-" + (date.getMonth()+1) + "-" + date.getDate();
        return ret;
    }
}
