import os
import os.path
import logging

from wwpdb.utils.config.ConfigInfo import ConfigInfo
from wwpdb.io.locator.ReleaseFileNames import ReleaseFileNames

logger = logging.getLogger(__name__)


class LocalFTPPathInfo(object):
    def __init__(self, siteId=None):
        self.__siteId = siteId
        self.__cI = ConfigInfo(siteId=self.__siteId)

        self.ftp_pdb_root = self.__cI.get('SITE_PDB_FTP_ROOT_DIR')
        self.ftp_pdb = os.path.join(self.ftp_pdb_root, 'pdb', 'data', 'structures', 'all')
        self.ftp_emdb_root = self.__cI.get('SITE_EMDB_FTP_ROOT_DIR')
        self.ftp_emdb = os.path.join(self.ftp_emdb_root, 'emdb', 'structures')
        self.__mapping = {
            'model': 'mmCIF',
            'structure_factors': 'structure_factors',
            'chemical_shifts': 'nmr_chemical_shifts',
            'nmr_data': 'nmr_data',
        }

    def __get_mapping(self, file_type):
        return self.__mapping.get(file_type)

    def get_ftp_pdb(self):
        return self.ftp_pdb

    def get_ftp_emdb(self):
        return self.ftp_emdb

    def get_model_path(self):
        return os.path.join(self.ftp_pdb, self.__get_mapping('model'))

    def get_sf_path(self):
        return os.path.join(self.ftp_pdb, self.__get_mapping('structure_factors'))

    def get_cs_path(self):
        return os.path.join(self.ftp_pdb, self.__get_mapping('chemical_shifts'))

    def get_nmr_data_path(self):
        return os.path.join(self.ftp_pdb, self.__get_mapping('nmr_data'))

    def get_model_fname(self, accession):
        model_file_name = ReleaseFileNames().get_model(accession=accession, for_release=False)
        return os.path.join(self.get_model_path(), model_file_name)

    def get_structure_factors_fname(self, accession):
        sf_file_name = ReleaseFileNames().get_structure_factor(accession=accession, for_release=False)
        return os.path.join(self.get_sf_path(), sf_file_name)

    def get_chemical_shifts_fname(self, accession):
        cs_file_name = ReleaseFileNames().get_chemical_shifts(accession=accession, for_release=False)
        return os.path.join(self.get_cs_path(), cs_file_name)

    def get_nmr_data_fname(self, accession):
        nmr_data_file_name = ReleaseFileNames().get_nmr_data(accession=accession, for_release=False)
        return os.path.join(self.get_nmr_data_path(), nmr_data_file_name)
