/*
 * Copyright (C) 2019 Robert Bosch GmbH Copyright (C) 2019 Robert Bosch Manufacturing Solutions GmbH, Germany. All rights reserved.
 */

import { Component, OnInit } from '@angular/core';
import { NotificationService } from '@bci-web-core/core';
import { ScrollIntoViewService } from '../../shared/scroll-into-view.service';
import { fruits, thingsArray } from './picklist-demo-model';

/**
 * This demo shows the basic use of the picklist and, in addition, other use cases such as filtering
 * or item templates.
 */
@Component({
  selector: 'bci-picklist-demo',
  templateUrl: './picklist-demo.component.html',
  styleUrls: ['./picklist-demo.component.scss'],
})
export class PicklistDemoComponent implements OnInit {

  /**List of numbers to demo virtual scrolling behvaiour */
  thingsList = [];

  /** Name list of all available fruits. */
  fruitNamesList = [];

  /** Number of currently available source items. */
  sourceItemCount: number;

  /** Number of currently available target items. */
  targetItemCount: number;

  constructor(private notificationService: NotificationService,
    public scrollIntoViewService: ScrollIntoViewService) {}

  ngOnInit() {
    setTimeout(() => {
      this.fruitNamesList = fruits.map((fruit) => fruit.name);
      this.onSourceChanged(this.fruitNamesList);
      this.thingsList=thingsArray;
    }, 3000);
  }

  reloadSource() {
    this.fruitNamesList = fruits.map((fruit) => fruit.name);
    this.onSourceChanged(this.fruitNamesList);
  }

  /**
   * Called when the user adds/removes items to/from the source list.
   * @param fruitNames The source items contained after the change.
   */
  onSourceChanged(fruitNames: string[]): void {
    this.sourceItemCount = fruitNames ? fruitNames.length : 0;
  }

  /**
   * Called when the user adds/removes items to/from the target list.
   * @param fruitNames The target items contained after the change.
   */
  onTargetChanged(fruitNames: string[]): void {
    this.targetItemCount = fruitNames ? fruitNames.length : 0;
    if (fruitNames && fruitNames.length > 0) {
      this.notificationService.information(`The fruit salad contains ${fruitNames.join(', ')}.`);
    } else {
      this.notificationService.information('The fruit salad is empty.');
    }
  }
}
