# -*- coding: utf-8 -*-
"""
This module contains classes to work with custom OpenFOAM classes, e.g., lists
and dictionaries.
"""

from __future__ import division, print_function
import os
import foampy


class FoamDict(dict):
    """Object that represents an OpenFOAM dictionary."""

    def __init__(self, name="", casedir="./", subdir="system"):
        self.name = name
        self.subdir = subdir
        self.casedir = casedir
        self.fpath = os.path.join(casedir, subdir, name)
        self.foam_version = foampy.foam_version
        self.header = ""
        if os.path.isfile(self.fpath):
            self.read()
        else:
            self.header = foampy.dictionaries.build_header(name,
                    self.foam_version, fileclass="dictionary")

    def read(self):
        """Parse dictionary."""
        # Read header first
        with open(self.fpath) as f:
            in_header = False
            for line in f:
                if line[:2] == "/*":
                    in_header = True
                elif line[:2] == r"\*":
                    self.header += line
                    in_header = False
                if in_header:
                    self.header += line
        # Parse header for OpenFOAM version
        splitheader = self.header.split()
        if "Version:" in splitheader:
            index = splitheader.index("Version:") + 1
            self.foam_version = splitheader[index]

    def __str__(self):
        """Create text from dictionary in OpenFOAM format."""
        return self.header


class BlockMeshDict(FoamDict):
    """Object to represent a `blockMeshDict`."""

    def __init__(self, name="blockMeshDict", casedir="./",
                 subdir="constant/polyMesh"):
        FoamDict.__init__(self)


class FoamList(list):
    """Class that represents an OpenFOAM list."""

    def __init__(self, py_list=[], dtype=float):
        py_list = [dtype(i) for i in py_list]
        list.__init__(self, py_list)

    def __str__(self):
        txt = "("
        for i in self:
            txt += str(i) + " "
        txt = txt[:-1]
        txt += ")"
        return txt


def test_foamdict():
    """Test `FoamDict` class."""
    d = FoamDict(name="controlDict", casedir="test")
    print(d)
    print(d.foam_version)


def test_foamlist():
    """Test `FoamList` class."""
    flist = FoamList([1, 2, 3, 4])
    print(flist)

    for i in flist:
        print(i)

    flist2 = FoamList([flist, flist], dtype=FoamList)
    print(flist2)

@@ -1861,18 +1861,20 @@
 lf, 
-py_
 list
-=%5B%5D
+_in=None
 , dt
@@ -1897,44 +1897,332 @@
     
-py_list = %5Bdtype(i) for i in py_list
+if isinstance(list_in, list):%0A            py_list = %5Bdtype(i) for i in list_in%5D%0A        elif isinstance(list_in, str):%0A            # Attempt to parse string into list%0A            list_in = list_in.replace(%22(%22, %22%22).replace(%22)%22, %22%22).split()%0A            py_list = %5Bdtype(i) for i in list_in%5D%0A        else:%0A            py_list=%5B
 %5D%0A  
@@ -2781,8 +2781,83 @@
 flist2)%0A
+%0A    flist = FoamList(%22(0 0 0 0)%22, dtype=int)%0A    assert flist%5B:%5D == %5B0%5D*4%0A

Allow creating FoamList from string