import { ChangeDetectorRef, Component, OnInit } from '@angular/core';
import { NbDialogRef, NbDialogService, NbToastrService } from '@nebular/theme';
import { first } from 'rxjs/operators';
import { DropDownOption } from '../../entity/dropDownOption';
import { HappinessCost } from '../../entity/happiness-cost.model';
import { HappinessWeek, HappinessTaskType, HappinessTask } from '../../entity/HappinessGroup';
import { HappinessCostService } from '../../services/crudServices/happiness-cost.service';
import { HappinessTaskService } from '../../services/crudServices/happiness-group.service';
import { StringUtils } from '../../utilities/string-utils';
import { AddNewCostDlgComponent } from './add-new-cost-dlg/add-new-cost-dlg.component';

@Component({
  selector: 'ngx-week-task-editor',
  templateUrl: './week-task-editor.component.html',
  styleUrls: ['./week-task-editor.component.scss']
})
export class WeekTaskEditorComponent implements OnInit {
  processing: boolean = false;
  allData: HappinessWeek[];
  data: HappinessWeek;
  taskrOptions: DropDownOption[] = [];
  showingCost: boolean = false;
  constructor(
    private happinessTaskService: HappinessTaskService,
    private happinessCostService: HappinessCostService,
    private dlgRef: NbDialogRef<WeekTaskEditorComponent>,
    private dlgService: NbDialogService,
    private toastrService: NbToastrService,
    private cdRef: ChangeDetectorRef
  ) {

  }
  ngOnInit(): void {
    if (this.data.tasks.length == 0) {
      this.data.tasks = [];
      for (let i = HappinessTaskType.IceBreak; i <= HappinessTaskType.Dessert; i++) {
        this.data.tasks.push(
          {
            weekId: this.data.id,
            id: 'new',
            type: i as HappinessTaskType,
            tasker: '',
            content: ''
          } as HappinessTask

        )
      }
    }
    let names = [] as string[];
    this.allData.forEach(week => {
      if (week.tasks) {
        names = names.concat(week.tasks.map(t => t.tasker ? t.tasker.trim() : ''));

      }
    });
    this.taskrOptions = names.filter(function (item, pos) {
      return !StringUtils.isNullOrWhitespace(item) && names.indexOf(item) == pos;
    }).map(name => new DropDownOption(name, name));
  }

  close() {
    this.dlgRef.close();
  }
  update() {
    if (this.processing == false) {

      this.processing = true;

      this.happinessTaskService.createOrUpdateAll(this.data.tasks).pipe(first()).subscribe(result => {
        this.processing = false;
        this.toastrService.success("Update Success!");
      });
    }
  }

  updateCost() {
    if (this.processing == false) {

      this.processing = true;

      this.happinessCostService.createOrUpdateAll(this.data.costs).pipe(first()).subscribe(result => {
        this.processing = false;
        this.toastrService.success("Update Cost Success!");
      });
    }

  }
  getTaskTitle(t: HappinessTaskType) {
    switch (t) {
      case HappinessTaskType.IceBreak: return '帶遊戲';
      case HappinessTaskType.Worship: return '唱歌';
      case HappinessTaskType.Testimony: return '見證';
      case HappinessTaskType.Message: return '信息';
      case HappinessTaskType.Gift: return '準備禮物';
      case HappinessTaskType.Dessert: return '準備點心';
      default: break;
    }
  }

  toggleView() {
    this.showingCost = !this.showingCost;
  }
  addNewCost() {
    this.dlgService.open(AddNewCostDlgComponent, {
      context: {
        taskrOptions: this.taskrOptions
      }
    }).onClose.pipe(first()).subscribe(result => {
      if (result) {
        result.weekId = this.data.id;
        this.happinessCostService.createOrUpdate(result).pipe(first()).subscribe(result => {
          this.data.costs.push(result);
        });
      }
    });
  }
  gotoNextWeek() {
    if ((this.allData.indexOf(this.data) + 1) < this.allData.length) {
      this.data = this.allData[this.allData.indexOf(this.data) + 1];
      this.cdRef.detectChanges();
    }
  }
  gotoPreviousWeek() {
    if ((this.allData.indexOf(this.data) - 1) >= 0) {
      this.data = this.allData[this.allData.indexOf(this.data) - 1];
      this.cdRef.detectChanges();
    }
  }
}