import {
  Component,
  OnInit,
  AfterViewInit,
  AfterContentInit,
  ViewChild
} from '@angular/core';
import { Router } from '@angular/router';
import { TreeNode, MenuItem, ContextMenu } from 'primeng/primeng';
import { Store, select } from '@ngrx/store';
import * as ListStore from '../../store/list';
import { ListSelector } from '../../store';
import { Page, PageParams } from 'app/commons/model';
import { RPCClientError } from '@loafer/ng-rpc/protocol';
import { Target } from 'packages/target/model';
import { Infra, InfraHost, InfraOSApplication, InfraService } from '../../model';
import { Domain } from 'packages/domain/model';
import { AuthSelector } from 'packages/member/store';

import { sensorListSelector } from 'packages/sensor/store';

import * as SensorListStore from 'packages/sensor/store/list';
import { Sensor } from 'packages/sensor/model';

import * as _ from 'lodash';

interface HostData {
  id: string;
  target?: Target;
  host: InfraHost;
  services: InfraService[];
}

@Component({
  selector: 'of-infra-map',
  templateUrl: './map.component.html'
})
export class MapComponent implements OnInit, AfterContentInit {
  // infraTree: TreeNode[] = testInfraList;
  infraTree: TreeNode[] = [];

  infras$ = this.listStore.pipe(select(ListSelector.select('page')));
  sensors$ = this.sensorListStore.pipe(select(sensorListSelector.select('page')));

  display = false;
  loading = false;

  totalList: Infra[];
  hostDataList: HostData[] = new Array();

  sensorMap: Map<number, Array<Sensor>> = new Map();

  targetTreeMap: Map<number, TreeNode> = new Map();

  DEFAULT_EXPANDED: Boolean = true;

  // visibleSidebar = false;
  // selectedNodeType: string;
  // selectedNode: Object = null;

  contextMenuProbe: MenuItem[];
  contextMenuHost: MenuItem[];

  @ViewChild('cmProbe') cmProbe: ContextMenu;
  @ViewChild('cmHost') cmHost: ContextMenu;

  constructor(private router: Router,
    private listStore: Store<ListStore.State>,
    private sensorListStore: Store<SensorListStore.State>
  ) { }

  ngAfterContentInit() {

    this.listStore.select(AuthSelector.select('domain')).subscribe(
      (domain: Domain) => {

        const pageParams: PageParams = {
          pageNo: '0',
          countPerPage: '10',
          sortCol: 'id',
          sortDirection: 'descending'
        };

        this.listStore.dispatch(new ListStore.ReadAllByDomain({ domain: domain, pageParams: pageParams }));
      },
      (error) => {
        console.log(error);
      }
    );


    this.infras$.subscribe(
      (page: Page) => {
        console.log(page);
        if (page !== null) {
          this.totalList = page.content;
          console.log(this.totalList);
          this.infraTree = this.generateInfraHostData();
        }
      },
      (error: RPCClientError) => {
        console.log(error.response.message);
      });

    this.sensors$.subscribe(
      (page: Page) => {
        if (page !== null) {
          const sensorList = page.content;
          console.log(sensorList);
          this.addTreeForSensor(sensorList);
        }
      },
      (error: RPCClientError) => {
        console.log(error.response.message);
      }
    );
  }

  ngOnInit() {
    this.initContextMenu();
  }

  initContextMenu() {
    this.contextMenuProbe = [
      // { label: 'Delete file', icon: 'fa-delete', command: (event) => this.deleteFile(this.selectedMenuItem) }
      { label: 'ProbeMenu', icon: 'fa-check' }
    ];
    this.contextMenuHost = [
      { label: 'HostMenu1', icon: 'fa-check' },
      { label: 'HostMenu2', icon: 'fa-check' },
    ];
  }

