{{- if .Values.enabled.enabled }}
  {{- $namespace := .Release.Namespace -}}
  {{- $deployName := .Values.deploy.name -}}
  {{- $domain := ternary (first (regexSplit ":" .Values.global.domain.DeployDomainName -1)) ( .Values.global.domain.DeployDomainName ) (contains ":" .Values.global.domain.DeployDomainName) -}}
  {{- $httpPort := ternary (last (regexSplit ":" .Values.global.domain.DeployDomainName -1)) ( 80 ) (contains ":" .Values.global.domain.DeployDomainName) -}}
  {{- $rtmpPort := .Values.apps.srs.ports.rtmp }}
  {{- $scheme := .Values.global.ingress.tls_enabled | ternary "https" "http" }}

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ $deployName }}-cm
  namespace: {{ $namespace }}
  labels:
  {{ include "srs.labels.standard" .  | nindent 4 }}
data:
  {{- if .Values.enabled.cluster }}
  srs.origin.conf: |-
    {{- include "srs.config.origin" . | nindent 4 }}

  srs.edge.conf: |-
    {{- include "srs.config.edge" . | nindent 4 }}

  {{- else }}
  srs.rtc.conf: |-
    {{- include "srs.config.rtc" . | nindent 4 }}
    {{- end }}

  {{- if .Values.enabled.nginx }}
  srs.nginx.conf: "empty"
  {{- end }}

  srs.op.conf: |-
    debug: false
    server:
      port: 8080

    spring:
      application:
        name: cmii-srs-operator
      platform:
        info:
          name: cmii-live-srs-operator
          description: cmii-live-srs-operator
          version: 1.2.0
          scanPackage: com.cmii.live
      datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        url: jdbc:mysql://{{ .Values.apps.mysql.endpoint }}/cmii_live_srs_op?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
        username: {{ .Values.apps.mysql.username }}
        password: {{ .Values.apps.mysql.password }}
        driver-class-name: com.mysql.cj.jdbc.Driver
      boot:
        admin:
          client:
            enabled: false
            url: http://127.0.0.1:8888
            instance:
              service-url: http://127.0.0.1:8080

    druid:
      mysql:
        usePingMethod: false

    management:
      endpoints:
        enabled-by-default: true
        web:
          exposure:
            include: '*'
      endpoint:
        health:
          show-details: always

    live:
      srs:
        rtmp-base: "rtmp://{{ $domain }}:{{ $rtmpPort }}"
        rtsp-base: "rtsp://{{ $domain }}:{{ .Values.apps.rtsp.ports.rtsp }}"
        srt-base: "srt://{{ $domain }}:23333"
        flv-base: "{{ $scheme }}://{{ $domain }}:{{ $httpPort }}"
        rtc-base: "webrtc://{{ $domain }}:{{ $httpPort }}"
    {{- if .Values.enabled.cluster }}
        api-base: "http://{{ $deployName }}-origin-svc:{{ .Values.apps.srs.ports.api }}"
    {{- else }}
        api-base: "http://{{ $deployName }}-rtc-svc:{{ .Values.apps.srs.ports.api }}"
    {{- end }}
      hls:
        max-ts: 200
        interval-ms: 6000
      minio:
        endpoint: {{ .Values.apps.srs.oss.endpoint }}
        access-key: {{ .Values.apps.srs.oss.ak }}
        secret-key: {{ .Values.apps.srs.oss.sk }}
        bucket: {{ .Values.apps.srs.oss.bucket }}
      sync:
        onStart: false
        pool:
          core: 8
          max: 12
          queue: 0
          keepalive: 20
        interval:
          sync: 150
          elect: 8
          keepalive: 20
          heartbeat: 8

    logging:
      level:
        com.cmii.live.srs.mapper: info

---
  {{- end}}
