package net.methodyne.booking;

import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;

/**
 * Created by Methodyne GmbH.
 * User: yuri
 * Date: 19.05.2003
 * Time: 11:16:17
 *
 */
public class CalendarRenderer {

    //String daynames[] = {"M", "T", "W", "T", "F", "S", "S"};
    String daynames[] = {"Mo", "Di", "Mi", "Do", "Fr", "Sa", "So"};
	public Date date;
	public int firstWeek;
	Calendar today;

    public CalendarRenderer(Date date)
    {
        this.date = date;
        today = Calendar.getInstance();
        today.setTime(date);
        //this.setSize(100,100);
    }

    public void setDate(Date date)
    {
        this.date = date;
        today = Calendar.getInstance();
        today.setTime(date);
    }

    public String render(boolean[] bold )
    {
        int days[] = new int[42];
        int todaycnt = -1;
        // how to get the day numbers in the right position
        // get a date
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // get the dates month
        //int month = cal.get(Calendar.MONTH);
        // get weekday number of first day in month = x
        cal.set(Calendar.DAY_OF_MONTH, 1);
        // to get the daynumber for the upper left corner of the month view
        int firstDay = cal.get(Calendar.DAY_OF_WEEK);
        int offset = 0;
        switch (firstDay)
        {
            case 1: // sunday
                offset = 6;
                break;
            case 2: // mon
                offset = 0;
                break;
            case 3: // tue
                offset = 1;
                break;
            case 4: // wed
                offset = 2;
                break;
            case 5: // thu
                offset = 3;
                break;
            case 6: // fri
                offset = 4;
                break;
            case 7: // sat
                offset = 5;
                break;
        }
        firstWeek = cal.get(Calendar.WEEK_OF_YEAR);
        // System.out.println(" fw" + firstWeek);
        // System.out.println("fd" + firstDay);
        // set calendar -=x days
        cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) - offset);
        // fill daymatrix [5][7] with day of month numbers
        // System.out.println("dom" + cal.get(Calendar.DAY_OF_MONTH));
        Calendar now = Calendar.getInstance();
        now.setTime(new Date());
        for (int i = 0; i < 42; i++)
        {
            days[i] = cal.get(Calendar.DAY_OF_MONTH);
            if (cal.get(Calendar.DAY_OF_YEAR) == now.get(Calendar.DAY_OF_YEAR) ) todaycnt = i;
            cal.add(Calendar.DAY_OF_MONTH, 1);
            // System.out.println(" " + cal.get(Calendar.DAY_OF_MONTH) + 1);
        }

        String White = "\"#FFFFFF\"";
        //String Grey = "\"#C0C0C0\"";
        String Red = "\"#FF0000\"";
        String BGColor = White;
        String boldStart = "";
        String boldEnd = "";

        String ret = "<TABLE>";

        ret += "<TR bgcolor=\"#FFCCCC\">";
        //DateFormat dfm = DateFormat.getDateInstance();
        SimpleDateFormat simpleFormatter = new SimpleDateFormat("MMMM yyyy");

        ret += "<td><a href=?prevMonth><b>&lt;&lt;</b></a></td><td colspan=5><b>" + simpleFormatter.format(date)  + "</b></td><td><a href=?nextMonth><b>&gt;&gt;</b></a></b></TD></b>";
        ret += "</TR>";

        ret += "<TR bgcolor=\"#FFCCCC\">";
        for (int i = 0; i < 7; i++)
        {
            ret += "<td><b>" + daynames[i] + "</b></TD>";
        }
        ret += "</TR>";

        for (int i = 0; i < 6; i++)
        {
            ret += "<TR bgcolor=" + White + ">";
            for (int j = 0; j < 7; j++)
            {
                BGColor = White;
                if (  ((i == 0) && (days[i * 7 + j] > 10)) || ((i >= 4) && (days[i * 7 + j] < 15)) ){
                    // BGColor = Grey;  // here we are in the previous or next month
                    //boldStart = "";
                    //boldEnd = "";
                }
                else{
                    if ((todaycnt == i * 7 + j) )
                        BGColor = Red;

                    if( bold[ days[i * 7 + j] ] ){ // here is only a number { 1 ... 31 } from this month
                        boldStart = "<b>";
                        boldEnd = "</b>";
                    }
                    else{
                        boldStart = "";
                        boldEnd = "";
                    }
                }
                ret += "<td bgcolor=" + BGColor + ">";
                if( ( ! ((i == 0) && (days[i * 7 + j] > 10)) ) && ( ! ((i >= 4) && (days[i * 7 + j] < 15)) ) ){
                    ret += boldStart + "<a href=\"?book=" + days[i * 7 + j] + "\">"  + days[i * 7 + j] + "</a>" + boldEnd;
                }
                else{ // blank out days not in this month - otherwise we have to support bold for more that 31 days...
                    //ret += days[i * 7 + j] ;
                }
                ret += "</td>";
            }
            ret += "</TR>";
        }
        ret += "</TABLE>";
        return ret;
    }


}