{{/*
    Template to create the invocation of a browse type from a given
    product type name, browse type name and browse type definition

    Expects '.' to be a dictionary in the following shape:

    {
        product_type_name: "<product_type_name>",
        browse_type_name: "<browse_type_name>", // or nil
        browse_type: {
            // either
            grey: {
                expression: "",
                range: [low, high], // optional
                nodata: nodatavalue, // optional
            }
            // or
            red: {
                expression: "",
                range: [low, high], // optional
                nodata: nodatavalue, // optional
            },
            green: {
                expression: "",
                range: [low, high], // optional
                nodata: nodatavalue, // optional
            },
            blue: {
                expression: "",
                range: [low, high], // optional
                nodata: nodatavalue, // optional
            },
            // optionally:
            alpha: {
                expression: "",
                range: [low, high], // optional
                nodata: nodatavalue, // optional
            }
        }
    }
*/}}
{{- define "browsetype.cli" }}
    python3 manage.py browsetype create "{{ .product_type_name }}" {{ if .browse_type_name }} "{{ .browse_type_name }}" {{- end }} \
    {{- if hasKey .browse_type "grey" }}
        --grey {{ .browse_type.grey.expression | quote }} \
    {{- if hasKey .browse_type.grey "range" }}
        --grey-range {{ range .browse_type.grey.range }}{{ . }} {{ end }}\
    {{- end }}
    {{- if hasKey .browse_type.grey "nodata" }}
        --grey-nodata {{ .browse_type.grey.nodata }} \
    {{- end }}
    {{- else if and (.browse_type.red) (and .browse_type.green .browse_type.blue) }}
        --red {{ .browse_type.red.expression | quote }} \
        --green {{ .browse_type.green.expression | quote }} \
        --blue {{ .browse_type.blue.expression | quote }} \
    {{- if hasKey .browse_type.red "range" }}
        --red-range {{ range .browse_type.red.range }}{{ . }} {{ end }}\
    {{- end }}
    {{- if hasKey .browse_type.green "range" }}
        --green-range {{ range .browse_type.green.range }}{{ . }} {{ end }}\
    {{- end }}
    {{- if hasKey .browse_type.blue "range" }}
        --blue-range {{ range .browse_type.blue.range }}{{ . }} {{ end }}\
    {{- end }}
    {{- if hasKey .browse_type.red "nodata" }}
        --red-nodata {{ .browse_type.red.nodata }} \
    {{- end }}
    {{- if hasKey .browse_type.green "nodata" }}
        --green-nodata {{ .browse_type.green.nodata }} \
    {{- end }}
    {{- if hasKey .browse_type.blue "nodata" }}
        --blue-nodata {{ .browse_type.blue.nodata }} \
    {{- end }}
    {{- if hasKey .browse_type "alpha" }}
        --grey {{ .browse_type.alpha.expression | quote }} \
    {{- if hasKey .browse_type.alpha "range" }}
        --alpha-range {{ range .browse_type.alpha.range }}{{ . }} {{ end }}\
    {{- end }}
    {{- if hasKey .browse_type.alpha "nodata" }}
        --alpha-nodata {{ .browse_type.alpha.nodata }} \
    {{- end }}
    {{- end }}
    {{- end }}
        --traceback
{{- end -}}

# Check if collection exits in database and initialize database only if not
if python3 manage.py id check {{ index (keys .Values.config.collections) 0 | quote }}; then
    echo "Initialize database"

    python3 manage.py coveragetype import /rgbnir_definition.json \
        --traceback


# TODO: deleteme

    python3 manage.py coveragetype create S2L2A_B01 --field-type B01 B01 "Solar irradiance" "W/m2/um" 1913.57
    python3 manage.py coveragetype create S2L2A_B02 --field-type B02 B02 "Solar irradiance" "W/m2/um" 1941.63
    python3 manage.py coveragetype create S2L2A_B03 --field-type B03 B03 "Solar irradiance" "W/m2/um" 1822.61
    python3 manage.py coveragetype create S2L2A_B04 --field-type B04 B04 "Solar irradiance" "W/m2/um" 1512.79
    python3 manage.py coveragetype create S2L2A_B05 --field-type B05 B05 "Solar irradiance" "W/m2/um" 1425.56
    python3 manage.py coveragetype create S2L2A_B06 --field-type B06 B06 "Solar irradiance" "W/m2/um" 1288.32
    python3 manage.py coveragetype create S2L2A_B07 --field-type B07 B07 "Solar irradiance" "W/m2/um" 1163.19
    python3 manage.py coveragetype create S2L2A_B08 --field-type B08 B08 "Solar irradiance" "W/m2/um" 1036.39
    python3 manage.py coveragetype create S2L2A_B8A --field-type B8A B8A "Solar irradiance" "W/m2/um" 955.19
    python3 manage.py coveragetype create S2L2A_B09 --field-type B09 B09 "Solar irradiance" "W/m2/um" 813.04
    python3 manage.py coveragetype create S2L2A_B11 --field-type B11 B11 "Solar irradiance" "W/m2/um" 245.59
    python3 manage.py coveragetype create S2L2A_B12 --field-type B12 B12 "Solar irradiance" "W/m2/um" 85.25








    echo "Initializing collection {{ index (keys .Values.config.collections) 0 | squote }}."

    {{- range $product_type_name, $product_type := .Values.config.products.types | default dict }}

    #
    # {{ $product_type_name }}
    #

    # create the product type
    python3 manage.py producttype create {{ $product_type_name | quote }} \
        {{ range $_, $coverage_type := $product_type.coverages }}--coverage-type {{ $coverage_type | quote }} \
        {{ end }} --traceback

    {{- if hasKey $product_type "default_browse" }}
    {{- template "browsetype.cli" dict "product_type_name" $product_type_name "browse_type_name" nil "browse_type" (get $product_type.browses $product_type.default_browse) -}}
    {{- end }}

    {{- range $browse_type_name, $browse_type := $product_type.browses }}
    {{- template "browsetype.cli" dict "product_type_name" $product_type_name "browse_type_name" $browse_type_name "browse_type" $browse_type -}}
    {{- end }}

    # create mask type
    {{- range $mask_type_name, $mask_type := $product_type.masks }}
    python3 manage.py masktype create {{ $product_type_name | quote }} {{ $mask_type_name | quote }} \
        {{ if $mask_type.validity -}} --validity \ {{- end }} --traceback
    {{- end }}
    {{- end }} {{/* range .Values.config.products.types */}}

    # set up collection type
    {{- range $collection_name, $collection := .Values.config.collections }}
    python3 manage.py collectiontype create "{{ $collection_name }}_type" \
    {{- range $coverage_type := $collection.coverage_types }}
        --coverage-type {{ $coverage_type | quote }} \
    {{- end }}
    {{- range $product_type_name := $collection.product_types }}
        --product-type {{ $product_type_name | quote }} \
    {{- end }}
        --traceback

    # Instantiate a collection for the collection itself and all levels
    python3 manage.py collection create {{ $collection_name | quote }} \
        --type "{{ $collection_name }}_type" \
        --traceback

    {{- range $product_level := $collection.product_levels }}
    python3 manage.py collection create "{{ $collection_name }}_{{ $product_level }}" \
        --type "{{ $collection_name }}_type" \
        --traceback
    {{- end }}
    {{- end }} {{/* range .collections */}}

else
    echo "Using existing database"
fi
