#!/usr/bin/env python3

# Copyright (c) 2020 cloudover.io
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import os
os.environ['DINEMIC_CONFIG'] = '/etc/starsheep/config.dinemic'

import dinemic
import logging
import sys
import time
import urllib.request
import yaml

from starsheep.context import Context
from starsheep.listeners.reject_listener import RejectListener
from starsheep.command_line import get_url, load_application, import_application, import_data, apply_data, check_version


if __name__ == "__main__":
    options = sys.argv[1:]

    if len(options) <= 0:
        print('Use ' + sys.argv[0] + ' [options] yaml_file')
        sys.exit(1)

    if '--verbose' in sys.argv:
        options.remove('--verbose')
        logging.basicConfig(level=logging.DEBUG)
        dinemic.set_loglevel('debug')
    if '--debug' in sys.argv:
        options.remove('--debug')
        logging.basicConfig(level=logging.INFO)
        dinemic.set_loglevel('info')
    else:
        logging.basicConfig(level=logging.ERROR)
        dinemic.set_loglevel('none')

    if '--no-data' in options:
        load_data = False
        options.remove('--no-data')
    else:
        load_data = True

    if '--only-data' in options:
        only_data = True
        options.remove('--only-data')
    else:
        only_data = False

    if '--launch' in options:
        launch = True
        options.remove('--launch')
    else:
        launch = False

    dinemic.prepare()

    ctx = Context()
    ctx.cmdline = options

    f = urllib.request.urlopen(get_url(options[-1]))
    document = yaml.safe_load(f)
    f.close()

    listener_read = RejectListener('reject_unauthorized_read_authorized_keys',
                              {'reason': 'not authorized',
                               'action': 'reject',
                               'trigger': '*:list_read_authorized_objects*',
                               'call_on': ['unauthorized_update', 'unauthorized_delete', 'unauthorized_remove']},
                              ctx)
    listener_update = RejectListener('reject_unauthorized_update_authorized_keys',
                              {'reason': 'not authorized',
                               'action': 'reject',
                               'trigger': '*:list_update_authorized_objects*',
                               'call_on': ['unauthorized_update', 'unauthorized_delete', 'unauthorized_remove']},
                              ctx)

    check_version(document, ctx)
    import_application(document, ctx)
    load_application(document, ctx)
    import_data(document, ctx)

    # TODO: Add CRUD
    # TODO: Add launch mode

    if launch:
        logging.info('Starting launch mode')
        dinemic.launch()

        if load_data:
            import_data(document, ctx)
            apply_data(document, ctx)
        if only_data:
            sys.exit(0)

        while True:
            time.sleep(1)
