import { Component, EventEmitter, Input, Output, SimpleChanges } from '@angular/core';
import { NzContextMenuService, NzDropdownMenuComponent } from 'ng-zorro-antd/dropdown';
import { NzFormatEmitEvent, NzTreeNode, NzTreeNodeOptions } from 'ng-zorro-antd/tree';
import { CommonNzModule } from '../../common.nz.module';

@Component({
  selector: 'department-tree',
  standalone: true,
  imports: [CommonNzModule],
  templateUrl: './department-tree.component.html',
  styleUrl: './department-tree.component.less',
})
export class DepartmentTreeComponent {
  // activated node
  activatedNode?: NzTreeNode;
  @Output() onAdd = new EventEmitter<NzTreeNode>();
  @Output() onDelete = new EventEmitter<NzTreeNode>();
  @Input() tree: NzTreeNodeOptions[] = [];
  expandedKeys: string[] = [];
  nodes: NzTreeNodeOptions[] = [
    // {
    //   title: 'parent 0',
    //   key: '100',
    //   author: 'NG ZORRO',
    //   expanded: true,
    //   children: [
    //     { title: 'leaf 0-0', key: '1000', author: 'NG ZORRO', isLeaf: true },
    //     { title: 'leaf 0-1', key: '1001', author: 'NG ZORRO', isLeaf: true },
    //   ],
    // },
    // {
    //   title: 'parent 1',
    //   key: '101',
    //   author: 'NG ZORRO',
    //   children: [
    //     { title: 'leaf 1-0', key: '1010', author: 'NG ZORRO', isLeaf: true },
    //     { title: 'leaf 1-1', key: '1011', author: 'NG ZORRO', isLeaf: true },
    //   ],
    // },
  ];

  ngOnChanges(changes: SimpleChanges): void {
    if (changes['tree']) {
      if (!this.nodes.length) {
        this.nodes = changes['tree'].currentValue;
      } else {
        const newNodes = changes['tree'].currentValue;
        this.retainExpandedProperty(newNodes);
        this.nodes = [...newNodes];
        setTimeout(() => {
          this.expandedKeys = this.expandedKeys.slice();
        }, 300);
      }
    }
  }
  retainExpandedProperty(tree: NzTreeNodeOptions[]) {
    tree.forEach(n => {
      if (this.expandedKeys.includes(n.key)) {
        n['isExpanded'] = true;
      }
      if (n.children) {
        this.retainExpandedProperty(n.children);
      }
    });
  }
  isFirstLevel(node: NzTreeNodeOptions) {
    return !node['parent'];
  }
  openFolder(data: NzTreeNode | NzFormatEmitEvent): void {
    const key = (data as NzFormatEmitEvent).node?.key;
    if (key) {
      if (this.expandedKeys.includes(key)) {
        this.expandedKeys = this.expandedKeys.filter(k => k !== key);
        (data as NzFormatEmitEvent).node?.setExpanded(false);
      } else {
        this.expandedKeys = [...this.expandedKeys, key];
      }
    }
  }

  activeNode(data: NzFormatEmitEvent): void {
    this.activatedNode = data.node!;
  }

  contextMenu($event: MouseEvent, menu: NzDropdownMenuComponent, node: NzTreeNode): void {
    this.nzContextMenuService.create($event, menu);
    this.activatedNode = node;
  }

  handleExpandChange(data: NzFormatEmitEvent): void {
    this.expandedKeys = data.keys || [];
  }
  handleAdd() {
    this.onAdd.emit(this.activatedNode);
  }
  handleDelete() {
    this.onDelete.emit(this.activatedNode);
  }
  constructor(private nzContextMenuService: NzContextMenuService) {}
}