  searchObj(treeList: any[], target: Target, searchList: any[]) {

    if (treeList === undefined || treeList === null) {
      return;
    }

    for (const t of treeList) {
      if (t.obj !== undefined && t.obj == null) {
        if (t.obj.target.id === target.id) {
          searchList.push(t);
        } else {
          this.searchObj(t.children, target, searchList);
        }
      }
    }

  }

  addTreeForSensor(sensorList: Array<Sensor>) {

    if (sensorList === undefined || sensorList === null || sensorList.length <= 0) {
      return;
    }

    // const tInfraTree = _.clone(this.infraTree);

    // const it = tInfraTree[0];
    // const it = this.infraTree[0];
    // // for (const it of this.infraTree) {
    //   if (it.children === null || it.children === undefined) {
    //     it.children = [];
    //   }

    //   for (const itt of this.infraTree) {

    //   }
    // }

    for (const sensor of sensorList) {
      const st = {
        label: 'Sensors - ' + sensor.crawler.name,
        type: 'sensor',
        obj: sensor,
        expanded: true
      };

      // FIXME:: target test id ....
      const tt = this.targetTreeMap.get(4);
      // const tt = this.targetTreeMap.get(sensor.target.id);
      if (tt !== undefined && tt !== null) {
        tt.children.push(st);
      }


      // it.children.push(st);

    }

    // this.infraTree = tInfraTree;

  }


  generateInfraHostData(filterStr?: string): TreeNode[] {

    const itl: TreeNode[] = [];

    const probeMap: Map<number, InfraHost[]> = new Map();

    const hostMap: Map<number, InfraService[]> = new Map();

    this.loading = true;

    this.sortInfraToMap(probeMap, hostMap, filterStr);

    this.targetTreeMap.clear();

    const infraTree: TreeNode = this.generateInfraTree(probeMap, hostMap);

    for (const infra of this.totalList) {
      this.getSensorByInfra(infra);
    }

    this.loading = false;

    itl.push(infraTree);

    return itl;

  }

  generateInfraTree(probeMap: Map<number, InfraHost[]>, hostMap: Map<number, InfraService[]>): TreeNode {

    const infraTree = {
      label: 'Infra',
      type: 'infra',
      expandedIcon: 'fa-folder-open',
      collapsedIcon: 'fa-folder',
      expanded: true,
      children: [],
    };

    probeMap.forEach((ifhl: InfraHost[], key: number) => {

      const tp = {
        label: 'Probe - ' + key,
        type: 'probe',
        expandedIcon: 'fa-folder-open',
        collapsedIcon: 'fa-folder',
        expanded: this.DEFAULT_EXPANDED.valueOf(),
        obj: key,
        children: [],
      };


      ifhl.map((ih: InfraHost, idx: number) => {

        const th = {
          label: 'Host - ' + ih.ip,
          type: 'host',
          expandedIcon: 'fa-folder-open',
          collapsedIcon: 'fa-folder',
          expanded: this.DEFAULT_EXPANDED.valueOf(),
          obj: ih,
          children: [],
        };

        this.targetTreeMap.set(ih.target.id, th);

        if (hostMap.has(ih.ip)) {

          const ifsl = hostMap.get(ih.ip);



          for (let i = 0; i < ifsl.length; ++i) {
            const ts = {
              label: 'Service - ' + ifsl[i].vendor.name,
              type: 'service',
              expandedIcon: 'fa-folder-open',
              collapsedIcon: 'fa-folder',
              expanded: this.DEFAULT_EXPANDED.valueOf(),
              obj: ifsl[i],
              children: [],
            };

            this.targetTreeMap.set(ifsl[i].target.id, ts);

            th.children.push(ts);
          }

        }


        tp.children.push(th);

      });

      // ProbeTree.children.push(tp);
      infraTree.children.push(tp);
    });

    return infraTree;
  }

