import { ControlVOBase, TreeControlVO, TreeNodeVO, TreeNodeRSVO, TreeService } from "@core";
import { {{pascalCase ctrl.psAppDataEntity.codeName}}Service } from '@api/{{spinalCase ctrl.psAppDataEntity.codeName}}/{{spinalCase ctrl.psAppDataEntity.codeName}}-service';

export class ControlVO extends ControlVOBase implements TreeControlVO {

  get rootVisible(): boolean {
    return {{ctrl.rootVisible}};
  }

  get treeNodes(): TreeNodeVO[] {
    return [
      {{#each ctrl.psDETreeNodes as | treeNode |}}
      {
        allowDrag: {{treeNode.allowDrag}},
        allowDrop: {{treeNode.allowDrop}},
        allowEditText: {{treeNode.allowEditText}},
        allowOrder: {{treeNode.allowOrder}},
        {{#if treeNode.psAppDataEntity}}
        appDataEntity: {
          codeName: '{{treeNode.psAppDataEntity.codeName}}',
          {{#each treeNode.psAppDataEntity.allPSAppDEFields as | field |}}
          {{#if field.keyField}}
          keyField: '{{field.codeName}}',
          {{/if}}
          {{#if field.majorField}}
          majorField: '{{field.codeName}}',
          {{/if}}
          {{/each}}
        },
        {{/if}}
        {{#if treeNode.appendCaption}}
        appendCaption: {{treeNode.appendCaption}},
        {{/if}}
        {{#if treeNode.psAppDEDataSet}}
        appDeDataSet: { codeName: '{{treeNode.psAppDEDataSet.codeName}}' },
        {{/if}}
        appendPNodeId: {{treeNode.appendPNodeId}},
        {{#if treeNode.psAppCodeList}}
        codeList: {
          codeName: '{{treeNode.psAppCodeList.codeName}}',
          tag: '{{treeNode.psAppCodeList.codeListTag}}',
          type: '{{treeNode.psAppCodeList.codeListType}}'
        },
        {{/if}}
        counterId: '{{treeNode.counterId}}',
        counterMode: {{treeNode.counterMode}},
        {{#if treeNode.psSysCss}}
        cssName: '{{treeNode.psSysCss.cssName}}',
        {{/if}}
        {{#if treeNode.psDETreeNodeDataItems}}
        deTreeNodeDataItems: [
          {{#each treeNode.psDETreeNodeDataItems as | dataItem |}}
          {
            customCode: {{dataItem.customCode}},
            {{#if dataItem.psAppDEField}}
            fieldCodeName: '{{dataItem.psAppDEField.codeName}}',
            {{/if}}
            name: '{{dataItem.name}}'
          },
          {{/each}}
        ],
        {{/if}}
        disableSelect: {{treeNode.disableSelect}},
        enableCheck: {{treeNode.enableCheck}},
        enableQuickSearch: {{treeNode.enableQuickSearch}},
        expandFirstOnly: {{treeNode.expandFirstOnly}},
        expanded: {{treeNode.expanded}},
        hasPSDETreeNodeRSs: {{#if treeNode.psDETreeNodeRSs}}true{{else}}{{#if treeNode.hasPSDETreeNodeRSs}}true{{else}}false{{/if}}{{/if}},
        {{#if treeNode.leafFlagPSAppDEField}}
        leafFlagPSAppDEField: { codeName: '{{treeNode.leafFlagPSAppDEField.codeName}}' },
        {{/if}}
        {{#if treeNode.idPSAppDEField}}
        idPSAppDEField: { codeName: '{{treeNode.idPSAppDEField.codeName}}' },
        {{/if}}
        {{#if treeNode.iconPSAppDEField}}
        iconPSAppDEField: { codeName: '{{treeNode.iconPSAppDEField.codeName}}' },
        {{/if}}
        {{#if treeNode.psSysImage}}
        {{#if treeNode.psSysImage.cssClass}}
        iconcls: '{{treeNode.psSysImage.cssClass}}',
        {{else if (treeNode.psSysImage.imagePath)}}
        icon: '{{treeNode.psSysImage.imagePath}}',
        {{/if}}
        {{/if}}
        id: '{{treeNode.id}}',
        navFilter: '{{treeNode.navFilter}}',
        {{#if treeNode.psNavigateContexts}}
        navigateContext: {
          {{#each treeNode.psNavigateContexts as | navContext |}}
          {{navContext.key}}: '{{#if navContext.rawValue}}{{navContext.value}}{{else}}%{{navContext.value}}%{{/if}}',
          {{/each}}
        },
        {{/if}}
        {{#if treeNode.psNavigateParams}}
        navigateParams: {
          {{#each treeNode.psNavigateParams as | navParam |}}
          {{navParam.key}}: '{{#if navParam.rawValue}}{{navParam.value}}{{else}}%{{navParam.value}}%{{/if}}',
          {{/each}}
        },
        {{/if}}
        {{#if treeNode.navPSAppView}}
        navView: {
          viewName: '{{treeNode.navPSAppView.codeName}}'
        },
        {{/if}}
        {{#if treeNode.nodeValue}}
        nodeValue: '{{treeNode.nodeValue}}',
        {{/if}}
        nodeType: '{{treeNode.nodeType}}',
        rootNode: {{treeNode.rootNode}},
        selectFirstOnly: {{treeNode.selectFirstOnly}},
        selected: {{treeNode.selected}},
        {{#if treeNode.sortDir}}
        sortDir: '{{treeNode.sortDir}}',
        {{/if}}
        {{#if treeNode.sortPSAppDEField}}
        sortPSAppDEField: { codeName: '{{treeNode.sortPSAppDEField.codeName}}' },
        {{/if}}
        {{#if treeNode.textFormat}}
        textFormat: '{{treeNode.textFormat}}',
        {{/if}}
        {{#if treeNode.textPSAppDEField}}
        textPSAppDEField: { codeName: '{{treeNode.textPSAppDEField.codeName}}' },
        {{/if}}
        {{#if treeNode.text}}
        text: '{{treeNode.text}}',
        {{/if}}
        {{#if treeNode.tooltip}}
        tooltip: '{{treeNode.tooltip}}',
        {{/if}}
        treeNodeType: '{{treeNode.treeNodeType}}',
      },
      {{/each}}
    ];
  }

  get treeNodeRSs(): TreeNodeRSVO[] {
    return [
      {{#each ctrl.psDETreeNodeRSs as | treeNodeRS |}}
      {
        {{#if treeNodeRS.childPSDETreeNode}}
        childDeTreeNode: {
          id: '{{treeNodeRS.childPSDETreeNode.id}}'
        },
        {{/if}}
        {{#if treeNodeRS.psNavigateContexts}}
        navigateContext: {
          {{#each treeNodeRS.psNavigateContexts as | navContext |}}
          {{navContext.key}}: '{{#if navContext.rawValue}}{{navContext.value}}{{else}}%{{navContext.value}}%{{/if}}',
          {{/each}}
        },
        {{/if}}
        {{#if treeNodeRS.psNavigateParams}}
        navigateParams: {
          {{#each treeNodeRS.psNavigateParams as | navParam |}}
          {{navParam.key}}: '{{#if navParam.rawValue}}{{navParam.value}}{{else}}%{{navParam.value}}%{{/if}}',
          {{/each}}
        },
        {{/if}}
        {{#if treeNodeRS.psDETreeNodeRSParams}}
        params: {
          {{#each treeNodeRS.psDETreeNodeRSParams as | param |}}
          {{param.key}}: { value: '{{param.value}}' }
          {{/each}}
        },
        {{/if}}
        {{#if treeNodeRS.parentFilter}}
        parentFilter: '{{treeNodeRS.parentFilter}}',
        {{/if}}
        {{#if treeNodeRS.parentPSDER1N}}
        parentPSDER1N: { codeName: '{{treeNodeRS.parentPSDER1N.codeName}}' },
        {{/if}}
        {{#if treeNodeRS.parentPSAppDEField}}
        parentPSAppDEField: { codeName: '{{treeNodeRS.parentPSAppDEField.codeName}}' },
        {{/if}}
        {{#if treeNodeRS.parentPSDETreeNode}}
        parentPSDETreeNode: { id: '{{treeNodeRS.parentPSDETreeNode.id}}' },
        {{/if}}
        parentValueLevel: {{#if treeNodeRS.parentValueLevel}}{{treeNodeRS.parentValueLevel}}{{else}}null{{/if}},
        searchMode: {{treeNodeRS.searchMode}}
      },
      {{/each}}
    ];
  }
}

export const ctrlState = () => {
  return {
    appEntityCodeName: '{{ctrl.psAppDataEntity.codeName}}',
    appDeCodeName:'{{ctrl.psAppDataEntity.codeName}}',
    appDeLogicName: '{{ctrl.psAppDataEntity.logicName}}',
    appDeKeyFieldName: '{{#if ctrl.psAppDataEntity.keyPSAppDEField}}{{ctrl.psAppDataEntity.keyPSAppDEField.codeName}}{{/if}}',
    appDeMajorFieldName: '{{#if ctrl.psAppDataEntity.majorPSAppDEField}}{{ctrl.psAppDataEntity.majorPSAppDEField.codeName}}{{/if}}',
    {{#and ctrl.psAppCounterRef ctrl.psAppCounterRef.psAppCounter}}
    appCounterRef: {
      {{#with ctrl.psAppCounterRef.psAppCounter as | counter |}}
      id: '{{ctrl.psAppCounterRef.id}}',
      getAction: '{{counter.getPSAppDEAction.codeName}}',
      timer: {{#if counter.timer}}{{counter.timer}}{{else}}6000{{/if}},
        {{#if counter.psAppDataEntity}}
      deCodeName: '{{lowerCase counter.psAppDataEntity.codeName}}'
        {{/if}}
      {{/with}}
    },
    counterService: {},
    {{/and}}
    contextMenus: {
      {{#each ctrl.psDETreeNodes as | treeNode |}}
        {{#if (and treeNode.psDEContextMenu treeNode.psDEContextMenu.psDEToolbarItems)}}
      {{treeNode.nodeType}}: [
          {{#each treeNode.psDEContextMenu.psDEToolbarItems as | item |}}
            {{#neq item.itemType "SEPERATOR"}}
        {{> @macro/front-end/widgets/tree-detail/tree-node-context-menu-item.hbs item=item owner=treeNode.nodeType}}
            {{/neq}}
          {{/each}}
      ],
        {{/if}}
      {{/each}}
    },
    controlCodeName: '{{ctrl.codeName}}',
    controlName: '{{ctrl.name}}',
    controlService: new TreeService<ControlVO>(ControlVO, new {{pascalCase ctrl.psAppDataEntity.codeName}}Service() ),
    currentSelectedNode: {},
    items: [],
    outputIconDefault: {{#eq ctrl.outputIconDefault false}}false{{else}}true{{/eq}},
    echoSelectedNodes: [],
    expandedKeys: [],
    selectedKeys: [],
    selectedNodes: [],
    selections: []
  }
};