# -*- coding: utf-8 -*-
import datetime
import logging

import click
import yaml

from scout.load import load_scout
from scout.exceptions import IntegrityError, ConfigError

logger = logging.getLogger(__name__)


@click.command('case', short_help='Load a case')
@click.option('--vcf', type=click.Path(exists=True),
              help='path to clinical VCF file to be loaded')
@click.option('--vcf-sv', type=click.Path(exists=True),
              help='path to clinical SV VCF file to be loaded')
@click.option('--owner', help='parent institute for the case')
@click.option('--ped', type=click.File('r'))
@click.option('-u', '--update', is_flag=True)
@click.argument('config', type=click.File('r'), required=False)
@click.pass_context
def case(context, vcf, vcf_sv, owner, ped, update, config):
    """Load a case into the database"""
    if config is None and ped is None:
        click.echo("you have to provide either config or ped file")
        context.abort()

    config_data = yaml.load(config) if config else {}

    if not config_data:
        config_data['analysis_date'] = datetime.date.today()

    config_data['vcf_snv'] = vcf if vcf else config_data.get('vcf_snv')
    config_data['vcf_sv'] = vcf_sv if vcf_sv else config_data.get('vcf_sv')
    config_data['owner'] = owner if owner else config_data.get('owner')
    config_data['rank_threshold'] = config_data.get('rank_threshold') or 5

    if not (config_data.get('vcf_snv') or config_data.get('vcf_sv')):
        logger.warn("Please provide a vcf file (use '--vcf')")
        context.abort()

    if not config_data.get('owner'):
        logger.warn("Please provide an owner for the case (use '--owner')")
        context.abort()

    try:
        load_scout(context.obj['adapter'], config_data, ped=ped, update=update)
    except (IntegrityError, ValueError, ConfigError, KeyError) as error:
        click.echo(error)
        context.abort()


@click.command('delete_case', short_help='Delete a case')
@click.option('-i', '--institute', help='institute id of related cases')
@click.option('-c', '--case_id')
@click.pass_context
def delete_case(context, institute, case_id):
    """Delete a case and it's variants from the database"""
    adapter = context.obj['adapter']

    if not case_id:
        click.echo("Please specify the id of the case that should be "
                       "deleted with flag '-c/--case_id'.")
        context.abort()

    if not institute:
        click.echo("Please specify the owner of the case that should be "
                       "deleted with flag '-i/--institute'.")
        context.abort()

    logger.info("Running deleting case {0}".format(case_id))

    case = adapter.delete_case(
        institute_id=institute,
        case_id=case_id
    )

    if case:
        adapter.delete_variants(case_id=case.case_id, variant_type='clinical')
        adapter.delete_variants(case_id=case.case_id, variant_type='research')
    else:
        logger.warning("Case does not exist in database")
        context.abort()


@click.command('cases', short_help='Fetch cases')
@click.option('-i', '--institute',
              help='institute id of related cases'
)
@click.option('-r', '--reruns',
              is_flag=True,
              help='requested to be rerun'
)
@click.option('-f', '--finished',
              is_flag=True,
              help='archived or solved'
)
@click.option('--causatives',
              is_flag=True,
              help='Has causative variants'
)
@click.option('--research-requested',
              is_flag=True,
              help='If research is requested'
)
@click.option('--is-research',
              is_flag=True,
              help='If case is in research mode'
)
@click.pass_context
def cases(context, institute, reruns, finished, causatives, research_requested,
          is_research):
    """Interact with cases existing in the database."""
    adapter = context.obj['adapter']

    models = adapter.cases(collaborator=institute, reruns=reruns,
                           finished=finished, has_causatives=causatives,
                           research_requested=research_requested,
                           is_research=is_research)
    for model in models:
        click.echo(model.case_id)

@@ -170,19 +170,16 @@
 ror%0A%0Alog
-ger
  = loggi
@@ -1011,16 +1011,472 @@
 lse %7B%7D%0A%0A
+    # check if the analysis is from a newer analysis%0A    adapter = context.obj%5B'adapter'%5D%0A    existing_case = adapter.case(config_data%5B'owner'%5D, config_data%5B'family'%5D)%0A    if existing_case:%0A        new_analysisdate = config_data.get('analysis_date')%0A        if new_analysisdate and new_analysisdate %3E existing_case.analysis_date:%0A            log.info(%22updated analysis - updating existing case%22)%0A            # update by default!%0A            update = True%0A%0A
     if n
@@ -1922,35 +1922,32 @@
 ')):%0A        log
-ger
 .warn(%22Please pr
@@ -2048,27 +2048,24 @@
 %0A        log
-ger
 .warn(%22Pleas
@@ -2167,38 +2167,23 @@
 d_scout(
-context.obj%5B'
 adapter
-'%5D
 , config
@@ -3032,19 +3032,16 @@
 %0A    log
-ger
 .info(%22R
@@ -3371,19 +3371,16 @@
      log
-ger
 .warning

update new analyses by default