'use strict'

kendo.culture("pt-PT");
kendo.culture().calendar.firstDay = 1;

app.factory('kendoSchedulerFactory', ['$http',

    function ($http) {

        return {
            getGUIOptions: function (data) {

                var CustomAgenda = kendo.ui.AgendaView.extend({
                    nextDate: function () {
                        var startDate = this.startDate();
                        var novaData = new Date(startDate.setMonth(startDate.getMonth() + 1));

                        return novaData;
                    },
                    previousDate: function () {
                        var endDate = this.endDate();

                        var newMonth = endDate.getMonth() - 1;

                        var novaData = new Date(endDate.getFullYear(), newMonth, 0); //new Date(endDate.setMonth(newMonth));

                        return novaData;
                    },
                    startDate: function () {
                        var date = kendo.ui.AgendaView.fn.endDate.call(this);

                        var novaxx = new Date(date.getFullYear(), date.getMonth(), 1);

                        return novaxx;
                    },
                    endDate: function () {
                        var date = kendo.ui.AgendaView.fn.endDate.call(this);

                        var novaxx = new Date(date.getFullYear(), date.getMonth() + 1, 0);

                        return novaxx;
                    }
                });

                var work_day_start = new Date();
                var work_day_end = new Date();

                work_day_start.setHours(8, 0);
                work_day_end.setHours(18, 0);

                return {
                    date: new Date(),
                    views: [
                        {
                            type: "day",
                            eventTemplate: kendo.template('<div style="width: 100%; height: 100%;" class="movie-template"><div style="font-size: 12px;" class="k-task" data-uid="#:uid#" title="#=kendo.toString(start, " HH:mm")# às #=kendo.toString(end, "HH:mm" )#">#=kendo.toString(start, "HH:mm")# - #=kendo.toString(end, "HH:mm")#<br />#:title#</div></div>'),
                        },
                        {
                            type: "week",
                            eventTemplate: kendo.template('<div class="movie-template"><div style="font-size: 12px;" class="k-task" data-uid="#:uid#" title="#=kendo.toString(start, " HH:mm")# às #=kendo.toString(end, "HH:mm" )# - #:title#">#=kendo.toString(start, "HH:mm")# - #=kendo.toString(end, "HH:mm")#<br />#:title#</div>')
                        },
                        {
                            selected: true,
                            type: "month",
                            eventTemplate: kendo.template('<div class="movie-template"><div style="font-size: 10px; cursor: pointer; cursor: hand;" class="k-task" data-uid="#:uid#" title="#=kendo.toString(start, " HH:mm")# às #=kendo.toString(end, "HH:mm" )# #:title#">#:title#</div>'),
                        },
                        {
                            title: "Agenda",
                            //start: firstDay,
                            //end: lastDay,
                            type: CustomAgenda,
                            eventTemplate: kendo.template('<div class="movie-template"><img style="height: 100%; width:100%; max-width:198px; max-height:125px; float:left; margin-right:10px;" src="data:image/png;base64, #= image #"><div class="k-task" data-uid="#:uid#" title="#=kendo.toString(start, " HH:mm")# às #=kendo.toString(end, "HH:mm" )#">#:title#</div>'),
                            //eventTemplate: '<div class="k-task" data-uid="#:uid#"><span class="k-scheduler-mark" style="background-color:#=resources[0].color#;"></span>#:title#</div>'
                        }],
                    dateHeaderTemplate: kendo.template('<strong>#=kendo.toString(date, "ddd d/M")#</strong>'),
                    editable: false,
                    workDay: false,
                    showWorkHours: true,
                    allDaySlot: false,
                    workWeekStart: 1, //Monday
                    workWeekEnd: 5, //Friday
                    workDayStart: work_day_start,
                    workDayEnd: work_day_end,
                    timezone: "Etc/UTC",
                    selectable: false,
                    //eventTemplate: kendo.template('<div class="k-task" data-uid="#:uid#" title="#=kendo.toString(start, "HH:mm")# às #=kendo.toString(end, "HH:mm")#">#=kendo.toString(start, "HH:mm")# - #=kendo.toString(end, "HH:mm")#<br />#:title#</div>'),
                    eventTemplate: kendo.template('<div class="movie-template"><div class="k-task" data-uid="#:uid#" title="#=kendo.toString(start, " HH:mm")# às #=kendo.toString(end, "HH:mm" )#">#=kendo.toString(start, "HH:mm")# - #=kendo.toString(end, "HH:mm")#<br />#:title#</div>'),
                    dataSource: {
                        data: data,
                        schema: {
                            model: {
                                fields: {
                                    title: { from: "Titulo" },
                                    info: { from: "Informacao" },
                                    start: { type: "date", from: "Inicio" },
                                    end: { type: "date", from: "Termo" },
                                    image: { from: "Image" }
                                }
                            }
                        }
                    },
                    dataBound: function (e) {
                        var scheduler = e.sender;

                        if (scheduler.view().options.type === "kendo.ui.AgendaView") {
                            var schedulerHeader = scheduler.element.find(".k-scheduler-header table tbody tr:eq(0)"),
                                datas = scheduler.dataSource.data();

                            //schedulerHeader.append("<th>Informação</th>");

                            //datas.forEach(function (item) {
                            //    $(".k-task[data-uid='" + item.uid + "']").parent()
                            //    .after("<td><pre class='pre-reset'>" + item.info + "</pre></td>");
                            //});
                        }
                    },
                    navigate: function (e) {
                        //mySessionFactory.setScheduleDate(e.date);
                    }
                };
            }
        };
    }
]);