import { ViewContainerRef } from '@angular/core';
import { Component, Input } from '@angular/core';
import { ModalDialogOptions, ModalDialogService } from '@nativescript/angular';
import { rightChevron } from '~/constants/icons';
import { CongEventDecisionOutput } from './../../../entities/cong-event-detail.types';
import { CongDecisionDetailModalComponent } from './../cong-decision-detail-modal/cong-decision-detail-modal.component';


@Component({
    selector: 'CongEventDecisionList',
    moduleId: module.id,
    templateUrl: './cong-event-decision-list.component.html',
})
export class CongEventDecisionListComponent {

    @Input()
    isLoading: boolean = true;
    rightChevron = rightChevron;

    private _decisions: (CongEventDecisionOutput | any)[]
    noSuggesterList: (CongEventDecisionOutput | any)[]
    hasSuggesterList: (CongEventDecisionOutput | any)[]
    @Input()
    set decisions(val: CongEventDecisionOutput[]) {
        this.hasSuggesterList = val.filter(i => i.Suggester).map(r => ({ ...r, type: 'suggester' }));
        this.noSuggesterList = val.filter(i => !i.Suggester).map(r => ({ ...r, type: 'noSuggester' }));
        this._decisions = [
            this.initListItems[0], ...this.hasSuggesterList,
            this.initListItems[1], ...this.noSuggesterList,
        ];
    }
    get decisions() {
        return this._decisions;
    };


    initListItems = [
        { type: 'suggester_header' },
        { type: 'noSuggester_header' },
    ]
    constructor(private _modalService: ModalDialogService,
        private _vcRef: ViewContainerRef,) {

    }
    shortenText(text: string) {
        if (text.length <= 100) return text
        return text.slice(0, 100) + '...'
    }

    onDetailTap(decision: CongEventDecisionOutput) {
        const options: ModalDialogOptions = {
            viewContainerRef: this._vcRef,
            context: decision,
            fullscreen: true,
        };
        this._modalService.showModal(CongDecisionDetailModalComponent, options)
    }



    templateSelector(item: any) {
        return item.type;
    }

}