  sortInfraToMap(probeMap: Map<number, InfraHost[]>, hostMap: Map<number, InfraService[]>, filterStr: string) {

    for (const infra of this.totalList) {
      const infraType = infra.infraType.name;
      if (infraType === 'HOST') {
        const infraHost: InfraHost = infra;
        if (filterStr && String(infraHost.ip).indexOf(filterStr) < 0) {
          continue;
        }

        if (probeMap.has(infraHost.probe.id) === false) {
          probeMap.set(infraHost.probe.id, []);
        }

        const ihl: InfraHost[] = probeMap.get(infraHost.probe.id);
        ihl.push(infraHost);
        probeMap.set(infraHost.probe.id, ihl);

      } else if (infraType === 'OS_SERVICE') {
        const infraService: InfraService = infra;
        if (filterStr && this.checkFilterString(infraService, filterStr)) {
          continue;
        }

        if (hostMap.has(infraService.host.ip) === false) {
          hostMap.set(infraService.host.ip, []);
        }

        const isl = hostMap.get(infraService.host.ip);
        isl.push(infraService);

      }

    }

  }

  getSensorByInfra(infra: Infra) {

    const pageParams: PageParams = {
      pageNo: '0',
      countPerPage: '10',
      sortCol: 'id',
      sortDirection: 'descending'
    };

    this.sensorListStore.dispatch(new SensorListStore.ReadAllByInfra({ id: String(infra.id), pageParams: pageParams }));

  }


  checkFilterString(infraService: InfraService, filterStr: string) {
    const upperCased = filterStr.toUpperCase().toUpperCase();
    if (infraService.vendor.name.toUpperCase().indexOf(upperCased) < 0 &&
      String(infraService.port).toUpperCase().indexOf(upperCased) < 0 &&
      infraService.portType.toUpperCase().indexOf(upperCased)) {
      return true;
    }
    return false;
  }

  getExistHost(infraHost: InfraHost): HostData {
    let node = null;
    for (const data of this.hostDataList) {
      if (data.host.ip === infraHost.ip) {
        node = data;
      }
    }
    return node;
  }

  showDialog() {
    this.display = true;
  }

  closeDialog() {
    this.display = false;
  }

  expandAll() {
    this.infraTree.forEach(node => {
      this.expandRecursive(node, true);
    });
  }

  collapseAll() {
    this.infraTree.forEach(node => {
      this.expandRecursive(node, false);
    });
  }

  private expandRecursive(node: TreeNode, isExpand: boolean) {
    node.expanded = isExpand;
    if (node.children) {
      node.children.forEach(childNode => {
        this.expandRecursive(childNode, isExpand);
      });
    }
  }

  onNodeSelect(event) {
    const nodeType = event.node.type;
    // this.selectedNodeType = nodeType;
    if (nodeType === 'probe') {
      this.router.navigate(['probe', event.node.obj, 'info']);
    } else if (nodeType === 'host') {
    }

    // const nodeType = event.node.type;
    // if (nodeType !== 'probe' && nodeType !== 'host' && nodeType !== 'service') {
    //   return;
    // }
    // this.selectedNodeType = nodeType;
    // if (nodeType === 'probe') {
    //   const probe = {
    //     id: event.node.obj
    //   };
    //   this.selectedNode = probe;
    // } else {
    //   this.selectedNode = event.node.obj;
    // }

    // this.visibleSidebar = true;
  }

  public nodeMenu(event: MouseEvent, node: any) {
    this.cmProbe.hide();
    this.cmHost.hide();
    if (node.type === 'probe') {
      this.cmProbe.show(event);
    }
    if (node.type === 'host') {
      this.cmHost.show(event);
    }

    return false;
  }

}

