# -*- coding: utf-8 -*-
from django.test import SimpleTestCase
from mock import patch

from corehq.apps.app_manager.const import CLAIM_DEFAULT_RELEVANT_CONDITION
from corehq.apps.app_manager.models import (
    Application,
    Module,
    CaseSearch,
    CaseSearchProperty,
    DefaultCaseSearchProperty,
    DetailColumn
)
from corehq.apps.app_manager.tests.util import TestXmlMixin, SuiteMixin, parse_normalize

DOMAIN = 'test_domain'


class RemoteRequestSuiteTest(SimpleTestCase, TestXmlMixin, SuiteMixin):
    file_path = ('data', 'suite')

    def setUp(self):
        self.app = Application.new_app(DOMAIN, "Untitled Application")
        self.module = self.app.add_module(Module.new_module("Untitled Module", None))
        self.app.new_form(0, "Untitled Form", None)
        self.module.case_type = 'case'
        # chosen xpath just used to reference more instances - not considered valid to use in apps
        self.module.case_details.short.columns.append(
            DetailColumn(header={"en": "report_name"}, model="case", format="calculate", field="whatever",
                         calc_xpath="instance('reports')/report[1]/name")
        )
        self.module.case_details.long.columns.append(
            DetailColumn(header={"en": "ledger_name"}, model="case", format="calculate", field="whatever",
                         calc_xpath="instance('ledgerdb')/ledgers/name/name")
        )
        self.module.search_config = CaseSearch(
            command_label={'en': 'Search Patients Nationally'},
            properties=[
                CaseSearchProperty(name='name', label={'en': 'Name'}),
                CaseSearchProperty(name='dob', label={'en': 'Date of birth'})
            ],
            relevant="{} and {}".format("instance('groups')/groups/group", CLAIM_DEFAULT_RELEVANT_CONDITION),
            default_properties=[
                DefaultCaseSearchProperty(
                    property=u'ɨŧsȺŧɍȺᵽ',
                    defaultValue=(
                        u"instance('casedb')/case"
                        u"[@case_id='instance('commcaresession')/session/data/case_id']"
                        u"/ɨŧsȺŧɍȺᵽ")
                ),
                DefaultCaseSearchProperty(
                    property='name',
                    defaultValue="instance('locations')/locations/location[@id=123]/@type",
                ),
            ],
        )

    def test_remote_request(self):
        """
        Suite should include remote-request if searching is configured
        """
        with patch('corehq.util.view_utils.get_url_base') as get_url_base_patch:
            get_url_base_patch.return_value = 'https://www.example.com'
            suite = self.app.create_suite()
        self.assertXmlPartialEqual(self.get_xml('remote_request'), suite, "./remote-request[1]")

    def test_case_search_action(self):
        """
        Case search action should be added to case list
        """
        suite = self.app.create_suite()
        self.assertXmlPartialEqual(self.get_xml('search_command_detail'), suite, "./detail[1]")

    def test_case_search_action_relevant_condition(self):
        condition = "'foo' = 'bar'"
        self.module.search_config.search_button_display_condition = condition
        suite = self.app.create_suite()
        suite = parse_normalize(suite, to_string=False)
        self.assertEqual(condition, suite.xpath('./detail[1]/action/@relevant')[0])

@@ -316,16 +316,17 @@
 ilColumn
+,
 %0A)%0Afrom 
@@ -408,16 +408,64 @@
 ormalize
+%0Afrom corehq.apps.builds.models import BuildSpec
 %0A%0ADOMAIN
@@ -682,16 +682,89 @@
 ation%22)%0A
+        self.app.build_spec = BuildSpec(version='tests', build_number=1)%0A
         
@@ -2925,24 +2925,643 @@
 quest%5B1%5D%22)%0A%0A
+    def test_duplicate_remote_request(self):%0A        %22%22%22%0A        Adding a second search config should not affect the initial one.%0A        %22%22%22%0A        # this tests a bug encountered by enishay%0A        copy_app = Application.wrap(self.app.to_json())%0A        copy_app.modules.append(Module.wrap(copy_app.modules%5B0%5D.to_json()))%0A        with patch('corehq.util.view_utils.get_url_base') as get_url_base_patch:%0A            get_url_base_patch.return_value = 'https://www.example.com'%0A            suite = copy_app.create_suite()%0A        self.assertXmlPartialEqual(self.get_xml('remote_request'), suite, %22./remote-request%5B1%5D%22)%0A%0A
     def test

add failing test for instance removal