#!/usr/bin/env python
# coding=utf-8

# Copyright (c) 2001, Canal TP and/or its affiliates. All rights reserved.
#
# This file is part of Navitia,
#     the software to build cool stuff with public transport.
#
# Hope you'll enjoy and contribute to this project,
#     powered by Canal TP (www.canaltp.fr).
# Help us simplify mobility and open public transport:
#     a non ending quest to the responsive locomotion way of traveling!
#
# LICENCE: This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Stay tuned using
# twitter @navitia
# [matrix] channel #navitia:matrix.org (https://app.element.io/#/room/#navitia:matrix.org)
# https://groups.google.com/d/forum/navitia
# www.navitia.io
from logging import Logger
from typing import Type

import kirin
from kirin import app, new_relic
from kirin.core.abstract_builder import AbstractKirinModelBuilder
from kirin.queue_workers import AbstractWorker, QueueWorkerLauncher
from kirin.queue_workers.siri_et_xml_tn.siri_et_xml_tn_model_maker import KirinModelBuilder

import logging


logger = logging.getLogger(__name__)


class SiriEtXmlTnWorker(AbstractWorker):
    @classmethod
    def get_builder_class(cls) -> Type[AbstractKirinModelBuilder]:
        return KirinModelBuilder

    @classmethod
    def get_logger(cls) -> Logger:
        return logger

    @new_relic.agent.background_task(name="siri_et_xml_tn_worker-init", group="Task")
    def __init__(self, contributor):
        super().__init__(contributor)

    @new_relic.agent.background_task(name="siri_et_xml_tn_worker-enter", group="Task")
    def __enter__(self):
        return super().__enter__()

    @new_relic.agent.background_task(name="siri_et_xml_tn_worker-process_message", group="Task")
    def process_message(self, body, message):
        super().process_message(body, message)

    @new_relic.agent.background_task(name="siri_et_xml_tn_worker-on_iteration", group="Task")
    def on_iteration(self):
        super().on_iteration()


@app.cli.command("siri_et_xml_tn_worker")
def siri_et_xml_tn_worker():
    # We assume a maximum of one SIRI_ET_XML_TN contributor is going to exist in the DB
    # Therefore, we enforce a maximum of one `SiriEtXmlTnWorker` is running at a time

    launcher = QueueWorkerLauncher(kirin.queue_workers.siri_et_xml_tn.siri_et_xml_tn_worker.SiriEtXmlTnWorker)
    launcher.process_loop()
