import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { NbDialogService } from '@nebular/theme';
import { Observable } from 'rxjs';
import { first } from 'rxjs/operators';
import { CellGroupRoutineEvents } from '../../entity/CellGroupRoutineEvents';
import { CellGroupRoutineEventsService } from '../../services/crudServices/cell-group-routine-events.service';
import { MsgBoxService } from '../../services/msg-box.service';
import { StateService } from '../../services/state.service';
import { FancySettings } from '../../ui/fancy-table/fancy-settings.model';
import { FancyTableComponent } from '../../ui/fancy-table/fancy-table.component';

@Component({
  selector: 'ngx-cell-group-routine-events',
  templateUrl: './cell-group-routine-events.component.html',
  styleUrls: ['./cell-group-routine-events.component.scss']
})
export class CellGroupRoutineEventsComponent implements OnInit {


  @ViewChild(FancyTableComponent, { static: false }) fancyTable: FancyTableComponent<CellGroupRoutineEvents>;
  data: CellGroupRoutineEvents;
  allData: CellGroupRoutineEvents[];
  selectedIndex: number = 0;

  constructor(
    private cellGroupRoutineEventsService: CellGroupRoutineEventsService,
    private msgBoxService: MsgBoxService,
    private dlgService: NbDialogService,
    protected stateService: StateService,
    protected route: ActivatedRoute,
  ) {
    //super(stateService, route);
  }

  //#region Table Setting
  settings = <FancySettings<CellGroupRoutineEvents>>{
    contextMenuItems: [
      {
        id: 'add',
        enabled: true,
        title: 'Add New',
        icon: 'plus-circle-outline',
        callback: (datum, element) => {
          this.openEditingDialog(true);
        },
      },
      {
        id: 'edit',
        enabled: true,
        title: 'Edit',
        icon: 'edit',
        callback: (datum, element) => {
          this.openEditingDialog(false);
        },
      },
      {
        enabled: true,
        id: 'delete',
        title: 'Delete',
        icon: 'trash-2-outline',
        callback: (datum, element) => {
          this.delete(datum);
        },
      }],
    onContextMenuOpening: (datum, menuItems) => {
      menuItems.find(i => i.id == 'delete').visible = !!datum;
      menuItems.find(i => i.id == 'edit').visible = !!datum;
      return datum;
    },
    columns: [

    ],

  };
  //#endregion

  //#region Implements Methods
  ngOnInit(): void {
  }
  get saveMethod(): Observable<any> {
    return Observable.of(true);
  }
  getAllData() {
    this.cellGroupRoutineEventsService.getAll().pipe(first()).subscribe(result => {

      this.allData = result;
    });
  }
  //#endregion

  //#region UI Calling Methods
  openEditingDialog(isAdding: boolean) {
    // this.dlgService.open(CellGroupRoutineEventsEditorComponent, {
    //   context: {
    //     data: isAdding ? null : ObjectUtils.CloneValue(this.allData[this.selectedIndex]),
    //     isAdding: isAdding
    //   }
    // }).onClose.pipe(first()).subscribe(result => {
    //   if (result) {
    //     this.getAllData();
    //   }
    // });
  }

  //#endregion

  //#region CRUD Methods
  delete(datum) {
    this.msgBoxService.showConfirmDeleteBox().pipe(first()).subscribe(answer => {
      if (answer === true) {
        this.cellGroupRoutineEventsService.delete(datum).pipe(first()).subscribe(result => {

          this.allData.splice(this.selectedIndex, 1);
          this.fancyTable.reload();
        });
      }
    });
  }
  //#endregion

}