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

import { Component } from '@angular/core';
import {
  Node,
  RootDataSource,
  RootNode,
} from '@bci-web-core/web-components/dist/types/components/multi-selector/model/multi-selector.interface';
import { of } from 'rxjs';
import { delay } from 'rxjs/operators';

// demo nodes of multi selector tree for second search type
const groups_node1_level1: Node = { id: 'groups_1', label: 'Blue Group', hasChildren: true, typeLabel: 'Big Groups' };

const groups_node2_level2: Node = { id: 'groups_2', label: 'Green Group', hasChildren: false, typeLabel: 'Little Groups' };
const groups_node3_level2: Node = { id: 'groups_3', label: 'Tiny Yellow Group 1', hasChildren: false, typeLabel: 'Tiny Groups' };
const groups_node4_level2: Node = { id: 'groups_4', label: 'Tiny Yellow Group 2', hasChildren: false, typeLabel: 'Tiny Groups' };
const groups_node5_level2: Node = { id: 'groups_5', label: 'Tiny Yellow Group 3', hasChildren: false, typeLabel: 'Tiny Groups' };
const groups_node6_level2: Node = { id: 'groups_6', label: 'Tiny Yellow Group 4', hasChildren: false, typeLabel: 'Tiny Groups' };
const groups_node7_level2: Node = { id: 'groups_7', label: 'Tiny Yellow Group 5', hasChildren: false, typeLabel: 'Tiny Groups' };
const groups_node8_level2: Node = { id: 'groups_8', label: 'Tiny Yellow Group 6', hasChildren: false, typeLabel: 'Tiny Groups' };

// demo nodes of multi selector tree
const node1_level1: Node = { id: '1_1', label: 'Enterprise 3', hasChildren: true, typeLabel: 'Enterprise' };

const node2_level2: Node = { id: '2_2', label: 'Region A', hasChildren: false, typeLabel: 'Region' };
const node3_level2: Node = { id: '3_2', label: 'Generic Site 1', hasChildren: false, typeLabel: 'Site' };
const node4_level2: Node = { id: '4_2', label: 'Generic Site 2', hasChildren: false, typeLabel: 'Site' };
const node5_level2: Node = { id: '5_2', label: 'Generic Site 3', hasChildren: false, typeLabel: 'Site' };
const node6_level2: Node = { id: '6_2', label: 'Generic Site 4', hasChildren: false, typeLabel: 'Site' };
const node7_level2: Node = { id: '7_2', label: 'Generic Site 5', hasChildren: false, typeLabel: 'Site' };
const node8_level2: Node = { id: '8_2', label: 'Generic Site 6', hasChildren: true, typeLabel: 'Site' };

const node9_level3: Node = { id: '9_3', label: 'Site 6 Area 4.1', hasChildren: false, typeLabel: 'Area' };
const node10_level3: Node = { id: '10_3', label: 'Site 6 Area 5.1', hasChildren: true, typeLabel: 'Area' };
const node11_level3: Node = { id: '11_3', label: 'Site 6 Area 3.1', hasChildren: false, typeLabel: 'Area' };

const node12_level4: Node = {
  id: '12_4',
  label: 'Site 6 Area 5.1 Line 1',
  hasChildren: true,
  typeLabel: 'Work Center',
};
const node13_level4: Node = {
  id: '13_4',
  label: 'Site 6 Area 5.1 Line 2',
  hasChildren: false,
  typeLabel: 'Work Center',
};
const node14_level4: Node = {
  id: '14_4',
  label: 'Site 6 Area 5.1 Line 3',
  hasChildren: false,
  typeLabel: 'Work Center',
};

const node15_level5: Node = { id: '15_5', label: 'Site 6 Area 5.1 Station 1', hasChildren: true, typeLabel: 'Station' };

const node16_level6: Node = {
  id: '16_6',
  label: 'Site 6 Area 5.1 Station 1 Sensor 1',
  hasChildren: false,
  typeLabel: 'Sensor',
};
const node17_level6: Node = {
  id: '17_6',
  label: 'Site 6 Area 5.1 Station 1 Sensor 2',
  hasChildren: false,
  typeLabel: 'Sensor',
};
// demo initial selection
const initialSelection = [
  [
    {
      id: '1_1',
      label: 'Enterprise 3',
    },
    {
      id: '8_2',
      label: 'Generic Site 6',
    },
    {
      id: '10_3',
      label: 'Site 6 Area 5.1',
    },
    {
      id: '12_4',
      label: 'Site 6 Area 5.1 Line 1',
    },
    {
      id: '15_5',
      label: 'Site 6 Area 5.1 Station 1',
    },
  ],
];

