import * as tslib_1 from "tslib";
import { Component, Input, Output, ViewEncapsulation, ChangeDetectionStrategy, forwardRef } from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
var ScheduleComponent = /** @class */ (function () {
    function ScheduleComponent() {
        /**
         * @docs-private
         */
        this.flag = false;
        /**
         * @docs-private
         */
        this.week = ['一', '二', '三', '四', '五', '六', '日'];
        /**
         * @docs-private
         */
        this.hours = [];
        /**
         * @docs-private
         */
        this.selectData = [
            {
                label: '全周',
                value: 7
            },
            {
                label: '工作日',
                value: 5
            },
            {
                label: '周末',
                value: 2
            }
        ];
        this.schedules = Array(169).fill(0);
        this.weekSelect = Array(7).fill(0);
        this.layerTime = Array(168).fill(0);
        for (var n = 0; n < 25; n++) {
            this.hours[n] = n;
        }
    }
    ScheduleComponent_1 = ScheduleComponent;
    ScheduleComponent.prototype.ngOnInit = function () {
        for (var i in this.selected) {
            if (this.selected.hasOwnProperty(i)) {
                for (var j = 0; j < this.selected[i].length; j++) {
                    for (var k = this.selected[i][j][0]; k <= this.selected[i][j][1]; k++) {
                        this.schedules[24 * parseInt(i) + k] = 1;
                        if (k === 23) {
                            this.weekSelect[i] = 2;
                        }
                        else if (k > 0 && k < 23) {
                            this.weekSelect[i] = 1;
                        }
                    }
                }
            }
        }
        this.topTimeChange();
    };
    /**
     * set day
     *
     * @param {object} event - page change event
     * @docs-private
     */
    ScheduleComponent.prototype.setDay = function (event) {
        var day = event.value;
        switch (day) {
            case 7:
                this.schedules = Array(169).fill(1);
                this.weekSelect = Array(7).fill(2);
                break;
            case 5:
                this.schedules = Array(24 * 5).fill(1).concat(Array(24 * 2 + 1).fill(0));
                this.weekSelect = Array(5).fill(2).concat(Array(2).fill(0));
                break;
            case 2:
                this.schedules = Array(24 * 5).fill(0).concat(Array(24 * 2 + 1).fill(1));
                this.weekSelect = Array(5).fill(0).concat(Array(2).fill(2));
                break;
        }
        this.topTimeChange();
    };
    /**
     * @docs-private
     */
    ScheduleComponent.prototype.select = function (i, j) {
        this.schedules[i * 24 + j] = (this.schedules[i * 24 + j] + 1) % 2;
        this.topTimeChange();
        var sum = 0;
        for (var k = i * 24; k < (i + 1) * 24; k++) {
            sum = sum + this.schedules[k];
        }
        if (sum === 0) {
            this.weekSelect[i] = 0;
        }
        else if (sum === 24) {
            this.weekSelect[i] = 2;
        }
        else {
            this.weekSelect[i] = 1;
        }
    };
    /**
     * @docs-private
     */
    ScheduleComponent.prototype.checkDay = function (j) {
        this.weekSelect[j] = this.weekSelect[j] > 0 ? 0 : 2;
        var value = parseInt(this.weekSelect[j] / 2 + '');
        for (var i = j * 24; i < (j + 1) * 24; i++) {
            this.schedules[i] = value;
        }
        this.topTimeChange();
    };
    /**
     * @docs-private
     */
    ScheduleComponent.prototype.showLabel = function (i, j) {
        var value = this.layerTime[i * 24 + j];
        if (value < 3) {
            return '';
        }
        else if (value === 24) {
            return '全天';
        }
        else {
            var index = Math.floor(value / 2);
            var tmp = (value + 1) % 2;
            var label = this.hours[j - index + tmp] + ':00-' + this.hours[j + index + 1] + ':00';
            return label;
        }
    };
    /**
     * @docs-private
     */
    ScheduleComponent.prototype.topTimeChange = function () {
        this.layerTime = [];
        var colspan = 0;
        for (var i = 0; i <= this.schedules.length + 1; i++) {
            if (this.schedules[i] === 0) {
                this.layerTime.push(0);
                colspan = 0;
            }
            else if (this.schedules[i] === 1 && this.schedules[i + 1] === 0) {
                colspan++;
                if (colspan >= 3) {
                    var index = this.layerTime.length - Math.floor(colspan / 2);
                    this.layerTime[index] = colspan;
                }
                this.layerTime.push(0);
            }
            else if (this.schedules[i] === 1 && this.schedules[i + 1] === 1 && (i + 1) % 24 !== 0) {
                this.layerTime.push(0);
                colspan++;
            }
            else if (this.schedules[i] === 1 && this.schedules[i + 1] === 1 && (i + 1) % 24 === 0) {
                colspan++;
                var index = this.layerTime.length - (Math.floor(colspan / 2));
                this.layerTime[index] = colspan;
                this.layerTime.push(0);
                colspan = 0;
            }
        }
        // 输出
        this.selectedOut = {};
        for (var k = 0; k < this.layerTime.length; k++) {
            if (this.layerTime[k] !== 0) {
                var i = Math.floor(k / 24);
                var j = k % 24;
                // debugger
                var half = Math.floor(this.layerTime[k] / 2);
                var tmp = (this.layerTime[k] + 1) % 2;
                var arr = [];
                arr.push(this.hours[j - half + tmp]);
                arr.push(this.hours[j + half + 1]);
                if (this.selectedOut[i] === undefined) {
                    this.selectedOut[i] = [];
                }
                this.selectedOut[i].push(arr);
            }
        }
    };
    /**
     * @docs-private
     */
    ScheduleComponent.prototype.mouseup = function () {
        this.flag = false;
    };
    /**
     * @docs-private
     */
    ScheduleComponent.prototype.mousedown = function (i, j) {
        this.startpoint = [i, j];
        this.flag = true;
        this.preSelect(i, j);
    };
    /**
     * @docs-private
     */
    ScheduleComponent.prototype.mouseover = function (i, j) {
        if (this.flag === true) {
            // let observable = Observable.fromEvent(event.target, 'mouseover')
            // .debounceTime(10)
            // .subscribe(() => this.preSelect(i, j));
            this.preSelect(i, j);
        }
    };
    /**
     * @docs-private
     */
    ScheduleComponent.prototype.mouseout = function (i, j) {
        if (this.flag === true) {
            this.preSelect(i, j);
        }
    };
    /**
     * @docs-private
     */
    ScheduleComponent.prototype.preSelect = function (i, j) {
        var x1 = this.startpoint[0] < i ? this.startpoint[0] : i;
        var x2 = this.startpoint[0] > i ? this.startpoint[0] : i;
        var y1 = this.startpoint[1] < j ? this.startpoint[1] : j;
        var y2 = this.startpoint[1] > j ? this.startpoint[1] : j;
        for (var p = x1; p <= x2; p++) {
            for (var q = y1; q <= y2; q++) {
                this.select(p, q);
            }
        }
    };
    var ScheduleComponent_1;
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", Object)
    ], ScheduleComponent.prototype, "selected", void 0);
    tslib_1.__decorate([
        Output(),
        tslib_1.__metadata("design:type", Object)
    ], ScheduleComponent.prototype, "selectedOut", void 0);
    ScheduleComponent = ScheduleComponent_1 = tslib_1.__decorate([
        Component({
            selector: 'nb-schedule',
            template: "<div class=\"nb-schedule\"><div class=\"hour-select\"><div class=\"hour-select-day\"><nb-select [datasource]=\"selectData\" (onChange)=\"setDay($event)\"></nb-select></div><ul class=\"hour-select-label\"><li>投放时段</li><li>未投时段</li></ul></div><div class=\"hour-table clearfix\"><div class=\"hour-title\"><ng-container *ngFor=\"let item of hours; index as j\"><span *ngIf=\"j % 2 === 0\">{{ item }}:00</span></ng-container></div><div class=\"week-title\"><div class=\"week-item\" *ngFor=\"let item of week; index as j\" (click)=\"checkDay(j)\"><nb-checkbox *ngIf=\"weekSelect[j] === 0\" theme=\"small\">{{ item }}</nb-checkbox><nb-checkbox *ngIf=\"weekSelect[j] === 1\" theme=\"small\" intermediate>{{ item }}</nb-checkbox><nb-checkbox *ngIf=\"weekSelect[j] === 2\" theme=\"small\" checked=\"checked\">{{ item }}</nb-checkbox></div></div><table class=\"table-con\"><tr *ngFor=\"let item1 of week; index as i\"><ng-container *ngFor=\"let item of hours; index as j\"><td *ngIf=\"j < 24\"><button title=\"{{ item }}:00-{{ hours[j + 1] }}:00\" [attr.data]=\"showLabel(i,j)\" [ngClass]=\"{'selected': schedules[i * 24 + j] === 1}\" (mousedown)=\"mousedown(i, j)\" (mouseup)=\"mouseup()\" (mouseover)=\"mouseover(i, j)\" (mouseout)=\"mouseout(i, j)\"></button></td></ng-container></tr></table></div></div>",
            encapsulation: ViewEncapsulation.None,
            changeDetection: ChangeDetectionStrategy.OnPush,
            preserveWhitespaces: false,
            host: {
                'class': 'nb-widget nb-schedule'
            },
            exportAs: 'nbSchedule',
            providers: [{
                    provide: NG_VALUE_ACCESSOR,
                    useExisting: forwardRef(function () { return ScheduleComponent_1; }),
                    multi: true
                }]
        }),
        tslib_1.__metadata("design:paramtypes", [])
    ], ScheduleComponent);
    return ScheduleComponent;
}());
export { ScheduleComponent };
//# sourceMappingURL=schedule.js.map