from zrb import (
    DockerComposeTask, Env, EnvFile, HTTPChecker, ServiceConfig, runner
)
from zrb.builtin._group import project_group
from ._common import (
    RESOURCE_DIR, APP_DIR, local_input, host_input, https_input
)
from .image import (
    image_input, image_env, build_snake_app_name_image
)
import os

###############################################################################
# Env Definitions
###############################################################################

host_port_env = Env(
    name='HOST_PORT',
    os_name='CONTAINER_ENV_PREFIX_HOST_PORT',
    default='httpPort'
)

###############################################################################
# Service Config Definitions
###############################################################################

snake_app_name_service_config = ServiceConfig(
    env_files=[
        EnvFile(
            env_file=os.path.join(APP_DIR, 'template.env'),
            prefix='CONTAINER_ENV_PREFIX'
        )
    ]
)

###############################################################################
# Task Definitions
###############################################################################

remove_snake_app_name_container = DockerComposeTask(
    icon='💨',
    name='remove-kebab-app-name-container',
    description='Rumove human readable app name container',
    group=project_group,
    cwd=RESOURCE_DIR,
    compose_cmd='down',
    compose_env_prefix='CONTAINER_ENV_PREFIX',
    compose_service_configs={
        'snake_app_name': snake_app_name_service_config
    },
    envs=[
        image_env,
        host_port_env,
    ],
)
runner.register(remove_snake_app_name_container)

stop_snake_app_name_container = DockerComposeTask(
    icon='⛔',
    name='stop-kebab-app-name-container',
    description='Stop human readable app name container',
    group=project_group,
    cwd=RESOURCE_DIR,
    compose_cmd='stop',
    compose_env_prefix='CONTAINER_ENV_PREFIX',
    compose_service_configs={
        'snake_app_name': snake_app_name_service_config
    },
    envs=[
        image_env,
        host_port_env,
    ],
)
runner.register(stop_snake_app_name_container)

init_snake_app_name_container = DockerComposeTask(
    icon='🔥',
    name='init-kebab-app-name-container',
    inputs=[
        local_input,
        host_input,
        image_input,
    ],
    skip_execution='{{not input.local_snake_app_name}}',
    upstreams=[
        build_snake_app_name_image,
        remove_snake_app_name_container
    ],
    cwd=RESOURCE_DIR,
    compose_cmd='up',
    compose_flags=['-d'],
    compose_env_prefix='CONTAINER_ENV_PREFIX',
    compose_service_configs={
        'snake_app_name': snake_app_name_service_config
    },
    envs=[
        image_env,
        host_port_env,
    ],
)

start_snake_app_name_container = DockerComposeTask(
    icon='🐳',
    name='start-kebab-app-name-container',
    description='Start human readable app name container',
    group=project_group,
    inputs=[
        local_input,
        host_input,
        https_input,
        image_input,
    ],
    skip_execution='{{not input.local_snake_app_name}}',
    upstreams=[init_snake_app_name_container],
    cwd=RESOURCE_DIR,
    compose_cmd='logs',
    compose_flags=['-f'],
    compose_env_prefix='CONTAINER_ENV_PREFIX',
    compose_service_configs={
        'snake_app_name': snake_app_name_service_config
    },
    envs=[
        image_env,
        host_port_env,
    ],
    checkers=[
        HTTPChecker(
            name='check-kebab-app-name',
            host='{{input.snake_app_name_host}}',
            port='{{env.HOST_PORT}}',
            is_https='{{input.snake_app_name_https}}'
        )
    ]
)
runner.register(start_snake_app_name_container)
