#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Tests for the QCSchema mixin of the class."""

import json
import pprint  # noqa: F401
import pytest  # noqa: F401


def test_water(H2O):
    """Test creating QCSchema for water."""
    correct = {
        "schema_name": "qcschema_molecule",
        "schema_version": 2,
        "symbols": ["O", "H", "H"],
        "geometry": [0.0, 0.0, 0.0, 1.430429, 0.0, 1.107157, -1.430429, 0.0, 1.107157],
        "molecular_charge": 0,
        "molecular_multiplicity": 1,
        "connectivity": [[0, 1, 1], [0, 2, 1]],
        "fragments": [[0, 1, 2]],
        "name": "water / TIP3P",
        "atom_labels": ["O", "H1", "H2"],
    }

    data = H2O.to_qcschema_json()

    result = json.loads(data)
    result["geometry"] = [round(v, 6) for v in result["geometry"]]
    for key in correct.keys():
        if key not in result:
            print("----")
            pprint.pp(result)
            print("----")

            assert False, f"{key} missing from result"
        elif correct[key] != result[key]:
            print("----")
            pprint.pp(result)
            print("----")

            print(f"values for {key} differ!")
            assert correct[key] == result[key]

    for key in result.keys():
        if key not in correct:
            print("----")
            pprint.pp(result)
            print("----")

            assert False, f"extra key '{key}' in result"


def test_acetic_acid(AceticAcid):
    """Test creating QCSchema for actic acid."""
    correct = {
        "schema_name": "qcschema_molecule",
        "schema_version": 2,
        "symbols": ["C", "H", "H", "H", "C", "O", "O", "H"],
        "geometry": [
            2.040337,
            0.034204,
            -0.034771,
            1.09264,
            5.929205,
            0.53158,
            1.362304,
            -1.27292,
            -1.485136,
            1.332635,
            -0.593941,
            1.80072,
            1.079601,
            2.62653,
            -0.597342,
            -0.250011,
            3.239369,
            -2.375008,
            1.843806,
            4.340701,
            1.118529,
            4.105241,
            0.030425,
            -0.057826,
        ],
        "molecular_charge": 0,
        "molecular_multiplicity": 1,
        "connectivity": [
            [0, 1, 1],
            [0, 2, 1],
            [0, 3, 1],
            [0, 4, 1],
            [4, 5, 2],
            [4, 6, 1],
            [6, 7, 1],
        ],
        "fragments": [[0, 1, 2, 3, 4, 5, 6, 7]],
        "name": "acetic acid / acetic acid",
    }

    data = AceticAcid.to_qcschema_json()

    result = json.loads(data)
    result["geometry"] = [round(v, 6) for v in result["geometry"]]
    for key in correct.keys():
        if key not in result:
            print("----")
            pprint.pp(result)
            print("----")

            assert False, f"{key} missing from result"
        elif correct[key] != result[key]:
            print("----")
            pprint.pp(result)
            print("----")

            print(f"values for {key} differ!")
            assert correct[key] == result[key]

    for key in result.keys():
        if key not in correct:
            print("----")
            pprint.pp(result)
            print("----")

            assert False, f"extra key '{key}' in result"


def test_from_schema(configuration, AceticAcid):
    """Test creating QCSchema for actic acid."""
    correct = '{"schema_name": "qcschema_molecule", "schema_version": 2, "symbols": ["C", "H", "H", "H", "C", "O", "O", "H"], "geometry": [2.040337296754675, 0.03420404285566326, -0.03477096069304994, 1.092639645256602, 5.929204688614864, 0.5315799588562472, 1.3623035632402012, -1.2729195175455674, -1.4851357613406495, 1.3326348630836315, -0.5939409209687825, 1.80072002415257, 1.0796005349967084, 2.626530340612506, -0.5973424279931026, -0.25001076628752755, 3.2393685228275113, -2.3750077934252807, 1.843805779793958, 4.340700908257376, 1.1185288931639272, 4.10524103312944, 0.030424590606418698, -0.05782561941344175], "molecular_charge": 0, "molecular_multiplicity": 1, "connectivity": [[0, 1, 1], [0, 2, 1], [0, 3, 1], [0, 4, 1], [4, 5, 2], [4, 6, 1], [6, 7, 1]], "fragments": [[0, 1, 2, 3, 4, 5, 6, 7]], "name": "acetic acid / acetic acid"}'  # noqa: E501

    configuration.from_qcschema_json(correct)

    assert configuration == AceticAcid
