#!python
# -*- coding: utf-8 -*-

import logging
import os
import sys
import platform
import subprocess
import click
from cleepcli.git import Git
from cleepcli.module import Module
from cleepcli.file import File
from cleepcli.watch import CleepWatchdog
from cleepcli.test import Test
from cleepcli.distrib import Distrib
import cleepcli.config as config

logging.basicConfig(level=logging.INFO, format=u'%(message)s', stream=sys.stdout)

@click.group()
def core():
    pass

@core.command()
def coreget():
    """ 
    Get or update core content from official repository
    """
    g = Git()
    if not os.path.exists(config.CORE_SRC):
        res = g.clone_core()
    else:
        res = g.pull_core()

    if not res:
        sys.exit(1)
    sys.exit(0)

@core.command()
def coresync():
    """
    Synchronize core content between source and execution folders
    """
    f = File()
    res = f.core_sync()

    if not res:
        sys.exit(1)
    sys.exit(0)

@click.group()
def mod():
    pass

@mod.command()
@click.option('--mod', required=True, help='Module name.')
def modsync(mod):
    """
    Synchronize core content between source and execution folders
    """
    f = File()
    res = f.module_sync(mod)

    if not res:
        sys.exit(1)
    sys.exit(0)

@mod.command()
@click.option('--name', prompt='Module name')
def modcreate(name):
    """
    Create new module skeleton
    """
    m = Module()
    res = m.create(name)

    if not res:
        sys.exit(1)
    sys.exit(0)

@click.group()
def watchdog():
    pass

@watchdog.command()
@click.option('--quiet', is_flag=True, help='Disable logging.')
@click.option('--loglevel', default=logging.INFO, help='Logging level (10=DEBUG, 20=INFO, 30=WARN, 40=ERROR).')
def watch(quiet, loglevel):
    """
    Start watchdog that monitors filesystem changes on Cleep sources
    """
    if quiet:
        logging.disable(logging.CRITICAL)
    else:
        logging.getLogger().setLevel(loglevel)
        
    w = CleepWatchdog()
    res = w.watch()

    if not res:
        sys.exit(1)
    sys.exit(0)

@click.group()
def test():
    pass

@test.command()
@click.option('--name', prompt='Module name')
@click.option('--coverage', is_flag=True, help='Display coverage report.')
def modtest(name, coverage):
    """
    Execute module tests
    """
    m = Test()
    res = m.module_test(name, coverage)

    if not res:
        sys.exit(1)
    sys.exit(0)

@test.command()
@click.option('--name', prompt='Module name')
@click.option('--missing', is_flag=True, help='Display missing statements.')
def modtestcov(name, missing):
    """
    Display module test coverage summary
    """
    m = Test()
    res = m.module_test_coverage(name, missing)

    if not res:
        sys.exit(1)
    sys.exit(0)

@click.group()
def distrib():
    pass

@distrib.command()
def build():
    """
    Build Cleep debian package
    """
    d = Distrib()
    res = d.build_cleep()

    if not res:
        sys.exit(1)
    sys.exit(0)

@click.group()
def general():
    pass

@general.command()
def version():
    """
    Display cleep-cli version
    """
    click.echo(config.VERSION)
    sys.exit(0)


cli = click.CommandCollection(sources=[general, core, mod, watchdog, test, distrib])
if __name__ == '__main__':

    def is_raspbian():
        if platform.system()!='Linux':
            return False
        res = subprocess.Popen(u'cat /etc/os-release | grep -i raspbian | wc -l', stdout=subprocess.PIPE, shell=True)
        stdout = res.communicate()[0]
        if stdout.strip()=='0':
            return False
        return True

    #execute only on raspbian
    if is_raspbian():
        cli()
    else:
        click.echo('Cleep-cli runs only on raspbian distribution')

