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}}',
          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: {{treeNode.hasPSDETreeNodeRSs}},
        {{#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 = {
  controlCodeName: '{{ctrl.codeName}}',
  controlName: '{{ctrl.name}}',
  controlService: new TreeService<ControlVO>(ControlVO, new {{pascalCase ctrl.psAppDataEntity.codeName}}Service() ),
  currentSelectedNode: {},
  data: [],
  echoSelectedNodes: [],
  expandedKeys: [],
  selectedKeys: [],
  selectedNodes: []
};