const testInfraList = [
  {
    label: 'Infra',
    type: 'infra',
    expandedIcon: 'fa-folder-open',
    collapsedIcon: 'fa-folder',
    expanded: true,
    children: [
      {
        label: 'Zone - 192.168.1.0/24',
        type: 'zone',
        expandedIcon: 'fa-folder-open',
        collapsedIcon: 'fa-folder',
        expanded: true,
        children: [
          {
            label: 'Host - 192.168.1.106 - Snoop Host',
            type: 'host',
            expandedIcon: 'fa-folder-open',
            collapsedIcon: 'fa-folder',
            expanded: true,
            children: [
              {
                label: 'Sensors - WMI, SSH, SNMP',
                expandedIcon: 'fa-folder-open',
                collapsedIcon: 'fa-folder',
                expanded: true
              },
              {
                label: 'Service - FTP(21)',
                expandedIcon: 'fa-folder-open',
                collapsedIcon: 'fa-folder',
                expanded: true,
                children: [
                  {
                    label: 'Sensors - FTP, Sensor',
                    expandedIcon: 'fa-folder-open',
                    collapsedIcon: 'fa-folder',
                    expanded: true
                  }
                ]
              }
            ]
          },
          {
            label: 'Host - 192.168.1.103 - Geek Host',
            expandedIcon: 'fa-folder-open',
            collapsedIcon: 'fa-folder',
            expanded: true,
            children: [
              {
                label: 'Sensors - WMI, SSH, SNMP',
                expandedIcon: 'fa-folder-open',
                collapsedIcon: 'fa-folder',
                expanded: true
              },
              {
                label: 'Service - MySQL(3306)',
                expandedIcon: 'fa-folder-open',
                collapsedIcon: 'fa-folder',
                expanded: true,
                children: [
                  {
                    label: 'Sensors - MySQL, PING',
                    expandedIcon: 'fa-folder-open',
                    collapsedIcon: 'fa-folder',
                    expanded: true
                  }
                ]
              },
              {
                label: 'Service - PostgreSQL(5555)',
                expandedIcon: 'fa-folder-open',
                collapsedIcon: 'fa-folder',
                expanded: true,
                children: [
                  {
                    label: 'Sensors - PostgreSQL, PING',
                    expandedIcon: 'fa-folder-open',
                    collapsedIcon: 'fa-folder',
                    expanded: true
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        label: 'Zone - 192.168.10.0/24',
        expandedIcon: 'fa-folder-open',
        collapsedIcon: 'fa-folder',
        expanded: true,
        children: [
          {
            label: 'Host - 192.168.10.106 - Snoop Host',
            expandedIcon: 'fa-folder-open',
            collapsedIcon: 'fa-folder',
            expanded: true,
            children: [
              {
                label: 'Sensors - WMI, SSH, SNMP',
                expandedIcon: 'fa-folder-open',
                collapsedIcon: 'fa-folder',
                expanded: true
              },
              {
                label: 'Service - FTP(21)',
                expandedIcon: 'fa-folder-open',
                collapsedIcon: 'fa-folder',
                expanded: true,
                children: [
                  {
                    label: 'Sensors - FTP, Sensor',
                    expandedIcon: 'fa-folder-open',
                    collapsedIcon: 'fa-folder',
                    expanded: true
                  }
                ]
              }
            ]
          },
          {
            label: 'Host - 192.168.10.103 - Geek Host',
            expandedIcon: 'fa-folder-open',
            collapsedIcon: 'fa-folder',
            expanded: true,
            children: [
              {
                label: 'Sensors - WMI, SSH, SNMP',
                expandedIcon: 'fa-folder-open',
                collapsedIcon: 'fa-folder',
                expanded: true
              },
              {
                label: 'Service - MySQL(3306)',
                expandedIcon: 'fa-folder-open',
                collapsedIcon: 'fa-folder',
                expanded: true,
                children: [
                  {
                    label: 'Sensors - MySQL, PING',
                    expandedIcon: 'fa-folder-open',
                    collapsedIcon: 'fa-folder',
                    expanded: true
                  }
                ]
              },
              {
                label: 'Service - PostgreSQL(5555)',
                expandedIcon: 'fa-folder-open',
                collapsedIcon: 'fa-folder',
                expanded: true,
                children: [
                  {
                    label: 'Sensors - PostgreSQL, PING',
                    expandedIcon: 'fa-folder-open',
                    collapsedIcon: 'fa-folder',
                    expanded: true
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
];

