import { expect } from 'chai';
import cloneDeep from 'lodash/cloneDeep';
import { Tree, TreeNode, TreeRaw } from '../src/tree';

function getIDList(nodes: TreeNode[]): string[] {
  return nodes.map(node => node.ID);
}

const rawData: TreeRaw[] = [
  { ID: '1', parentID: '-1', seq: 1 },
  { ID: '2', parentID: '-1', seq: 3 },
  { ID: '3', parentID: '-1', seq: 2 },
  { ID: '4', parentID: '-1', seq: 4 },
  { ID: '5', parentID: '-1', seq: 5 },
  { ID: '6', parentID: '1', seq: 2 },
  { ID: '7', parentID: '1', seq: 1 },
  { ID: '8', parentID: '7', seq: 1 },
  { ID: '9', parentID: '2', seq: 1 },
];

const complicatedRawData: TreeRaw[] = [
  { ID: '1', parentID: '-1', seq: 1 },
  { ID: '2', parentID: '-1', seq: 4 },
  { ID: '3', parentID: '-1', seq: 3 },
  { ID: '4', parentID: '-1', seq: 5 },
  { ID: '5', parentID: '-1', seq: 6 },
  { ID: '6', parentID: '1', seq: 2 },
  { ID: '7', parentID: '1', seq: 1 },
  { ID: '8', parentID: '7', seq: 1 },
  { ID: '9', parentID: '2', seq: 1 },
  { ID: '10', parentID: '1', seq: 3 },
  { ID: '11', parentID: '1', seq: 4 },
  { ID: '12', parentID: '1', seq: 5 },
  { ID: '13', parentID: '1', seq: 6 },
  { ID: '14', parentID: '-1', seq: 2 },
];

describe('Tree', () => {
  const tree = new Tree(cloneDeep(rawData));

  it('genData', () => {
    const IDList = tree.data.map(item => item.ID);
    expect(IDList).to.have.ordered.members([
      '1',
      '7',
      '8',
      '6',
      '3',
      '2',
      '9',
      '4',
      '5',
    ]);
  });

  it('find', () => {
    const node = tree.find('3');
    expect(node).to.have.property('ID', '3');
  });

  it('findParent', () => {
    const node = tree.findParent('6');
    expect(node).to.have.property('ID', '1');
  });

  it('findNext', () => {
    const node = tree.findNext('7');
    expect(node).to.have.property('ID', '6');
  });

  it('findPrev', () => {
    const node = tree.findPrev('6');
    expect(node).to.have.property('ID', '7');
  });

  it('getNodesPosterity', () => {
    const node1 = tree.find('1');
    const node2 = tree.find('2');
    if (node1 && node2) {
      const nodes = tree.getNodesPosterity([node1, node2]);
      const IDList = getIDList(nodes);
      expect(IDList).to.have.ordered.members(['1', '7', '8', '6', '2', '9']);

      const exNodes = tree.getNodesPosterity([node1, node2], false);
      const exIDList = getIDList(exNodes);
      expect(exIDList).to.have.ordered.members(['7', '8', '6', '9']);
    }
  });

  it('isOperable', () => {
    const node1 = tree.find('1');
    const node2 = tree.find('2');
    const node3 = tree.find('3');
    const node6 = tree.find('6');
    const node7 = tree.find('7');
    const node8 = tree.find('8');
    if (node1 && node2 && node3 && node6 && node7 && node8) {
      expect(tree.isOperable([node1, node2, node3])).to.be.equal(false);
      expect(tree.isOperable([node1, node3, node2])).to.be.equal(false);

      expect(tree.isOperable([node7, node6, node8])).to.be.equal(false);
      expect(tree.isOperable([node7, node8, node6])).to.be.equal(true);
    }
  });

  it('sameDepthNodes-empty-depth', () => {
    const nodes = tree.sameDepthNodes(tree.data);
    const IDList = getIDList(nodes);
    expect(IDList).to.have.ordered.members(['1', '3', '2', '4', '5']);
  });

  it('sameDepthNodes-specific-depth', () => {
    const nodes = tree.sameDepthNodes(tree.data, 1);
    const IDList = getIDList(nodes);
    expect(IDList).to.have.ordered.members(['7', '6', '9']);
  });
});

