# -*- coding: utf-8 -*-
"""
Created on Thu Jun 28 17:44:45 2012

@author: pietro
"""
import ctypes
import grass.lib.raster as libraster
import datetime


class History(object):
    """
    *Examples*

    ::
        >>> import grass.lib.gis as libgis
        >>> libgis.G_gisinit('')
        >>> hist = History('elevation')
        >>> hist.creator
        'helena'
        >>> hist.src1
        ''
        >>> hist.src2
        ''
        >>> hist.keyword
        'generated by r.proj'
        >>> hist.date
        datetime.datetime(2006, 11, 7, 1, 9, 51)
        >>> hist.mapset
        'PERMANENT'
        >>> hist.maptype
        'raster'
        >>> hist.title
        'elev_ned10m'

    ..
    """
    def __init__(self, name=''):
        self.c_hist = ctypes.pointer(libraster.History())
        #                'Tue Nov  7 01:11:23 2006'
        self.date_fmt = '%a %b  %d %H:%M:%S %Y'
        if name:
            self.read(name)

    def __del__(self):
        """Rast_free_history"""
        pass

    #----------------------------------------------------------------------
    #libraster.HIST_CREATOR
    def _get_creator(self):
        return libraster.Rast_get_history(self.c_hist,
                                          libraster.HIST_CREATOR)

    def _set_creator(self, creator):
        return libraster.Rast_set_history(self.c_hist,
                                          libraster.HIST_CREATOR,
                                          ctypes.c_char_p(creator))

    creator = property(fget=_get_creator, fset=_set_creator)

    #----------------------------------------------------------------------
    #libraster.HIST_DATSRC_1
    def _get_src1(self):
        return libraster.Rast_get_history(self.c_hist,
                                          libraster.HIST_DATSRC_1)

    def _set_src1(self, src1):
        return libraster.Rast_set_history(self.c_hist,
                                          libraster.HIST_DATSRC_1,
                                          ctypes.c_char_p(src1))

    src1 = property(fget=_get_src1, fset=_set_src1)

    #----------------------------------------------------------------------
    #libraster.HIST_DATSRC_2
    def _get_src2(self):
        return libraster.Rast_get_history(self.c_hist,
                                          libraster.HIST_DATSRC_2)

    def _set_src2(self, src2):
        return libraster.Rast_set_history(self.c_hist,
                                          libraster.HIST_DATSRC_2,
                                          ctypes.c_char_p(src2))

    src2 = property(fget=_get_src2, fset=_set_src2)

    #----------------------------------------------------------------------
    #libraster.HIST_KEYWORD
    def _get_keyword(self):
        return libraster.Rast_get_history(self.c_hist,
                                          libraster.HIST_KEYWRD)

    def _set_keyword(self, keyword):
        return libraster.Rast_set_history(self.c_hist,
                                          libraster.HIST_KEYWRD,
                                          ctypes.c_char_p(keyword))

    keyword = property(fget=_get_keyword, fset=_set_keyword)

    #----------------------------------------------------------------------
    #libraster.HIST_MAPID
    def _get_date(self):
        date_str = libraster.Rast_get_history(self.c_hist,
                                              libraster.HIST_MAPID)
        return datetime.datetime.strptime(date_str, self.date_fmt)

    def _set_date(self, datetimeobj):
        date_str = datetimeobj.strftime(self.date_fmt)
        return libraster.Rast_set_history(self.c_hist,
                                          libraster.HIST_MAPID,
                                          ctypes.c_char_p(date_str))

    date = property(fget=_get_date, fset=_set_date)

    #----------------------------------------------------------------------
    #libraster.HIST_MAPSET
    def _get_mapset(self):
        return libraster.Rast_get_history(self.c_hist,
                                          libraster.HIST_MAPSET)

    def _set_mapset(self, mapset):
        return libraster.Rast_set_history(self.c_hist,
                                          libraster.HIST_MAPSET,
                                          ctypes.c_char_p(mapset))

    mapset = property(fget=_get_mapset, fset=_set_mapset)

    #----------------------------------------------------------------------
    #libraster.HIST_MAPTYPE
    def _get_maptype(self):
        return libraster.Rast_get_history(self.c_hist,
                                          libraster.HIST_MAPTYPE)

    def _set_maptype(self, maptype):
        return libraster.Rast_set_history(self.c_hist,
                                          libraster.HIST_MAPTYPE,
                                          ctypes.c_char_p(maptype))

    maptype = property(fget=_get_maptype, fset=_set_maptype)

    #----------------------------------------------------------------------
    #libraster.HIST_NUM_FIELDS
    #
    # Never used in any raster modules
    #
    #    def _get_num_fields(self):
    #        return libraster.Rast_get_history(self.c_hist,
    #                                          libraster.HIST_NUM_FIELDS)
    #
    #    def _set_num_fields(self, num_fields):
    #        return libraster.Rast_set_history(self.c_hist,
    #                                          libraster.HIST_NUM_FIELDS,
    #                                          ctypes.c_char_p(num_fields))
    #
    #    num_fields = property(fget = _get_num_fields, fset = _set_num_fields)
    #----------------------------------------------------------------------
    #libraster.HIST_TITLE
    def _get_title(self):
        return libraster.Rast_get_history(self.c_hist,
                                          libraster.HIST_TITLE)

    def _set_title(self, title):
        return libraster.Rast_set_history(self.c_hist,
                                          libraster.HIST_TITLE,
                                          ctypes.c_char_p(title))

    title = property(fget=_get_title, fset=_set_title)

    def append(self, obj):
        """Rast_append_history"""
        libraster.Rast_append_history(self.c_hist,
                                      ctypes.c_char_p(str(obj)))

    def appent_fmt(self, fmt, *args):
        """Rast_append_format_history"""
        libraster.Rast_append_format_history(self.c_hist,
                                             ctypes.c_char_p(fmt),
                                             *args)

    def clear(self):
        """Rast_clear_history"""
        libraster.Rast_clear_history(self.c_hist)

    def command(self):
        """Rast_command_history"""
        libraster.Rast_command_history(self.c_hist)

    def format(self, field, fmt, *args):
        """Rast_format_history"""
        libraster.Rast_format_history(self.c_hist,
                                      ctypes.c_int(field),
                                      ctypes.c_char_p(fmt),
                                      *args)

    def length(self):
        """Rast_history_length"""
        libraster.Rast_history_length(self.c_hist)

    def line(self, line):
        """Rast_history_line"""
        libraster.Rast_history_line(self.c_hist,
                                    ctypes.c_int(line))

    def read(self, name):
        """Rast_read_history. ::

            >>> import grass.lib.gis as libgis
            >>> libgis.G_gisinit('')
            >>> import ctypes
            >>> import grass.lib.raster as libraster
            >>> hist = libraster.History()
            >>> libraster.Rast_read_history(ctypes.c_char_p('elevation'),
            ...                             ctypes.c_char_p(''),
            ...                             ctypes.byref(hist))
            0
            >>> libraster.Rast_get_history(ctypes.byref(hist),
            ...                            libraster.HIST_MAPID)
            'Tue Nov  7 01:09:51 2006'

        ..
        """
        libraster.Rast_read_history(ctypes.c_char_p(name),
                                    ctypes.c_char_p(''),
                                    self.c_hist)

    def write(self, name):
        """Rast_write_history"""
        libraster.Rast_write_history(ctypes.c_char_p(name),
                                     self.c_hist)

    def short(self, name, maptype,):
        """Rast_short_history"""
        libraster.Rast_short_history(ctypes.c_char_p(name),
                                     ctypes.c_char_p(maptype),
                                     self.c_hist)
