import { ChangeDetectorRef, Component, OnInit } from '@angular/core';
import { first } from 'rxjs/operators';
import { CellGroupRoutineEventAttendee, CellGroupRoutineEvents } from '../../entity/CellGroupRoutineEvents';
import { MsgBoxService } from '../../services/msg-box.service';
import { NbToastrService } from '@nebular/theme';
import { SessionService } from '../../services/session.service';
import { LineService } from '../../services/line.service';
import { HeaderService } from '../../services/header.service';
import { StringUtils } from '../../utilities/string-utils';
import { DateUtils } from '../../utilities/date-utils';
import { NumberUtils } from '../../utilities/number-utils';
import { CellGroupRoutineEventsService } from '../../services/crudServices/cell-group-routine-events.service';
import { ActivatedRoute } from '@angular/router';
import { PastoralDomainService } from '../../services/crudServices/pastoral-domain.service';
import { MyAppBase } from '../MyAppBase';
import { StateService } from '../../services/state.service';
import { LoginUserService } from '../../services/login-user.service';

@Component({
  selector: 'ngx-dinner',
  templateUrl: './dinner.component.html',
  styleUrls: ['./dinner.component.scss']
})
export class DinnerComponent extends MyAppBase {
  constructor(
    private cellGroupRoutineEventsService: CellGroupRoutineEventsService,
    private messageService: MsgBoxService,
    private toastrService: NbToastrService,
    private sessionService: SessionService,
    private lineService: LineService,
    private cdRef: ChangeDetectorRef,
    private loginUserService: LoginUserService,
    protected stateService: StateService,
    protected route: ActivatedRoute,
    protected pastoralDomainService: PastoralDomainService
  ) {
    super(stateService, route, pastoralDomainService);
  }

  cellGroupEvent: CellGroupRoutineEvents;
  data: CellGroupRoutineEventAttendee;

  potluckItems: DinnerInfo[] = [{ item: '' }];

  isLoading: boolean = true;
  processing: boolean = false;

  pageOnInit(): void {
    this.stateService.SetPageTitle("晚宴系統");
    this.getAllData();

  }

  getAllData() {

    this.isLoading = true;
    this.cellGroupRoutineEventsService.getComingEvent().pipe(first()).subscribe(result => {
      this.cellGroupEvent = result;

      this.data = this.cellGroupEvent.attendees.find(a => a.id == this.loginUserService.userAccess.memberId);
      if (!this.data) {
        this.data = new CellGroupRoutineEventAttendee(this.cellGroupEvent.id, this.loginUserService.userAccess.memberId);
        this.data.name = this.loginUserService.userAccess.firstName;
        this.cellGroupEvent.attendees.push(this.data);
      } else {
        this.potluckItems = this.data.potluckItem.split("|").map(s => new DinnerInfo(s));
      }
      this.cdRef.detectChanges();
      this.isLoading = false;

    });
  }
  update(): void {
    this.processing = true;
    this.data.potluckItem = this.potluckItems.filter(item => !!item.item).map(t => t.item).join('|');
    this.cellGroupRoutineEventsService.createOrUpdateAttendee(this.data).pipe(first()).subscribe(result => {

      this.processing = false;
      this.toastrService.success('菜單更新完成!');
      this.lineService.pushCommandMessage(this.cellGroupEvent.pastoralDomainId, 'dinner');
    });
  }

  addAttendee() {
    if (this.potluckItems.length > 1) {
      this.potluckItems = this.potluckItems.filter(d => !!d.item);
    }
    this.potluckItems.push(new DinnerInfo(''));
    this.cdRef.detectChanges();
  }
  addItem() {
    if (this.potluckItems.length > 1) {
      this.potluckItems = this.potluckItems.filter(d => !!d.item);
    }
    this.potluckItems.push(new DinnerInfo(''));
    this.cdRef.detectChanges();
  }
}

export class DinnerInfo {
  /**
   *
   */
  constructor(item: string) {
    this.item = item;
  }
  item: string;
}