# -*- coding: utf-8 -*-
"""
Profile: http://hl7.org/fhir/StructureDefinition/ImmunizationRecommendation
Release: STU3
Version: 3.0.2
Revision: 11917
Last updated: 2019-10-24T11:53:00+11:00
"""

import io
import json
import os
import unittest

import pytest

from .. import immunizationrecommendation
from ..fhirdate import FHIRDate
from .fixtures import force_bytes


@pytest.mark.usefixtures("base_settings")
class ImmunizationRecommendationTests(unittest.TestCase):
    def instantiate_from(self, filename):
        datadir = os.environ.get("FHIR_UNITTEST_DATADIR") or ""
        with io.open(os.path.join(datadir, filename), "r", encoding="utf-8") as handle:
            js = json.load(handle)
            self.assertEqual("ImmunizationRecommendation", js["resourceType"])
        return immunizationrecommendation.ImmunizationRecommendation(js)

    def testImmunizationRecommendation1(self):
        inst = self.instantiate_from("immunizationrecommendation-example.json")
        self.assertIsNotNone(
            inst, "Must have instantiated a ImmunizationRecommendation instance"
        )
        self.implImmunizationRecommendation1(inst)

        js = inst.as_json()
        self.assertEqual("ImmunizationRecommendation", js["resourceType"])
        inst2 = immunizationrecommendation.ImmunizationRecommendation(js)
        self.implImmunizationRecommendation1(inst2)

    def implImmunizationRecommendation1(self, inst):
        self.assertEqual(force_bytes(inst.id), force_bytes("example"))
        self.assertEqual(
            force_bytes(inst.identifier[0].system), force_bytes("urn:ietf:rfc:3986")
        )
        self.assertEqual(
            force_bytes(inst.identifier[0].value),
            force_bytes("urn:oid:1.3.6.1.4.1.21367.2005.3.7.1235"),
        )
        self.assertEqual(
            inst.recommendation[0].date.date,
            FHIRDate("2015-02-09T11:04:15.817-05:00").date,
        )
        self.assertEqual(
            inst.recommendation[0].date.as_json(), "2015-02-09T11:04:15.817-05:00"
        )
        self.assertEqual(
            force_bytes(inst.recommendation[0].dateCriterion[0].code.coding[0].code),
            force_bytes("earliest"),
        )
        self.assertEqual(
            force_bytes(inst.recommendation[0].dateCriterion[0].code.coding[0].display),
            force_bytes("Earliest Date"),
        )
        self.assertEqual(
            force_bytes(inst.recommendation[0].dateCriterion[0].code.coding[0].system),
            force_bytes(
                "http://hl7.org/fhir/immunization-recommendation-date-criterion"
            ),
        )
        self.assertEqual(
            inst.recommendation[0].dateCriterion[0].value.date,
            FHIRDate("2015-12-01T00:00:00-05:00").date,
        )
        self.assertEqual(
            inst.recommendation[0].dateCriterion[0].value.as_json(),
            "2015-12-01T00:00:00-05:00",
        )
        self.assertEqual(
            force_bytes(inst.recommendation[0].dateCriterion[1].code.coding[0].code),
            force_bytes("recommended"),
        )
        self.assertEqual(
            force_bytes(inst.recommendation[0].dateCriterion[1].code.coding[0].display),
            force_bytes("Recommended"),
        )
        self.assertEqual(
            force_bytes(inst.recommendation[0].dateCriterion[1].code.coding[0].system),
            force_bytes(
                "http://hl7.org/fhir/immunization-recommendation-date-criterion"
            ),
        )
        self.assertEqual(
            inst.recommendation[0].dateCriterion[1].value.date,
            FHIRDate("2015-12-01T00:00:00-05:00").date,
        )
        self.assertEqual(
            inst.recommendation[0].dateCriterion[1].value.as_json(),
            "2015-12-01T00:00:00-05:00",
        )
        self.assertEqual(
            force_bytes(inst.recommendation[0].dateCriterion[2].code.coding[0].code),
            force_bytes("overdue"),
        )
        self.assertEqual(
            force_bytes(inst.recommendation[0].dateCriterion[2].code.coding[0].display),
            force_bytes("Past Due Date"),
        )
        self.assertEqual(
            force_bytes(inst.recommendation[0].dateCriterion[2].code.coding[0].system),
            force_bytes(
                "http://hl7.org/fhir/immunization-recommendation-date-criterion"
            ),
        )
        self.assertEqual(
            inst.recommendation[0].dateCriterion[2].value.date,
            FHIRDate("2016-12-28T00:00:00-05:00").date,
        )
        self.assertEqual(
            inst.recommendation[0].dateCriterion[2].value.as_json(),
            "2016-12-28T00:00:00-05:00",
        )
        self.assertEqual(inst.recommendation[0].doseNumber, 1)
        self.assertEqual(
            force_bytes(inst.recommendation[0].forecastStatus.text),
            force_bytes("Not Complete"),
        )
        self.assertEqual(
            force_bytes(inst.recommendation[0].protocol.description),
            force_bytes("First sequence in protocol"),
        )
        self.assertEqual(inst.recommendation[0].protocol.doseSequence, 1)
        self.assertEqual(
            force_bytes(inst.recommendation[0].protocol.series),
            force_bytes("Vaccination Series 1"),
        )
        self.assertEqual(
            force_bytes(inst.recommendation[0].vaccineCode.coding[0].code),
            force_bytes("14745005"),
        )
        self.assertEqual(
            force_bytes(inst.recommendation[0].vaccineCode.coding[0].display),
            force_bytes("Hepatitis A vaccine"),
        )
        self.assertEqual(
            force_bytes(inst.recommendation[0].vaccineCode.coding[0].system),
            force_bytes("http://snomed.info/sct"),
        )
        self.assertEqual(
            force_bytes(inst.text.div),
            force_bytes(
                '<div xmlns="http://www.w3.org/1999/xhtml">Authored by Joginder Madra</div>'
            ),
        )
        self.assertEqual(force_bytes(inst.text.status), force_bytes("generated"))

    def testImmunizationRecommendation2(self):
        inst = self.instantiate_from(
            "immunizationrecommendation-target-disease-example.json"
        )
        self.assertIsNotNone(
            inst, "Must have instantiated a ImmunizationRecommendation instance"
        )
        self.implImmunizationRecommendation2(inst)

        js = inst.as_json()
        self.assertEqual("ImmunizationRecommendation", js["resourceType"])
        inst2 = immunizationrecommendation.ImmunizationRecommendation(js)
        self.implImmunizationRecommendation2(inst2)

    def implImmunizationRecommendation2(self, inst):
        self.assertEqual(force_bytes(inst.id), force_bytes("example"))
        self.assertEqual(
            force_bytes(inst.identifier[0].system), force_bytes("urn:ietf:rfc:3986")
        )
        self.assertEqual(
            force_bytes(inst.identifier[0].value),
            force_bytes("urn:oid:1.3.6.1.4.1.21367.2005.3.7.1235"),
        )
        self.assertEqual(
            inst.recommendation[0].date.date,
            FHIRDate("2015-02-09T11:04:15.817-05:00").date,
        )
        self.assertEqual(
            inst.recommendation[0].date.as_json(), "2015-02-09T11:04:15.817-05:00"
        )
        self.assertEqual(
            force_bytes(inst.recommendation[0].dateCriterion[0].code.coding[0].code),
            force_bytes("earliest"),
        )
        self.assertEqual(
            force_bytes(inst.recommendation[0].dateCriterion[0].code.coding[0].display),
            force_bytes("Earliest Date"),
        )
        self.assertEqual(
            force_bytes(inst.recommendation[0].dateCriterion[0].code.coding[0].system),
            force_bytes(
                "http://hl7.org/fhir/immunization-recommendation-date-criterion"
            ),
        )
        self.assertEqual(
            inst.recommendation[0].dateCriterion[0].value.date,
            FHIRDate("2015-12-01T00:00:00-05:00").date,
        )
        self.assertEqual(
            inst.recommendation[0].dateCriterion[0].value.as_json(),
            "2015-12-01T00:00:00-05:00",
        )
        self.assertEqual(
            force_bytes(inst.recommendation[0].dateCriterion[1].code.coding[0].code),
            force_bytes("recommended"),
        )
        self.assertEqual(
            force_bytes(inst.recommendation[0].dateCriterion[1].code.coding[0].display),
            force_bytes("Recommended"),
        )
        self.assertEqual(
            force_bytes(inst.recommendation[0].dateCriterion[1].code.coding[0].system),
            force_bytes(
                "http://hl7.org/fhir/immunization-recommendation-date-criterion"
            ),
        )
        self.assertEqual(
            inst.recommendation[0].dateCriterion[1].value.date,
            FHIRDate("2015-12-01T00:00:00-05:00").date,
        )
        self.assertEqual(
            inst.recommendation[0].dateCriterion[1].value.as_json(),
            "2015-12-01T00:00:00-05:00",
        )
        self.assertEqual(
            force_bytes(inst.recommendation[0].dateCriterion[2].code.coding[0].code),
            force_bytes("overdue"),
        )
        self.assertEqual(
            force_bytes(inst.recommendation[0].dateCriterion[2].code.coding[0].display),
            force_bytes("Past Due Date"),
        )
        self.assertEqual(
            force_bytes(inst.recommendation[0].dateCriterion[2].code.coding[0].system),
            force_bytes(
                "http://hl7.org/fhir/immunization-recommendation-date-criterion"
            ),
        )
        self.assertEqual(
            inst.recommendation[0].dateCriterion[2].value.date,
            FHIRDate("2016-12-28T00:00:00-05:00").date,
        )
        self.assertEqual(
            inst.recommendation[0].dateCriterion[2].value.as_json(),
            "2016-12-28T00:00:00-05:00",
        )
        self.assertEqual(inst.recommendation[0].doseNumber, 1)
        self.assertEqual(
            force_bytes(inst.recommendation[0].forecastStatus.text),
            force_bytes("Not Complete"),
        )
        self.assertEqual(
            force_bytes(inst.recommendation[0].protocol.description),
            force_bytes("First sequence in protocol"),
        )
        self.assertEqual(inst.recommendation[0].protocol.doseSequence, 1)
        self.assertEqual(
            force_bytes(inst.recommendation[0].protocol.series),
            force_bytes("Vaccination Series 1"),
        )
        self.assertEqual(
            force_bytes(inst.recommendation[0].targetDisease.coding[0].code),
            force_bytes("40468003"),
        )
        self.assertEqual(
            force_bytes(inst.recommendation[0].targetDisease.coding[0].system),
            force_bytes("http://snomed.info/sct"),
        )
        self.assertEqual(
            force_bytes(inst.text.div),
            force_bytes(
                '<div xmlns="http://www.w3.org/1999/xhtml">Authored by Joginder Madra</div>'
            ),
        )
        self.assertEqual(force_bytes(inst.text.status), force_bytes("generated"))
