import numpy as np
from os import path
from scipy.interpolate import interp1d
import fnmatch
import yaml
import bisect
import os
from LabPy import Core  # @UnresolvedImport

materialsDir = path.join(path.dirname(path.realpath(__file__)), r"../materials") 
if not path.isdir(materialsDir):
    materialsDir = os.environ["PYLAB_MATERIALS_DIR"]

#===============================================================================
# Methods
#===============================================================================

def literal_unicode_representer(dumper, data):
    return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|')

def GetMaterialsList():
    """Generates list of all materials in database.
    
    Returns:
        list: list of paths to all available materials
    
    """
    res = ["Static"]
    for root, _ ,files in os.walk(materialsDir):
        root = root[len(materialsDir)+1:]
        files[:] = fnmatch.filter(files,'*.yml')
        files = [path.join(root, fn)[:-4] for fn in files]
        res.extend(files)
    return res

def SaveMaterialFile(filename, wls, n):
    stream = open(filename, "w")
    
    data = {}
    data["BOOK"] = "a"
    data["BOOK LONG"] = "b"
    data["PAGE"] = "c"
    data["PAGE LONG"] = "d"
    data["REFERENCES"] = "e"
    data["COMMENTS"] = "f"
    
    nkData = []
    for wl, n in zip(wls, n):
        nkData.append("%s %s %s" % (1e6 * wl, n.real, n.imag))
    nkData = "\n".join(nkData)
    
    class literal_unicode(str): pass

    yaml.add_representer(literal_unicode, literal_unicode_representer)