---
- name: Registering container name
  set_fact:
    pname: "{{ cname_prefix }}"
    cname_1: "{{ cname_prefix ~ '1' }}"
    cname_2: "{{ cname_prefix ~ '2' }}"

####################################################################
## Profiles ########################################################
####################################################################

- block:
    - name: Define service
      set_fact:
        test_service: |
          version: '3'
          services:
            {{ cname_1 }}:
              image: "{{ docker_test_image_alpine }}"
              command: '/bin/sh -c "sleep 10m"'
              profiles:
                - profile_1
                - profile_all
              stop_grace_period: 1s
            {{ cname_2 }}:
              image: "{{ docker_test_image_alpine }}"
              command: '/bin/sh -c "sleep 10m"'
              profiles:
                - profile_2
                - profile_all
              stop_grace_period: 1s
        test_cases:
          - test_name: no services enabled
          - test_name: enable 1
            profiles_value:
              - profile_1
          - test_name: stop all services
            profiles_value:
              - profile_1
            stopped_value: true
          - test_name: enable 2
            profiles_value:
              - profile_2
          - test_name: stop all services
            profiles_value:
              - profile_2
            stopped_value: true
          - test_name: enable both
            profiles_value:
              - profile_1
              - profile_2
          - test_name: stop all services
            profiles_value:
              - profile_1
              - profile_2
            stopped_value: true
          - test_name: enable all
            profiles_value:
              - profile_all

    - name: Profiles ({{ test_case.test_name }})
      docker_compose:
        project_name: "{{ pname }}"
        definition: "{{ test_service | from_yaml }}"
        profiles: "{{ test_case.profiles_value | default(omit) }}"
        stopped: "{{ test_case.stopped_value | default(omit) }}"
        state: present
      register: profiles_outputs
      loop: "{{ test_cases }}"
      loop_control:
        loop_var: test_case

    - name: Cleanup
      docker_compose:
        project_name: "{{ pname }}"
        state: absent
        definition: "{{ test_service | from_yaml }}"

    - assert:
        that:
          - profiles_outputs.results[0] is not changed
          - profiles_outputs.results[1].services[cname_1][cname_1_name].state.running
          - profiles_outputs.results[1].services[cname_2] == {}
          - not profiles_outputs.results[2].services[cname_1][cname_1_name].state.running
          - profiles_outputs.results[2].services[cname_2] == {}
          - not profiles_outputs.results[3].services[cname_1][cname_1_name].state.running
          - profiles_outputs.results[3].services[cname_2][cname_2_name].state.running
          - not profiles_outputs.results[4].services[cname_1][cname_1_name].state.running
          - not profiles_outputs.results[4].services[cname_2][cname_2_name].state.running
          - profiles_outputs.results[5].services[cname_1][cname_1_name].state.running
          - profiles_outputs.results[5].services[cname_2][cname_2_name].state.running
          - not profiles_outputs.results[6].services[cname_1][cname_1_name].state.running
          - not profiles_outputs.results[6].services[cname_2][cname_2_name].state.running
          - profiles_outputs.results[7].services[cname_1][cname_1_name].state.running
          - profiles_outputs.results[7].services[cname_2][cname_2_name].state.running
      vars:
        cname_1_name: "{{ pname + '_' + cname_1 + '_1' }}"
        cname_2_name: "{{ pname + '_' + cname_2 + '_1' }}"
  when: docker_compose_version is version('1.28.0', '>=')

####################################################################
## Env_file ########################################################
####################################################################

- block:
    - name: Define service and files
      set_fact:
        compose_file: "{{ remote_tmp_dir }}/docker-compose.yml"
        env_file: "{{ remote_tmp_dir }}/.env"
        env_sleep_cmd: sleep 10m
        new_env_file: "{{ remote_tmp_dir }}/new.env"
        new_env_sleep_cmd: sleep 20m
        test_service: |
          version: '3'
          services:
            {{ cname_1 }}:
              image: "{{ docker_test_image_alpine }}"
              command: '/bin/sh -c "${SLEEP_CMD}"'
              stop_grace_period: 1s

    - name: Define testcases
      set_fact:
        test_cases:
          - test_name: Without env_file option
          - test_name: With env_file option
            env_file: "{{ new_env_file }}"

    - name: Generate compose file
      ansible.builtin.copy:
        content: "{{ test_service }}"
        dest: "{{ compose_file }}"

    - name: Generate .env file
      ansible.builtin.copy:
        content: |
          SLEEP_CMD="{{ env_sleep_cmd }}"
        dest: "{{ env_file }}"

    - name: Generate new.env file
      ansible.builtin.copy:
        content: |
          SLEEP_CMD="{{ new_env_sleep_cmd }}"
        dest: "{{ new_env_file }}"

    - name: Env_file
      docker_compose:
        project_name: "{{ pname }}"
        project_src: "{{ remote_tmp_dir }}"
        env_file: "{{ test_case.env_file | default(omit) }}"
      register: env_file_outputs
      loop: "{{ test_cases }}"
      loop_control:
        loop_var: test_case

    - name: Cleanup
      docker_compose:
        project_name: "{{ pname }}"
        state: absent
        definition: "{{ test_service | from_yaml }}"

    - assert:
        that:
          - "env_sleep_cmd is in (env_file_outputs.results[0].services[cname_1][cname_1_name].cmd | join(' '))"
          - "new_env_sleep_cmd is in (env_file_outputs.results[1].services[cname_1][cname_1_name].cmd | join(' '))"
      vars:
        cname_1_name: "{{ pname + '_' + cname_1 + '_1' }}"
        cname_2_name: "{{ pname + '_' + cname_2 + '_1' }}"

    - name: Remove files
      ansible.builtin.file:
        path: "{{ file_path }}"
        state: absent
      loop_control:
        loop_var: file_path
      loop:
        - "{{ compose_file }}"
        - "{{ env_file }}"
        - "{{ new_env_file }}"
  when: docker_compose_version is version('1.25.0', '>=')

####################################################################
## Project_src #####################################################
####################################################################

- name: Define service and files
  set_fact:
    compose_file: "{{ remote_tmp_dir }}/docker-compose.yml"
    env_sleep_cmd: sleep 10m
    new_env_sleep_cmd: sleep 20m
    test_service: |
      version: '3'
      services:
        {{ cname_1 }}:
          image: "{{ docker_test_image_alpine }}"
          command: '/bin/sh -c 10m'
          stop_grace_period: 1s

- name: Generate compose file
  ansible.builtin.copy:
    content: "{{ test_service }}"
    dest: "{{ compose_file }}"

- name: Start with project_src
  docker_compose:
    project_src: "{{ remote_tmp_dir }}"
  register: project_src_1

- name: Start with project_src (idempotent)
  docker_compose:
    project_src: "{{ remote_tmp_dir }}"
  register: project_src_2

- name: Stop with project_src
  docker_compose:
    project_src: "{{ remote_tmp_dir }}"
    state: absent
  register: project_src_3

- name: Stop with project_src (idempotent)
  docker_compose:
    project_src: "{{ remote_tmp_dir }}"
    state: absent
  register: project_src_4

- name: Remove files
  ansible.builtin.file:
    path: "{{ file_path }}"
    state: absent
  loop_control:
    loop_var: file_path
  loop:
    - "{{ compose_file }}"

- assert:
    that:
      - project_src_1 is changed
      # - project_src_2 is not changed    -- for some reason, this currently fails!
      - project_src_3 is changed
      - project_src_4 is not changed
