#!/bin/bash

# Check if the input file is provided
if [ "$#" -lt 1 ]; then
    echo "Usage: $0 <input-file>"
    exit 1
fi

input_file="$1"

cat << EOF
<lumed-data-table-control class=""
                          [modelsPrototype]="modelPrototype"
                          [models]="items"
                          (eventChannel)="onEvent(\$event)"
                          [isReadOnly]="false"
                          [isFixed]="true"
                          noDataRowText="No data fits the current filters"
                          [pageSize]="30"
                          footerAdditionalInfo="[maxResults]='100'"
                          [actionMenuTemplate]="actionMenu"
                          [hasDetailButton]="true"
                          [lumedSideDetailsTemplate]="sideDetails"
                          [lumedSideDetailsColumnNames]="[
EOF

$(dirname "$0")/enculumedParseMetadata.bash "$input_file" | while IFS= read -r line; do
    IFS=':' read -r name type lookup <<< "$line"
    if [[ "$type" != "none" ]]; then
        echo "                            '$name',"
    fi
done

cat << EOF
                          ]"
                          lumedSideDetailsTitle="$input_file"
                          [hasAddButton]="true"
                          [hasEditButton]="true"
                          [hasDeleteButton]="true"
                          [hasSelectionCheckbox]="false"
                          (selectedItemsChange)="onEvent(\$event)"
                          [isMultipleSelection]="false"
                          [editableOnlyWhenSelected]="false"
                          [templates]="{
EOF

$(dirname "$0")/enculumedParseMetadata.bash "$input_file" | while IFS= read -r line; do
    IFS=':' read -r name type lookup <<< "$line"
    if [[ "$type" != "none" ]]; then
        echo "                            $name : ${name}Template,"
    fi
done

cat << EOF
                          }"
                          (lumedColumnConfigChange)="onEvent(\$event)"
                          [allowColumnReordering]="true"
                          [allowColumnResizing]="true"
                          [autoSelectFirstItem]="true"
                          [columnsOptions]="{}"
                          [columnsOrder]="[]"
                          [lumedColumnConfigDisabled]="false"
                          [lumedColumnConfigDefault]="{
EOF

$(dirname "$0")/enculumedParseMetadata.bash "$input_file" | while IFS= read -r line; do
    IFS=':' read -r name type lookup <<< "$line"
    if [[ "$type" != "none" ]]; then
        echo "                            $name : { visible: true, index: null, width: null },"
    fi
done

cat << EOF
                          }"
                          lumedExcelExport
                          [lumedExcelExportDisabled]="false">

    <lumed-button-mdc additional-button
                      class="filter-button"
                      tooltip="additional button">
        :)
    </lumed-button-mdc>
</lumed-data-table-control>

<ng-template #actionMenu
             let-item="item">
    <button mat-menu-item>B==D</button>
</ng-template>

<ng-template #sideDetails
             let-item="item">
    <lumed-form-mdc #form="ngForm"
                    name="form-data-demo"
                    ngForm
                    [form]="form">
        <lumed-data-form name="Form DATA"
                         class=""
                         [isReadOnly]="true"
                         [disabledFields]="[]"
                         [modelPrototype]="modelPrototype"
                         [model]="item"
                         (eventChannel)="onEvent(\$event)"
                         (submitted)="onEvent(\$event)" />
    </lumed-form-mdc>
</ng-template>

EOF


$(dirname "$0")/enculumedParseMetadata.bash "$input_file" | while IFS= read -r line; do
    IFS=':' read -r name type lookup <<< "$line"
    if [[ "$type" != "none" ]] && [[ "$type" != "None" ]] && [[ "$lookup" == "none" ]]; then
cat << EOF
<ng-template #${name}Template
             let-item="item"
             let-dataContext="dataContext">
    <lumed-display-data name="${name}Column"
                        class=""
                        [hideAdditionalInformation]="true"
                        [isReadOnly]="true"
                        displayAdapterType="${type}"
                        memberName="${name}"
                        [model]="dataContext?.model" />
</ng-template>

EOF
    elif [[ "$type" != "none" ]] && [[ "$type" != "None" ]] && [[ "$lookup" != "none" ]]; then
cat << EOF
<ng-template #${name}Template
             let-item="item"
             let-dataContext="dataContext">
    <lumed-display-data name="${name}Column"
                        class=""
                        [hideAdditionalInformation]="true"
                        [isReadOnly]="true"
                        displayAdapterType="${type}"
                        memberName="${name}"
                        lookupProviderKey="$lookup"
                        [model]="dataContext?.model" />
</ng-template>

EOF
    fi
done


