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

@Component({
  selector: 'ngx-prayer',
  templateUrl: './prayer.component.html',
  styleUrls: ['./prayer.component.scss']
})
export class PrayerComponent extends MyAppBase {
  constructor(
    private cellGroupRoutineEventsService: CellGroupRoutineEventsService,
    private toastrService: NbToastrService,
    private cdRef: ChangeDetectorRef,
    private loginUserService: LoginUserService,
    private authService: AuthService,
    protected stateService: StateService,
    protected route: ActivatedRoute,
    protected pastoralDomainService: PastoralDomainService
  ) {
    super(stateService, route, pastoralDomainService);
  }

  cellGroupEvent: CellGroupRoutineEvents;
  data: CellGroupRoutineEventPrayer;

  prayer: BindingHelper<string>;

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

  pageOnInit(): void {
    this.stateService.SetPageTitle("禱告中心");
    this.getAllData();
  }
  getAllData() {

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

      this.data = this.cellGroupEvent.prayers.find(a => a.memberId == this.loginUserService.userAccess.memberId);
      if (!this.data) {
        this.data = new CellGroupRoutineEventPrayer(this.cellGroupEvent.id, this.loginUserService.userAccess.memberId);
        this.cellGroupEvent.prayers.push(this.data);
        this.prayer = new BindingHelper(['']);
      } else {
        this.prayer = new BindingHelper(this.data.prayer.split("|"));
      }
      this.cdRef.detectChanges();
      this.isLoading = false;

    });
  }
  update(): void {
    this.processing = true;
    this.data.prayer = this.prayer.getValue().join('|');
    this.cellGroupRoutineEventsService.createOrUpdatePrayer(this.data).pipe(first()).subscribe(result => {

      this.processing = false;
      this.toastrService.success('禱告事項更新完成!');

      // let message =
      //   `再過 ${NumberUtils.FormatNumber(DateUtils.getIntervalMinutes(new Date(), this.cellGroupEvent.time))} 分鐘就是萬眾期待的小組晚宴啦!!!` + "\n" +
      //   `${DateUtils.format(this.cellGroupEvent.time, 'MM/dd hh:mm aa')} 準時開飯唷~` + "\n" +
      //   "======= 晚宴清單 =======" + "\n";

      // let comment = "";
      // for (let i = 0; i < this.cellGroupEvent.attendees.length; i++) {
      //   const attendees = this.cellGroupEvent.attendees[i];
      //   message += `${attendees.name} - ${attendees.potluckItem.split('|').join(", ")}` + "\n";

      //   if (attendees.comment) {
      //     comment += `${attendees.name} : ${attendees.comment}` + "\n";
      //   }
      // }

      // if (comment) {
      //   message += "\n======= 備註 =======" + "\n" + comment;
      // }

      // message += "\n請使用方舟晚宴系統新增菜單唷!" + "\n" + "https://golife.love/CellGroup/dinner?openExternalBrowser=1"
      // this.lineService.pushLineMessage(message);
    });
  }

  addPrayer() {
    if (this.prayer.length > 1) {
      this.prayer.list = this.prayer.list.filter(d => !!d.value);
    }
    this.prayer.push('');
    this.cdRef.detectChanges();
  }
  logout() {
    this.authService.logout();
  }

}