@Component({
  selector: 'bci-multi-selector-demo',
  templateUrl: './multi-selector-demo.component.html',
  styleUrls: ['./multi-selector-demo.component.scss'],
})
export class MultiSelectorDemoComponent {
  // indicates selection mode, multi or single
  singleSelectionMode = 'multi';
  // indicates if demo page simulates loading
  simulateLoading = false;
  // indicates if standard demo has an initial selection
  initialSelectionBool = false;
  // initial selection is an empty array or filled by swapInitialSelection method
  initialSelection = [];
  // sample data
  multiSelectorDataSource: RootDataSource = {
    getRoots: () => {
      return Promise.resolve([node1_level1]);
    },
    getChildren: (nodeId: Node['id']) => {
      if (this.simulateLoading === false) {
        return this.getNonLoadingData(nodeId);
      } else {
        return this.loadData(nodeId);
      }
    },
  };
  // sample data (groups for second search type)
  multiSelectorDataSourceGroups: RootDataSource = {
    getRoots: () => {
      return Promise.resolve([groups_node1_level1]);
    },
    getChildren: (nodeId: Node['id']) => {
      return this.getNonLoadingDataGroups(nodeId);
    },
  };
  // sample data with 2 search types
  rootNodes: RootNode[] = [
    { id: '1', label: 'Facilities', dataSource: this.multiSelectorDataSource },
    { id: '2', label: 'Groups', dataSource: this.multiSelectorDataSourceGroups },
  ];
  // sample data with 1 search types
  rootNodesSingleSearchType: RootNode[] = [
    { id: '1', label: 'Search Type 1', dataSource: this.multiSelectorDataSource },
  ];

  /**
   * function to get children of given nodeId
   * @param nodeId nodeId from parent
   * @returns children nodes of given nodeId
   */
  getNonLoadingData(nodeId: string): Promise<Node[]> {
    if (nodeId === '1_1') {
      return Promise.resolve([
        node2_level2,
        node3_level2,
        node4_level2,
        node5_level2,
        node6_level2,
        node7_level2,
        node8_level2,
      ]);
    }
    if (nodeId === '8_2') {
      return Promise.resolve([node9_level3, node10_level3, node11_level3]);
    }
    if (nodeId === '10_3') {
      return Promise.resolve([node12_level4, node13_level4, node14_level4]);
    }
    if (nodeId === '12_4') {
      return Promise.resolve([node15_level5]);
    }
    if (nodeId === '15_5') {
      return Promise.resolve([node16_level6, node17_level6]);
    }
  }

  /**
   * function to get children of given nodeId
   * @param nodeId nodeId from parent
   * @returns children nodes of given nodeId
   */
  getNonLoadingDataGroups(nodeId: string): Promise<Node[]> {
    if (nodeId === 'groups_1') {
      return Promise.resolve([
        groups_node2_level2,
        groups_node3_level2,
        groups_node4_level2,
        groups_node5_level2,
        groups_node6_level2,
        groups_node7_level2,
        groups_node8_level2,
      ]);
    }
  }
  /**
   * function to simulate loading of children of given nodeId
   * @param nodeId nodeId from parent
   * @returns children nodes of given nodeId
   */
  loadData(nodeId: string): Promise<Node[]> {
    if (nodeId === '1_1') {
      return of([node2_level2, node3_level2, node4_level2, node5_level2, node6_level2, node7_level2, node8_level2])
        .pipe(delay(2000))
        .toPromise();
    }
    if (nodeId === '8_2') {
      return of([node9_level3, node10_level3, node11_level3]).pipe(delay(2000)).toPromise();
    }
    if (nodeId === '10_3') {
      return of([node12_level4, node13_level4, node14_level4]).pipe(delay(2000)).toPromise();
    }
    if (nodeId === '12_4') {
      return of([node15_level5]).pipe(delay(2000)).toPromise();
    }
    if (nodeId === '15_5') {
      return of([node16_level6, node17_level6]).pipe(delay(2000)).toPromise();
    }
  }
  /**
   * swaps initialSelectionBool and sets initialSelection or set it to an empty array
   */
  swapInitialSelection() {
    if (this.initialSelectionBool === false) {
      this.initialSelectionBool = true;
      this.initialSelection = initialSelection;
    } else {
      this.initialSelectionBool = false;
      this.initialSelection = [];
    }
  }
  /**
   * swaps simulateLoading boolean
   */
  swapSimulateLoading() {
    if (this.simulateLoading === false) {
      this.simulateLoading = true;
    } else {
      this.simulateLoading = false;
    }
  }
  /**
   * swaps singleSelectionMode from single to multi and multi to single
   */
  swapSingleSelectionMode() {
    if (this.singleSelectionMode === 'single') {
      this.singleSelectionMode = 'multi';
    } else {
      this.singleSelectionMode = 'single';
    }
  }
}
