# -*- coding: utf-8 -*-
'''ChemShell initialisation'''

#  Copyright (C) 2017 The authors of Py-ChemShell
#
#  This file is part of Py-ChemShell.
#
#  Py-ChemShell is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Lesser General Public License as
#  published by the Free Software Foundation, either version 3 of the
#  License, or (at your option) any later version.
#
#  Py-ChemShell is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with Py-ChemShell.  If not, see
#  <http://www.gnu.org/licenses/>.

__author__ = ('You Lu <you.lu@stfc.ac.uk>',
              'Thomas W. Keal <thomas.keal@stfc.ac.uk>')

try:
    import numpy
except ModuleNotFoundError:
    from sys import exit
    print(' >>> FATAL ERROR: ChemShell cannot find NumPy\n')
    exit(999)

# YL 26/02/2021: this is in fact a "strange" implementation because `import chemsh`
#                immediately reloads the current file (a consequence is that no attributes
#                can thus be passed in before processing the current file, except by chemsh.x);
#                but I don't find a better solution yet because we do need the module object
#                `chemsh`. (An advantage is that it allows relative importing through out the
#                ChemShell package.) There are three kinds of entry points to the current file/module:
#                A. chemsh.x, which can set attributes for `chemsh`
#                B. python3 the system interpreter, which cannot set attributes so that the
#                   default CHEMSH_DEBUG is hardwired below
#                C. Importing from other Python files (e.g., chemsh/tools/plot.py) which can
#                   set attributes but will be wiped by `import chemsh` anyway
import chemsh

# if called by Python/c API, CHEMSH_ARCH will be added
# so define CHEMSH_ARCH if this is not the case (called by system Python interpreter)
if not hasattr(chemsh, 'CHEMSH_ARCH'):
    from os  import getenv
    # if not defined in env., 'gnu' is default
    setattr(chemsh, 'CHEMSH_ARCH', getenv('CHEMSH_ARCH', 'gnu'))

# path to the lib dir
if not hasattr(chemsh, 'CHEMSH_LIB_DIR'):
    from os import path
    CHEMSH_LIB_DIR = path.join(path.dirname(__file__), '..', 'build', CHEMSH_ARCH, 'lib')
    setattr(chemsh, 'CHEMSH_LIB_DIR', path.abspath(CHEMSH_LIB_DIR))

# path to the root dir
if not hasattr(chemsh, 'CHEMSH_ROOT_DIR'):
    from os import path
    CHEMSH_ROOT_DIR = path.join(path.dirname(__file__), '..')
    setattr(chemsh, 'CHEMSH_ROOT_DIR', path.abspath(CHEMSH_ROOT_DIR))

# YL 18/07/2020: has to be placed before quoting chemsh.io which will be using CHEMSH_DEBUG
# TWK 21/6/2023: To ensure consistent outcomes CHEMSH_DEBUG for python3 execution
#                should be the same as the default used by chemsh.x (i.e. 0).
#                Can be adjusted as necessary when debugging etc.
if not hasattr(chemsh, 'CHEMSH_DEBUG'):
    setattr(chemsh, 'CHEMSH_DEBUG', 0)

# print a banner
from chemsh.io import banner
banner.printBanner()

# TODO
#if getattr(chemsh, 'CHEMSH_DEBUG') > 4:
#    numpy.seterr(all='raise')
#numpy.seterr(all='raise')
#from warnings import simplefilter
#simplefilter('error')

if chemsh.CHEMSH_DEBUG > 9:
    print("\n >>> NumPy configuration:\n")
    numpy.__config__.show()
    print("\n >>>\n")

dictbuff = []
# add numpy/ctypes classes
dictbuff.extend(['LP_'])
setattr(chemsh, "CHEMSH_TYPES", sorted(list(set(dictbuff))))


# create a name list of legal ChemShell modules (must be after CHEMSH_ARCH definition)
from chemsh.dl_py2f.utils import modutils, objutils
setattr(chemsh, "CHEMSH_MODS", objutils.getModuleList(chemsh))
if not len(chemsh.CHEMSH_MODS):
    print("\n >>> ERROR: ChemShell quitting...\n")
    quit(1)

# create a list of primitive Python types
setattr(chemsh, "PYTHON_TYPES", [ int, float, complex, bool, str, list, tuple, range, bytes, bytearray, memoryview, set, frozenset, dict ])




# import modules visible to users here so that it is enough for users to do just a "from chemsh import *"
# YL 04/10/2018: this is an automated procedure now
#from importlib      import import_module
# YL 06/10/2020: added a kwarg "pkg_path" to import ChemShell-exclusive modules only

from chemsh.base import importClasses
importClasses(chemsh, pkg_path=chemsh.CHEMSH_ROOT_DIR)

# define here what the users can see
# YL 12/11/2021: added 'addons'
__all__ = 'addons', \
          'tools', \
          'QMMM', \
          'CASTEP', 'CP2K', 'DFTBplus', 'FHIaims', 'GAMESS_UK', 'Gaussian', 'LSDalton', 'MNDO', 'NWChem', 'ORCA', 'TURBOMOLE', 'Molpro', 'PySCF', \
          'DL_POLY', 'GULP', \
          'Fragment', 'Polynucleotide', 'Protein', \
          'Opt', 'SP', 'MD', 'ChargeFitting', 'Scan', 'Parameterise', \
          'Solvation', \
          'NAMD', \
          'DL_FIELD', 'DL_MONTE', \
          'Layer', 'NLayerSubtractive'




