from __future__ import absolute_import
from __future__ import print_function
import os
from corehq.apps.es.case_search import CaseSearchES
from corehq.apps.es import queries

from custom.enikshay.case_utils import (
    CASE_TYPE_EPISODE,
    get_person_case_from_episode,
    get_occurrence_case_from_episode,
)
from custom.enikshay.const import (
    ENROLLED_IN_PRIVATE,
)
from custom.enikshay.management.commands.base_data_dump import BaseDataDump

DOMAIN = "enikshay"


class Command(BaseDataDump):
    """
    1. Episode DRTB cases
    https://docs.google.com/spreadsheets/d/1OPp0oFlizDnIyrn7Eiv11vUp8IBmc73hES7qqT-mKKA/edit#gid=1091819174
    """
    def __init__(self, *args, **kwargs):
        super(Command, self).__init__(*args, **kwargs)
        self.case_type = CASE_TYPE_EPISODE
        self.input_file_name = os.path.join(os.path.dirname(__file__),
                                            'data_dumps_drtb_episodes.csv')

    def get_case_ids(self, case_type):
        """
        All open and closed episode cases whose host/host = a person case (open
        or closed) with person.dataset = 'real' and person.enrolled_in_private
        != 'true'
        """
        return (CaseSearchES()
                .domain(DOMAIN)
                .case_type(case_type)
                .case_property_query(ENROLLED_IN_PRIVATE, 'true', clause=queries.MUST_NOT)
                .case_property_query("episode_type", "confirmed_drtb", clause=queries.MUST)
                .get_ids()[0:10])

    def include_case_in_dump(self, episode):
        person = self.get_person(episode)
        return person and person.get_case_property('dataset') == 'real'

    def get_custom_value(self, column_name, episode):
        if column_name == "Current Treating Facility - District":
            if not episode.closed and episode.get_case_property('is_active', '') == 'yes':
                return self.get_person(episode).get_case_property('dto_id')
            else:
                return ''
        elif column_name == "Current Treating Facility - District Name":
            if not episode.closed and episode.get_case_property('is_active', '') == 'yes':
                return self.get_person(episode).get_case_property('dto_name')
            else:
                return ''
        elif column_name == "Current Treating Facility - TU":
            if not episode.closed and episode.get_case_property('is_active', '') == 'yes':
                return self.get_person(episode).get_case_property('tu_id')
            else:
                return ''
        elif column_name == "Current Treating Facility- TU Name":
            if not episode.closed and episode.get_case_property('is_active', '') == 'yes':
                return self.get_person(episode).get_case_property('tu_name')
            else:
                return ''
        elif column_name == "Current Treating Facility - PHI":
            if not episode.closed and episode.get_case_property('is_active', '') == 'yes':
                return self.get_person(episode).owner_id
            else:
                return ''
        elif column_name == "Current Owner - PHI Name":
            if episode.closed and episode.get_case_property('is_active', '') == 'yes':
                return self.get_person(episode).get_case_property('phi_name')
            else:
                return ''
        return Exception("unknown custom column %s" % column_name)

    def get_person(self, episode):
        if 'person' not in self.context:
            self.context['person'] = get_person_case_from_episode(DOMAIN, episode.case_id)
        return self.context['person']

    def get_occurrence(self, episode):
        if 'occurrence' not in self.context:
            self.context['occurrence'] = get_occurrence_case_from_episode(DOMAIN, episode.case_id)
        return self.context['occurrence']

    def get_case_reference_value(self, case_reference, episode, calculation):
        if case_reference == 'person':
            return self.get_person(episode).get_case_property(calculation)
        elif case_reference == 'occurrence':
            return self.get_occurrence(episode).get_case_property(calculation)
        return Exception("unknown case reference %s" % case_reference)

    def handle(self, *args, **options):
        self.setup()
        self.generate_dump()

@@ -1840,36 +1840,32 @@
 erty('is_active'
-, ''
 ) == 'yes':%0A    
@@ -1852,32 +1852,32 @@
 ive') == 'yes':%0A
+
                 
@@ -2120,36 +2120,32 @@
 erty('is_active'
-, ''
 ) == 'yes':%0A    
@@ -2391,36 +2391,32 @@
 erty('is_active'
-, ''
 ) == 'yes':%0A    
@@ -2584,16 +2584,17 @@
 Facility
+ 
 - TU Nam
@@ -2664,36 +2664,32 @@
 erty('is_active'
-, ''
 ) == 'yes':%0A    
@@ -2935,36 +2935,32 @@
 erty('is_active'
-, ''
 ) == 'yes':%0A    
@@ -3104,32 +3104,32 @@
 er - PHI Name%22:%0A
+
             if e
@@ -3187,12 +3187,8 @@
 ive'
-, ''
 ) ==

fix arguments to case property and column name [skip ci]