import { Component, Input, OnInit, Optional } from '@angular/core';
import { NbDialogRef, NbDialogService } from '@nebular/theme';
import { first } from 'rxjs/operators';
import { HappinessWeek } from '../../entity/HappinessGroup';
import { PastoralDomain } from '../../entity/PastoralDomain';
import { PastoralDomainService } from '../../services/crudServices/pastoral-domain.service';
import { NumberUtils } from '../../utilities/number-utils';
import { StringUtils } from '../../utilities/string-utils';
import { HappinessWeekEditorComponent } from '../happiness-week-editor/happiness-week-editor.component';
import { WeekTaskEditorComponent } from '../week-task-editor/week-task-editor.component';

@Component({
  selector: 'happiness-week-list',
  templateUrl: './happiness-week-list-dlg.component.html',
  styleUrls: ['./happiness-week-list-dlg.component.scss']
})
export class HappinessWeekListDlgComponent implements OnInit {

  processing: boolean = false;

  private _group: PastoralDomain;
  public get group(): PastoralDomain {
    return this._group;
  }
  @Input() public set group(v: PastoralDomain) {
    if (this._group != v) {
      this._group = v;
      this.currentWeek = this.group.happinessWeeks.find(w => w.date >= new Date());
    }
  }
  currentWeek: HappinessWeek
  public get isDlg(): boolean {
    return this.dlgRef != null;
  }

  constructor(
    private dlgService: NbDialogService,
    private pastoralDomainService: PastoralDomainService,
    @Optional() private dlgRef: NbDialogRef<any>
  ) { }

  ngOnInit(): void {
  }
  close() {
    this.dlgRef.close();
  }
  openEditDlg(week: HappinessWeek) {
    this.dlgService.open(HappinessWeekEditorComponent, {
      context: {
        data: week,
        allWeeks: this.group.happinessWeeks
      }
    }).onClose.pipe(first()).subscribe(result => {
      if (result) {
        if (this.isDlg) {
          this.close();
        } else {
          this.runGetAllData();
        }
      }
      if (result) this.close();
    });
  }
  openTaskDlg(week: HappinessWeek, showingCost: boolean) {
    this.dlgService.open(WeekTaskEditorComponent, {
      context: {
        data: week,
        showingCost: showingCost,
        allData: this.group.happinessWeeks
      }
    }).onClose.pipe(first()).subscribe(result => {
      if (result) {
        if (this.isDlg) {
          this.close();
        } else {
          this.runGetAllData();
        }
      }
    });

  }

  runGetAllData() {

    this.processing = true;
    this.pastoralDomainService.initialize().pipe(first()).subscribe(result => {
      this.group = this.pastoralDomainService.myHappinessGroup;
      this.processing = false;
    });
  }
  weekDisplay(seq: number) {
    let week = this.group.happinessWeeks[seq - 1];
    let title = `W${seq} ${week.topic}`;
    let weekCost = week.costs.map(d => d.amount).reduce((a, b) => a + b, 0);
    if (weekCost > 0) {
      title += "\t" + StringUtils.getHtmlBadge(`Cost: $${NumberUtils.FormatCurrency(weekCost)}`, 'warning')
    }
    return title;
  }
}
