{{- if and .Values.enabled .Values.frontendApplication.enabled }}
  {{- $namespace := .Release.Namespace -}}
  {{- $TenantEnvironment  := .Values.global.domain.TenantEnvironment -}}
  {{- $DeployDomainName := ternary (first (regexSplit ":" .Values.global.domain.DeployDomainName -1)) ( .Values.global.domain.DeployDomainName ) (contains ":" .Values.global.domain.DeployDomainName) -}}
  {{- $IsPrivateDeployment := .Values.global.domain.IsPrivateDeployment -}}
  {{- $scope := $ -}}
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: frontend-applications-ingress
  namespace: {{ $namespace }}
  labels:
    type: {{ .Values.frontendApplication.type }}
    accessmode: {{ $.Values.frontendApplication.accessmode }}
    helm.sh/chart: {{ include "all-ingress.name" $scope }}
    app.kubernetes.io/managed-by: {{ $.Release.Service }}
    {{- if $.Values.global.image.tag }}
    app.kubernetes.io/version: {{ $.Values.global.image.tag | quote }}
  {{- end }}
  annotations:
    {{- include "all-ingress.frontend.commom.annotations" $scope | nindent 4 }}
    {{- if .Values.global.ingress.tls_enabled  }}
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
    nginx.ingress.kubernetes.io/permanent-redirect-code: '301'
    {{- end }}
    nginx.ingress.kubernetes.io/configuration-snippet: |
      {{- range $applicationName, $values := .Values.frontendApplication.manifest }}
      {{- if $IsPrivateDeployment }}
      {{- if eq $values.shortname "" }}
      {{- /*  主域名的情况, 域名改造  */}}
      rewrite ^(/green)$ $1/ redirect;
      rewrite ^(/supervision)$ $1/ redirect;
      rewrite ^(/inspection)$ $1/ redirect;
      rewrite ^(/pangu)$ $1/ redirect;
      rewrite ^(/park)$ $1/ redirect;
      {{- /*  主域名的情况, 域名改造 end end end */}}
      {{- else if not (contains "appli" $values.shortname) }}
      {{- /*  特殊短域名的情况   */}}
      {{- /* short name not contains application, judge for situations */}}
      {{- if contains "-portal" $values.shortname}}
      rewrite ^(/cmsportal)$ $1/ redirect;
      {{- else if contains "-rescue" $values.shortname }}
      rewrite ^(/{{ trimSuffix "-rescue" $values.shortname }})$ $1/ redirect;
      {{- else if contains "screen" $values.shortname }}
      rewrite ^(/ {{ trimSuffix "-screen" $values.shortname }})$ $1/ redirect;
      {{- else }}
      {{- /*  没有特殊规则的域名 全部会出现在这里    */}}
      rewrite ^(/{{ $values.shortname }})$ $1/ redirect;
      {{- end }}
      {{- else if  (contains "appli" $values.shortname) }}
      rewrite ^(/{{ trimSuffix "-application" $values.shortname }})$ $1/ redirect;
      {{- else }}
      {{- /*  备份一下 以防万一    */}}
      rewrite ^(/{{ $values.shortname }})$ $1/ redirect;
      {{- end }}
      {{- else }}
      {{- if eq $values.shortname "" }}
      rewrite ^(/{{- $TenantEnvironment -}})$ $1/ redirect;
      {{- else }}
      rewrite ^(/{{- cat $TenantEnvironment $values.shortname | replace " " "/" -}})$ $1/ redirect;
      {{- end }}
      {{- end }}
  {{- end }}
spec:
  rules:
    - host: {{ $DeployDomainName }}
      http:
        paths:
          {{- /* 域名改造, add for demo  */}}
          - path: /inspection/?(.*)
            pathType: ImplementationSpecific
            backend:
              serviceName: cmii-uav-platform
              servicePort: 9528
          - path: /supervision/?(.*)
            pathType: ImplementationSpecific
            backend:
              serviceName: cmii-uav-platform
              servicePort: 9528
          - path: /green/?(.*)
            pathType: ImplementationSpecific
            backend:
              serviceName: cmii-uav-platform
              servicePort: 9528
          - path: /pangu/?(.*)
            pathType: ImplementationSpecific
            backend:
              serviceName: cmii-uav-platform
              servicePort: 9528
          - path: /park/?(.*)
            pathType: ImplementationSpecific
            backend:
              serviceName: cmii-uav-platform
              servicePort: 9528
          {{- /* 域名改造, end end end  */}}
          {{- range $applicationName, $values := .Values.frontendApplication.manifest }}
          {{- if $IsPrivateDeployment }}
          {{- if eq $values.shortname ""}}
          - path: /?(.*)
          {{- else if (contains "appli" $values.shortname) }}
          - path: /{{ trimSuffix "-application" $values.shortname }}/?(.*)
          {{- else }}
          - path: /{{ $values.shortname }}/?(.*)
          {{- end }}
          {{- else }}
          {{- if eq $values.shortname ""}}
          - path: /{{ $TenantEnvironment }}/?(.*)
          {{- else if  not (contains "appli" $values.shortname) }}
          - path: /{{ $TenantEnvironment }}/{{ trimSuffix "-application" $values.shortname }}/?(.*)
          {{- else }}
          - path: /{{- cat $TenantEnvironment $values.shortname | replace " " "/" -}}/?(.*)
            {{- end }}
            {{- end }}
            pathType: ImplementationSpecific
            backend:
              serviceName: {{ $applicationName }}
              servicePort: 9528
  {{- end }}
  {{- if .Values.global.ingress.tls_enabled  }}
  tls:
    - hosts:
        - {{ $DeployDomainName }}
    - secretName: {{ $DeployDomainName | quote }}
  {{- end }}
  {{- end }}

