
import os
import twc
import twc.products
import twccommon
import wxscan.dataUtil as dataUtil
import twc.dsmarshal as dsm
import twccommon.Log


class AnimatedMap(twc.products.Product):
    """This class supports maps that "animate" (i.e. - support multiple data
       layer images that 'move' over the map. If we ever need a map that
       supports static images over a map (like in WxScan), we'll need a
       different class."""

    def __init__(self, params):
        twc.products.Product.__init__(self, params)

        # set the max number of images that can be missing before the
        # animation loop is cancelled (-1 = unlimited)
        self._maxAllowedImageGap = -1  # image file count

        # for debugging, show any image files present regardless of
        # expiration time
        self._ignoreImageExpiration = 0

        # for debugging, show any image files present regardless of
        # any gaps in the image animation
        self._ignoreTimeGaps        = 0

        # set image root
        TWCPERSDIR = os.environ['TWCPERSDIR']
        imageRoot = TWCPERSDIR + '/data/volatile/images'
        self.updateData(
                imageRoot=imageRoot,

                # list of data images to display
                imageList = [],

                # image parameters (-1 = undefined)
                imageFrequency = -1, # in seconds
                maxImages      = -1, # image file count

                # display "timing" for each image: show image for 'x' frames
                # (-1 = undefined)
                imageDuration      = -1,  # in frames
                lastImageDuration  = -1)  # in frames (frame count)
        data = self.getData()


    def _loadData(self): 
        params = twccommon.DefaultedData(self.getParams())
        data   = self.getData()

        # Assume there is no data
        data.noDataAvailable = 1

        # setup the product config string name, we'll use this a lot
        productString = 'Config.%s.%s.%s' % (dsm.getConfigVersion(), params.product, params.productInst)

        # see if we have any data:
        data.imageList = dataUtil.getValidFileList(dataPath=data.imageRoot,
                                                   prefix=productString,
                                                   suffix='*[0-9].tif',
                                                   startTimeNdx=4, endTimeNdx=5, sortIndex=4)

        # IF we need to check for gaps AND we found valid images
        if (self._maxAllowedImageGap >= 0) and (len(data.imageList) > 0):
            # make sure there aren't any time gaps!
            # this needs to be the list of ALL valid images because this
            # function will clean up this product's valid images if there
            # is a time gap in the loop (per the reqs).
            data.imageList = dataUtil.checkImageListForGaps(
                params.product, data.imageList, data.imageFrequency,
                self._maxAllowedImageGap, self._ignoreTimeGaps)

        # per the requirements, restrict the time period to MAX_IMAGES
        data.imageList = data.imageList[0:data.maxImages]

        # If we still have images after checking for gaps
        if len(data.imageList) > 0:
            # Clear the noDataAvailable flag
            data.noDataAvailable = 0

        # SORT oldest to newest (IMPORTANT: we MUST slice THEN reverse! Not the
        # other way around!)
        data.imageList.reverse()

        # Update data with params map data which does not need resolving
        data.vector = params.vector
        data.textString = params.textString
        data.tiffImage = params.tiffImage
        data.labeledTiffImage = params.labeledTiffImage

