#                       SNS-MP2 LICENSE AGREEMENT
# 
# Copyright 2017, <NAME>. All rights reserved.
# 
# Redistribution and use of (1) the SNS-MP2 software in source and binary forms
# and (2) the associated electronic structure data released with the software,
# with or without modification, is permitted provided that the following
# conditions are met:
# 
#     * Redistributions of source code and the associated data must retain the
#     above copyright notice, this list of conditions, and the following
#     disclaimer.
# 
#     * Redistributions in binary form must reproduce the above copyright 
#     notice, this list of conditions, and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
# 
# Neither the name of <NAME> Research nor the names of its contributors may
# be used to endorse or promote products derived from this software without
# specific prior written permission.
# 
# THIS SOFTWARE AND DATA ARE PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDINGNEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE AND/OR DATA, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


import json
import os.path

from psi4.driver import qcdb
from psi4 import core
from psi4.driver.qcdb import periodictable


def inject_desres_basis():
    with open(os.path.join(os.path.dirname(__file__), 'desbasis.json')) as f:
        basis_json = json.load(f)

    for name, spans in basis_json.items():
        anon = _build_basis_funtion(name, spans)
        anon.__name__ = 'basisspec_psi4_yo__%s' % str(name).replace('-', '')
        qcdb.libmintsbasisset.basishorde[name.upper()] = anon


def _build_basis_funtion(name, spans):
    def anon(mol, role):
        for span, target in spans.items():
            start, end = span.split('-')
            for z in range(int(start), int(end)+1):
                symbol = periodictable.to_E(z)
                mol.set_basis_by_symbol(symbol, str(target), role=role)
    return anon
