import pytest
import logging
import os
import sys
import inspect
from pathlib import Path
import shutil

from multipipsa.multipipsa import PipsaRun
from multipipsa.multipipsa import ApbsRun


@pytest.fixture(scope="session")
def script_directory():
    script_directory = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
    logging.debug("Script directory is \"%s\"",script_directory)
    return script_directory


@pytest.fixture(scope="session")
def pdb_home(script_directory):
    pdb_home=Path(os.path.join(script_directory,'..','data','example','pdbs/'))
    logging.debug("PDB home is \"%s\"",pdb_home)
    return pdb_home

@pytest.fixture(scope="session")
def pipsa_home(script_directory):
    pipsa_home=Path(os.path.join(script_directory,'..','data','pipsa/'))
    logging.debug("PIPSA home is \"%s\"",pipsa_home)
    return pipsa_home


@pytest.fixture(scope="session")
def testdir(tmp_path_factory):
    testdir=tmp_path_factory.mktemp("test-data")
    logging.info("Using temporary path \"%s\"",testdir)
    return testdir

@pytest.fixture(scope="session")
def examplePDBs(testdir):
    logging.debug("Using temporary path \"%s\"",testdir)
    examplePDBs = os.path.join(str(testdir), 'data', 'example', 'pdbs')
    os.makedirs(examplePDBs, exist_ok=True)
    logging.debug("Running PIPSA in \"%s\"",examplePDBs)
    return examplePDBs

@pytest.fixture(scope="session")
def localPIPSA(testdir):
    logging.debug("Using temporary path \"%s\"",testdir)
    localPIPSA =  os.path.join(str(testdir), 'data', 'pipsa')
    os.makedirs(localPIPSA, exist_ok=True)
    logging.debug("Running PIPSA from \"%s\"",localPIPSA)
    return localPIPSA

@pytest.fixture(scope="session")
def setupDir(examplePDBs ,localPIPSA,pdb_home,pipsa_home):
    try:
       shutil.copytree(pdb_home, examplePDBs,symlinks=True,dirs_exist_ok=True)
       logging.debug('Directory copied successfully: %s to %s' % (os.listdir(examplePDBs),examplePDBs))
       shutil.copytree(pipsa_home, localPIPSA,symlinks=True,dirs_exist_ok=True)
       logging.debug('Directory copied successfully: %s to %s' % (os.listdir(localPIPSA),localPIPSA))
    except shutil.Error as e:
         logging.error('Directory not copied. Error: %s' % e)
         return False
    return True

@pytest.fixture(scope="session")
def ingrp():
    ingrp = ["AC1", "AC5", "AC6"]
    return ingrp

@pytest.fixture(scope="session")
def outgrp():
    outgrp = ["AC2", "AC3", "AC4", "AC7", "AC8", "AC9"]
    return outgrp

@pytest.fixture(scope="session")
def apbs(setupDir,examplePDBs, localPIPSA, ingrp, outgrp):
    logging.debug("Setting up APBS run")
    return ApbsRun(dataDir=examplePDBs, pipsaRoot=localPIPSA,
               temp=298.15, pH="7.4", ios=0.100, structures=ingrp+outgrp)

@pytest.fixture(scope="session")
def pr(setupDir,examplePDBs, localPIPSA):
    logging.debug("Setting up PIPSA run")
    pr= PipsaRun(pipsaRoot=localPIPSA,
              dataDir=examplePDBs,
              pointsTemplate="AC5")
    return pr