describe('Tree change', () => {
  it('insert', () => {
    const tree = new Tree(cloneDeep(rawData));
    const treeRaw: TreeRaw[] = [
      {
        ID: '10',
        parentID: '2',
        seq: 2,
      },
      {
        ID: '11',
        parentID: '7',
        seq: 2,
      },
    ];
    const updateData = tree.prepareInsert(treeRaw);
    tree.insert(treeRaw, updateData);
    const IDList = tree.data.map(item => item.ID);
    expect(IDList).to.have.ordered.members([
      '1',
      '7',
      '8',
      '11',
      '6',
      '3',
      '2',
      '9',
      '10',
      '4',
      '5',
    ]);
  });

  it('complicated-insert', () => {
    const tree = new Tree(cloneDeep(complicatedRawData));
    const treeRaw: TreeRaw[] = [
      {
        ID: '15',
        parentID: '7',
        seq: 1,
      },
      {
        ID: '16',
        parentID: '7',
        seq: 1,
      },
      {
        ID: '17',
        parentID: '7',
        seq: 2,
      },
      {
        ID: '18',
        parentID: '12',
        seq: 1,
      },
      {
        ID: '19',
        parentID: '18',
        seq: 1,
      },
    ];
    const updateData = tree.prepareInsert(treeRaw);
    tree.insert(treeRaw, updateData);
    const dataIDList = getIDList(tree.data);
    expect(dataIDList).to.have.ordered.members([
      '1',
      '7',
      '15',
      '16',
      '8',
      '17',
      '6',
      '10',
      '11',
      '12',
      '18',
      '19',
      '13',
      '14',
      '3',
      '2',
      '9',
      '4',
      '5',
    ]);
    expect(tree.parentMap).to.have.all.keys('-1', '1', '7', '12', '18', '2');
    const parentD1 = getIDList(tree.parentMap['-1']);
    expect(parentD1).to.have.ordered.members(['1', '14', '3', '2', '4', '5']);
    const parent1 = getIDList(tree.parentMap['1']);
    expect(parent1).to.have.ordered.members(['7', '6', '10', '11', '12', '13']);
    const parent7 = getIDList(tree.parentMap['7']);
    expect(parent7).to.have.ordered.members(['15', '16', '8', '17']);
    const parent12 = getIDList(tree.parentMap['12']);
    expect(parent12).to.have.ordered.members(['18']);
    const parent18 = getIDList(tree.parentMap['18']);
    expect(parent18).to.have.ordered.members(['19']);
    const parent2 = getIDList(tree.parentMap['2']);
    expect(parent2).to.have.ordered.members(['9']);
  });

  it('delete', () => {
    const tree = new Tree(cloneDeep(rawData));
    const deleteNodes: TreeNode[] = [tree.find('1') as TreeNode];
    // const updateData = tree.prepareDelete(deleteNodes);
    tree.delete(deleteNodes);
    const IDList = tree.data.map(item => item.ID);
    expect(IDList).to.have.ordered.members(['3', '2', '9', '4', '5']);
  });
  it('upMove', () => {
    const tree = new Tree(cloneDeep(rawData));
    const nodes = [tree.find('2'), tree.find('4')];
    const updateData = tree.prepareUpMove(nodes as TreeNode[]);
    tree.move(nodes as TreeNode[], updateData);
    const IDList = getIDList(tree.data);
    expect(IDList).to.have.ordered.members([
      '1',
      '7',
      '8',
      '6',
      '2',
      '9',
      '4',
      '3',
      '5',
    ]);
    expect(nodes[0]).to.have.property('seq', 2);
    expect(nodes[1]).to.have.property('seq', 3);
    const node3 = tree.find('3');
    expect(node3).to.have.property('seq', 4);
  });

  it('specialUpMove', () => {
    const tree = new Tree([
      { ID: '1', parentID: '-1', seq: 1 },
      { ID: '2', parentID: '-1', seq: 2 },
      { ID: '3', parentID: '-1', seq: 2 },
      { ID: '4', parentID: '-1', seq: 2 },
      { ID: '5', parentID: '-1', seq: 5 },
    ]);
    const nodes = [tree.find('3'), tree.find('4')];
    const updateData = tree.prepareUpMove(nodes as TreeNode[]);
    tree.move(nodes as TreeNode[], updateData);
    const IDList = getIDList(tree.data);
    expect(IDList).to.have.ordered.members(['1', '3', '4', '2', '5']);
    expect(nodes[0]).to.have.property('seq', 1);
    expect(nodes[1]).to.have.property('seq', 2);
    const node3 = tree.find('1');
    expect(node3).to.have.property('seq', 0);
  });

  it('downMove', () => {
    const tree = new Tree(cloneDeep(rawData));
    const nodes = [tree.find('3'), tree.find('2')];
    const updateData = tree.prepareDownMove(nodes as TreeNode[]);
    tree.move(nodes as TreeNode[], updateData);
    const IDList = getIDList(tree.data);
    expect(IDList).to.have.ordered.members([
      '1',
      '7',
      '8',
      '6',
      '4',
      '3',
      '2',
      '9',
      '5',
    ]);
    expect(nodes[0]).to.have.property('seq', 3);
    expect(nodes[1]).to.have.property('seq', 4);
    const node4 = tree.find('4');
    expect(node4).to.have.property('seq', 2);
  });

  it('specialDownMove', () => {
    const tree = new Tree([
      { ID: '1', parentID: '-1', seq: 1 },
      { ID: '2', parentID: '-1', seq: 2 },
      { ID: '3', parentID: '-1', seq: 2 },
      { ID: '4', parentID: '-1', seq: 4 },
      { ID: '5', parentID: '-1', seq: 5 },
    ]);
    const nodes = [tree.find('2')];
    const updateData = tree.prepareDownMove(nodes as TreeNode[]);
    tree.move(nodes as TreeNode[], updateData);
    const IDList = getIDList(tree.data);
    expect(IDList).to.have.ordered.members(['1', '3', '2', '4', '5']);
    expect(nodes[0]).to.have.property('seq', 2);
    const node3 = tree.find('3');
    expect(node3).to.have.property('seq', 1);
  });

  it('single-upLevel', () => {
    const tree = new Tree(cloneDeep(complicatedRawData));
    const node10 = tree.find('10');
    if (node10) {
      const updateData = tree.prepareUpLevel([node10]);
      tree.upLevel([node10], updateData);
      const dataIDList = getIDList(tree.data);
      expect(dataIDList).to.have.ordered.members([
        '1',
        '7',
        '8',
        '6',
        '10',
        '11',
        '12',
        '13',
        '14',
        '3',
        '2',
        '9',
        '4',
        '5',
      ]);
      expect(tree.parentMap).to.have.all.keys('-1', '1', '7', '10', '2');
      const parentD1 = getIDList(tree.parentMap['-1']);
      expect(parentD1).to.have.ordered.members([
        '1',
        '10',
        '14',
        '3',
        '2',
        '4',
        '5',
      ]);
      const parent1 = getIDList(tree.parentMap['1']);
      expect(parent1).to.have.ordered.members(['7', '6']);
      const parent7 = getIDList(tree.parentMap['7']);
      expect(parent7).to.have.ordered.members(['8']);
      const parent10 = getIDList(tree.parentMap['10']);
      expect(parent10).to.have.ordered.members(['11', '12', '13']);
      const parent2 = getIDList(tree.parentMap['2']);
      expect(parent2).to.have.ordered.members(['9']);
    }
  });

  it('multi-upLevel', () => {
    const tree = new Tree(cloneDeep(complicatedRawData));
    const node10 = tree.find('10');
    const node11 = tree.find('11');
    if (node10 && node11) {
      const updateData = tree.prepareUpLevel([node10, node11]);
      tree.upLevel([node10, node11], updateData);
      const dataIDList = getIDList(tree.data);
      expect(dataIDList).to.have.ordered.members([
        '1',
        '7',
        '8',
        '6',
        '10',
        '11',
        '12',
        '13',
        '14',
        '3',
        '2',
        '9',
        '4',
        '5',
      ]);
      expect(tree.parentMap).to.have.all.keys('-1', '1', '7', '11', '2');
      const parentD1 = getIDList(tree.parentMap['-1']);
      expect(parentD1).to.have.ordered.members([
        '1',
        '10',
        '11',
        '14',
        '3',
        '2',
        '4',
        '5',
      ]);
      const parent1 = getIDList(tree.parentMap['1']);
      expect(parent1).to.have.ordered.members(['7', '6']);
      const parent7 = getIDList(tree.parentMap['7']);
      expect(parent7).to.have.ordered.members(['8']);
      const parent11 = getIDList(tree.parentMap['11']);
      expect(parent11).to.have.ordered.members(['12', '13']);
      const parent2 = getIDList(tree.parentMap['2']);
      expect(parent2).to.have.ordered.members(['9']);
    }
  });

  it('single-downLevel', () => {
    const tree = new Tree(cloneDeep(complicatedRawData));
    const node10 = tree.find('10');
    if (node10) {
      const updateData = tree.prepareDownLevel([node10]);
      tree.downLevel([node10], updateData);
      const dataIDList = getIDList(tree.data);
      expect(dataIDList).to.have.ordered.members([
        '1',
        '7',
        '8',
        '6',
        '10',
        '11',
        '12',
        '13',
        '14',
        '3',
        '2',
        '9',
        '4',
        '5',
      ]);
      expect(tree.parentMap).to.have.all.keys('-1', '1', '7', '6', '2');
      const parentD1 = getIDList(tree.parentMap['-1']);
      expect(parentD1).to.have.ordered.members(['1', '14', '3', '2', '4', '5']);
      const parent1 = getIDList(tree.parentMap['1']);
      expect(parent1).to.have.ordered.members(['7', '6', '11', '12', '13']);
      const parent7 = getIDList(tree.parentMap['7']);
      expect(parent7).to.have.ordered.members(['8']);
      const parent11 = getIDList(tree.parentMap['6']);
      expect(parent11).to.have.ordered.members(['10']);
      const parent2 = getIDList(tree.parentMap['2']);
      expect(parent2).to.have.ordered.members(['9']);
    }
  });

  it('multi-downLevel', () => {
    const tree = new Tree(cloneDeep(complicatedRawData));
    const node6 = tree.find('6');
    const node10 = tree.find('10');
    if (node6 && node10) {
      const updateData = tree.prepareDownLevel([node6, node10]);
      tree.downLevel([node6, node10], updateData);
      const dataIDList = getIDList(tree.data);
      expect(dataIDList).to.have.ordered.members([
        '1',
        '7',
        '8',
        '6',
        '10',
        '11',
        '12',
        '13',
        '14',
        '3',
        '2',
        '9',
        '4',
        '5',
      ]);
      expect(tree.parentMap).to.have.all.keys('-1', '1', '7', '2');
      const parentD1 = getIDList(tree.parentMap['-1']);
      expect(parentD1).to.have.ordered.members(['1', '14', '3', '2', '4', '5']);
      const parent1 = getIDList(tree.parentMap['1']);
      expect(parent1).to.have.ordered.members(['7', '11', '12', '13']);
      const parent7 = getIDList(tree.parentMap['7']);
      expect(parent7).to.have.ordered.members(['8', '6', '10']);
      const parent2 = getIDList(tree.parentMap['2']);
      expect(parent2).to.have.ordered.members(['9']);
    }
  });
});
