package tw.org.joyce929.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.datepicker.client.CalendarUtil;
import com.smartgwt.client.types.TimeUnit;
import com.smartgwt.client.util.DateUtil;
import com.smartgwt.client.widgets.calendar.*;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.layout.VLayout;

import java.util.Date;

public class MainEntryPoint implements EntryPoint
{
    DateTimeFormat sortFormat= DateTimeFormat.getFormat("MM/dd");
    public Timeline calendar;

    public void onModuleLoad()
    {
        VLayout layout = new VLayout();
        layout.setWidth100();
        layout.setHeight100();
        layout.addMember(getTimeline());

        layout.draw();
    }

    public Timeline getTimeline ()
    {
        Date startDate = new Date();
        startDate.setYear(121);
        startDate.setMonth(2);
        startDate.setDate(21);
        startDate.setHours(0);
        startDate.setMinutes(0);
        startDate.setSeconds(0);

        Date endDate = CalendarUtil.copyDate(startDate);
        CalendarUtil.addDaysToDate(endDate,27);
        endDate.setHours(23);
        endDate.setMinutes(59);
        endDate.setSeconds(59);

//        DateUtil.setDefaultDisplayTimezone("08:00");  //my Timezone is +8
        GWT.log(startDate+"--------------"+endDate);

        calendar = new Timeline();
        calendar.setHeight(451);
        calendar.setStartDate(startDate);    // firstDay of week is sunday so setting startDate in 2021/03/21 00:00:00
        calendar.setEndDate(endDate);        // firstDay of week is sunday so setting endDate in 2021/04/17 23:59:59
        calendar.setCanEditLane(true);
        calendar.setShowEventDescriptions(false);
        calendar.setEventSnapGap(180);
        calendar.setLaneEventPadding(2);
        calendar.setEventAutoArrange(true);
        ///////////////////////////////////////////////////////////////////////////////
        calendar.setFirstDayOfWeek(0);      // set firstDayOfWeek in sunday , if not setting this , it display current
        //////////////////////////////////////////////////////////////////////////////
        calendar.setLanes(TimelineLaneData.getRecords());
        calendar.setLaneFields(new ListGridField[]{ new ListGridField("title", "Developer", 120)});
        calendar.setData(TimelineData.getRecords());


        HeaderLevel hl = new HeaderLevel(TimeUnit.WEEK) ;
        hl.setTitleFormatter(new HeaderLevelTitleCustomizer()
        {
            @Override
            public String getTitle(HeaderLevel headerLevel, Date startDate, Date endDate, String defaultValue, Calendar calendar)
            {
                Date test = CalendarUtil.copyDate(startDate);
                CalendarUtil.addDaysToDate(test,6);

                Date yearStart = new Date(startDate.getYear(), 0, 0);
                double week = (startDate.getTime() - yearStart.getTime())/(double)(7 * 24 * 60 * 60 * 1000);
                int num = (int)Math.ceil(week)+1 ;
                if(num>52)
                    num = 1 ;

//                GWT.log(startDate+"----------header-----------"+ endDate);
//                                                                                                Date endDate   not working (return null)
                return "<span style='color:blue;font-size:110%;'>"+num+" week："+sortFormat.format(startDate)+"～"+sortFormat.format(test)+"</span>";
            }
        });
        //  unitCount set 4 , display 4 week  header ? , but when set setFirstDayOfWeek =0  in 2021/03/21 to 2021/04/17 ,
        //  it displays 6 week header ( in different smartgwt builds have different display count ; builds in 2021-04-20 with builds in 2021-04-26 display seem different )
        calendar.setResolution(new HeaderLevel[] { new HeaderLevel(TimeUnit.MONTH), hl }, TimeUnit.WEEK, 4, null);
        // set TimeUnit.Day, unitCount 28 , granularityPerColumn 7  display 5 week header
        calendar.getHeaderLevels()[1].setHeaderWidth(150);


        ////////////////////////////////////////////////////////////////////////
/*
        * have the same result
        Date test1 = CalendarUtil.copyDate(startDate);
        Date test2 = CalendarUtil.copyDate(endDate);

        CalendarUtil.addDaysToDate(test1, 0);
        CalendarUtil.addDaysToDate(test2,0);

        GWT.log(test1 +"------RangeDate--------"+test2);

        calendar.setTimelineRange(test1,test2);
*/

        return calendar;
    }
}

class TimelineData
{
    private static CalendarEvent[] records;
    private static Date today = new Date(121, 2, 21);
    private static int year = today.getYear();
    private static int month = today.getMonth();
    private static int start = today.getDate();

    public TimelineData() {	}
    public static CalendarEvent[] getRecords()
    {
        if (records == null)
            records = getNewRecords();

        return records;
    }

    public static CalendarEvent[] getNewRecords()
    {
        return new CalendarEvent[]
                {
                        new CalendarEvent(4, "Calculated Fields", "Formula and Summary", new Date(year, month, start), new Date(year, month, start + 6, 23, 59, 59), "charlesMadigen"),
                        new CalendarEvent(5, "ListGrid", "in ListGrid", new Date(year, month, start + 7), new Date(year, month, start + 13, 23, 59, 59), "charlesMadigen"),
                        new CalendarEvent(6, "Text import", "Server text-import", new Date(year, month, start + 21), new Date(year, month, start + 27, 23, 59, 59), "charlesMadigen"),

                        new CalendarEvent(9, "DataSource", "New ", new Date(year, month, start), new Date(year, month, start + 6, 23, 59, 59), "tamaraKane"),
                        new CalendarEvent(10, "New Samples", "Add...", new Date(year, month, start + 14), new Date(year, month, start + 20, 23, 59, 59), "tamaraKane"),

                };
    }
}

class TimelineLaneData
{
    private static Lane[] records;

    public static Lane[] getRecords()
    {
        if (records == null)
            records = getNewRecords();

        return records;
    }

    public TimelineLaneData() {	}

    public static Lane[] getNewRecords()
    {
        Lane[] lanes = new Lane[]
                {
                        getLane("charlesMadigen", "Charles Madigen", "Managers"),
                        getLane("tamaraKane", "Tamara Kane", "Developers"),
                };
        return lanes;
    }

    private static Lane getLane(String name, String title, String devGroup)
    {
        Lane lane = new Lane(name, title);
        lane.setAttribute("devGroup", devGroup);
        return lane;
    }
}