import Component  from '../component.js'
import { elementAttributeContains } from '../../core/utils.js';
import CoUxButton from './ux-button.js';

export default class CoUxTreeNode extends Component {

    constructor(parent) {
      let selector = 'ux-tree-node';
      super(parent, selector);
      //https://citnet.tech.ec.europa.eu/CITnet/stash/projects/CSDR/repos/eui/browse/packages/components/src/lib/ux-components/ux-tree/ux-tree.component.html
    }

    get dataE2E() {
      return this.currentPath + '//li[@data-e2e="ux-tree-node"]';
    }
    async isFirst() {
      return await elementAttributeContains(this.dataE2E, 'class', 'first-child');
    }
    async isLast() {
      return await elementAttributeContains(this.dataE2E, 'class', 'last-child');
    }
    get treeNodeWrapper() {
      let path = '//div[contains(@class,"ux-tree__node-wrapper")]';
      return this.currentPath + path;
    }
    async isSelected() {
      return await elementAttributeContains(this.treeNodeWrapper, 'class', 'ux-tree__node--selected');
    }
    get nodeToggleButton() {
      return new CoUxButton(this.treeNodeWrapper);
    }
    async isNodeExpanded() {
      if ( await (await $(this.nodeToggleButton.buttonIcon)).isExisting() &&
      ((await(await $(this.nodeToggleButton.buttonIcon)).getAttribute('class')).includes('ux-icon-minus-circle-thin'))) {
         return true;
      } else if ( await (await $(this.nodeToggleButton.buttonIcon)).isExisting() &&
      ((await (await $(this.nodeToggleButton.buttonIcon)).getAttribute('class')).includes('ux-icon-plus-circle-thin'))) {
         return false;
      } else {
         return true;
      }
    }
    async isMultiselect() {
      let cl = await(await $(this.treeNodeWrapper + '//span[contains(@class,"ml-2")')).getAttribute('class');
      if (cl.includes('ux-tree__node-checkbox-label')) {
         return true;
      } else {
         return false;
      }
    }
    get nodeContentType() {
      let path = '//span[contains(@class,"ux-tree__node-type")]';
      return this.treeNodeWrapper + path;
    }
    get nodeContentIcon() {
      let path = '//span[contains(@class,"ux-tree__node-icon")]';
      return this.treeNodeWrapper + path;
    }
    get nodeContentLabelWrapped() {
      let path = '//div[contains(@class,"ux-tree__node-label-wrapped")]';
      return this.treeNodeWrapper + path;
    }
    get nodeContentLabel() {
      let path = '//span[contains(@class,"ux-tree__node-label")]';
      return this.treeNodeWrapper + path;
    }
    get nodeContentBadge() {
      let path = '//span[contains(@class,"ux-tree__node-badge")]';
      return this.treeNodeWrapper + path;
    }
    get nodeMetacontentToggle() {
      let path = '//span[contains(@class,"ux-tree__node-metacontent-toggle")]';
      return this.treeNodeWrapper + path;
    }
    get treeNodesList() {
      let path = '//ul[contains(@class,"ux-tree__nodes")]';
      return this.dataE2E + path;
    }
    async hasChildren() {
      let buttonPresent = await(await $(this.treeNodeWrapper + '//ux-button[contains(@class,"ux-tree__node-toggle-icon")]')).isExisting();
      if (buttonPresent) {
         return true;
      } else {
         return false;
      }
    }
    get nodeChild() {
      return new CoUxTreeNode(this.treeNodesList);
    }
    get treeNodesMetacontent() {
      let path = '//span[contains(@class,"ux-tree__node-metacontent")]';
      return this.treeNodeWrapper + path;
    }

    byTitle(title)  {
      this.currentPath = this.parent + '//' + this.selector + '//li[@data-e2e="ux-tree-node" and @title="' + title + '"]/ancestor::' + this.selector + '[1]';
      return this;
    }
}


