class Calendar extends Directive('hn.enterprise')
    constructor: (ProjectUserAvailability) ->
        return {
            scope: {
                # project object
                project: '=',

                # moment timezone object
                timezone: '=' # if this is undefined we use the users timezone
            }
            bindToController: yes
            restrict: 'E'
            require: "?ngModel"
            controllerAs: 'vm'
            templateUrl: "/modules/enterprise/components/calendar/calendar.html"
            link: (scope, element, attrs, ngModel) ->
                return if not ngModel

                start_hour = 8
                hour_span = 10
                day_span = 7

                times = [] # times are stored as unix time (ms), start of 30 minute block
                time_ids = [] # ids of each time stored in the server

                scope.$watch('timezone', (timezone) ->
                    # update all to match new timezone
                    if not timezone
                        return

                    # update the values on the client
                    offset = moment.tz(timezone.name).utcOffset()
                    _.map(times, (t) ->
                        return moment(t).utcOffset(offset)
                    )

                    # update on the server
                    _.each(times, (time, index) ->
                        time_id = time_ids[index]
                        ProjectUserAvailability.updateOne({
                            project_id: scope.project.id
                            id: time_id
                        }, {
                            start: moment(time).utc().format()
                            end: moment(time + moment.duration(30, 'minutes').valueOf()).utc().format()
                        })
                    )

                )

                # gets current or specific time in timezone.
                getTime = (time) ->
                    return moment(time)

                # update ng-model
                update = () ->
                    ngModel.$setViewValue(times)

                addTime = (time) ->
                    end = getTime(time).add(30, 'minutes').utc().format()
                    start = time.utc().format()

                    ProjectUserAvailability.createOne({
                        project_id: scope.project.id
                    }, {
                        start: start,
                        end: end
                    }).$promise.then((tt) ->
                        time_ids.push(tt.id)
                        times.push(+time)
                        update()
                    )

                removeTime = (time) ->
                    idx = times.indexOf(+time)
                    id = time_ids[idx]

                    ProjectUserAvailability.deleteOne({
                        project_id: scope.project.id
                    }, {
                        id: id
                    }).$promise.then((t) ->
                        times = _.without(times, +time)
                        time_ids = _.without(time_ids, id)
                        update()
                    )


                scope.getDateString = (dateDiff) ->
                    day = moment().add(dateDiff, 'day')
                    return day.format('ddd') + ' ' + day.format('DD')

                scope.getHourString = (timeDiff) ->
                    return getTime().hour(start_hour).add(timeDiff, 'hours').format('h A')

                scope.getTime = (day, hour, minutes) ->
                    return getTime()
                        .hour(start_hour)
                        .minutes(0)
                        .seconds(0)
                        .milliseconds(0)
                        .add(day, 'days')
                        .add(hour, 'hours')
                        .add(minutes, 'minutes')

                scope.canSelectTime = (day, hour, minutes=0) ->
                    time = scope.getTime(day, hour, minutes)
                    if time.isBefore(getTime().add(1, 'day'))
                        return no
                    return yes

                scope.selectTime = (days, hours, minutes=0) ->
                    if scope.canSelectTime(days, hours, minutes)
                        time = scope.getTime(days, hours, minutes)
                        if scope.isSelected(days, hours, minutes)
                            removeTime(time)
                        else
                            addTime(time)

                scope.isSelected = (days, hours, minutes=0) ->
                    thisTime = scope.getTime(days, hours, minutes)
                    return _.indexOf(times, +thisTime.toDate()) > -1

                scope.getFullString = (day, hour, minutes=0) ->
                    time = scope.getTime(day, hour, minutes)
                    if time.isBefore(getTime().add(1, 'day'))
                        return "not available";
                    return time.format('ddd DD. h:mm A')

                scope.days = []
                count = 0
                while scope.days.length != day_span
                    day = count + 1
                    dayObj = parseInt(moment().add(day, 'days').format('d'), 10)
                    if dayObj == 6
                        count += 2
                    else if dayObj == 0
                        count++
                    else
                        scope.days.push(day)
                        count++

                scope.hours = [0..(hour_span-1)] # from 9am to 5pm (not including)

        }
